Skip to content

Commit

Permalink
solution
Browse files Browse the repository at this point in the history
  • Loading branch information
kapesha committed Sep 25, 2024
1 parent 56fe035 commit df9a80a
Show file tree
Hide file tree
Showing 10 changed files with 359 additions and 4 deletions.
88 changes: 88 additions & 0 deletions src/controllers/expenceController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const expenseService = require('../services/expenceService.js');
const userService = require('../services/userService.js');

const getExpenses = async (req, res) => {
const { userId, categories, from, to } = req.query;

res.send(await expenseService.getAllExpenses(userId, categories, from, to));
};

const getOneExpense = async (req, res) => {
const { id } = req.params;
const expense = await expenseService.getExpenceById(id);

if (!expense) {
res.sendStatus(404);

return;
}
res.status(200);
res.json(expense);
};

const createExpance = async (req, res) => {
const { userId, spentAt, title, amount, category, note } = req.body;

if (!userId || !spentAt || !title || !amount) {
res.sendStatus(400);

return;
}

if (!(await userService.getUserById(userId))) {
res.sendStatus(400);

return;
}

const expense = await expenseService.createExpence(
userId,
spentAt,
title,
amount,
category,
note,
);

res.status(201).json(expense);
};

const updateExpense = async (req, res) => {
const { id } = req.params;

if (!(await expenseService.getExpenceById(id))) {
res.sendStatus(404);

return;
}

const updatedExpense = await expenseService.patchExpence(id, req.body);

if (!updatedExpense) {
res.sendStatus(404);

return;
}
res.status(200).json(updatedExpense);
};

const removeExpense = async (req, res) => {
const { id } = req.params;

if (!(await expenseService.getExpenceById(id))) {
res.sendStatus(404);

return;
}

await expenseService.deleteExpence(id);
res.sendStatus(204);
};

module.exports = {
getExpenses,
getOneExpense,
createExpance,
updateExpense,
removeExpense,
};
73 changes: 73 additions & 0 deletions src/controllers/userController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const userService = require('../services/userService.js');

const getUsers = async (req, res) => {
res.send(await userService.getAllUsers());
};

const getUser = async (req, res) => {
const { id } = req.params;
const user = await userService.getUserById(id);

if (!user) {
res.sendStatus(404);

return;
}
res.status(200);
res.json(user);
};

const createUser = async (req, res) => {
const { name } = req.body;

if (!name) {
res.sendStatus(400);

return;
}

const user = await userService.createUser(name);

res.status(201).json(user);
};

const updateUser = async (req, res) => {
const { id } = req.params;

if (!(await userService.getUserById(id))) {
res.sendStatus(404);

return;
}

const { name } = req.body;
const updatedUser = await userService.patchUser({ id, name });

if (!updatedUser) {
res.sendStatus(404);

return;
}
res.status(200).json(updatedUser);
};

const removeUser = async (req, res) => {
const { id } = req.params;

if (!(await userService.getUserById(id))) {
res.sendStatus(404);

return;
}

await userService.deleteUser(id);
res.sendStatus(204);
};

module.exports = {
getUser,
getUsers,
createUser,
updateUser,
removeUser,
};
11 changes: 10 additions & 1 deletion src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
'use strict';

const express = require('express');
const { userRoute } = require('./routes/userRoute.js');
const { expenseRoute } = require('./routes/expenseRoute.js');

const createServer = () => {
// your code goes here
const app = express();

app.use('/users', express.json(), userRoute);
app.use('/expenses', express.json(), expenseRoute);

return app;
};

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const sequelize = new Sequelize({
host: POSTGRES_HOST || 'localhost',
dialect: 'postgres',
port: POSTGRES_PORT || 5432,
password: POSTGRES_PASSWORD || '123',
password: POSTGRES_PASSWORD || '0107299',
});

module.exports = {
Expand Down
39 changes: 38 additions & 1 deletion src/models/Expense.model.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,46 @@
'use strict';

const { DataTypes } = require('sequelize');
const { sequelize } = require('../db.js');

const Expense = sequelize.define(
// your code goes here
'Expence',
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
userId: {
type: DataTypes.INTEGER,
allowNull: false,
},
spentAt: {
type: DataTypes.STRING,
allowNull: false,
},
title: {
type: DataTypes.STRING,
allowNull: false,
},
amount: {
type: DataTypes.INTEGER,
allowNull: false,
},
category: {
type: DataTypes.STRING,
allowNull: true,
},
note: {
type: DataTypes.STRING,
allowNull: true,
},
},
{
tableName: 'expences',
updatedAt: false,
createdAt: false,
},
);

module.exports = {
Expand Down
19 changes: 18 additions & 1 deletion src/models/User.model.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
'use strict';

const { DataTypes } = require('sequelize');
const { sequelize } = require('../db.js');

const User = sequelize.define(
// your code goes here
'User',
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
},
{
tableName: 'users',
updatedAt: false,
createdAt: false,
},
);

module.exports = {
Expand Down
12 changes: 12 additions & 0 deletions src/routes/expenseRoute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const express = require('express');
const expenceController = require('../controllers/expenceController.js');

const expenseRoute = express.Router();

expenseRoute.get('/', expenceController.getExpenses);
expenseRoute.get('/:id', expenceController.getOneExpense);
expenseRoute.post('/', expenceController.createExpance);
expenseRoute.patch('/:id', expenceController.updateExpense);
expenseRoute.delete('/:id', expenceController.removeExpense);

module.exports = { expenseRoute };
12 changes: 12 additions & 0 deletions src/routes/userRoute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const express = require('express');
const userController = require('../controllers/userController.js');

const userRoute = express.Router();

userRoute.get('/', userController.getUsers);
userRoute.get('/:id', userController.getUser);
userRoute.post('/', userController.createUser);
userRoute.patch('/:id', userController.updateUser);
userRoute.delete('/:id', userController.removeUser);

module.exports = { userRoute };
71 changes: 71 additions & 0 deletions src/services/expenceService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const { Op } = require('sequelize');
const { models } = require('../models/models.js');
const { Expense } = models;

const getAllExpenses = async (userId, categories, from, to) => {
const whereClause = {};

if (userId) {
whereClause.userId = userId;
}

if (categories && categories.length > 0) {
whereClause.category = categories;
}

if (from) {
whereClause.spentAt = { [Op.gte]: from };
}

if (to) {
whereClause.spentAt = { [Op.lte]: to };
}

return Expense.findAll({ where: whereClause });
};

const getExpenceById = async (id) => {
return Expense.findByPk(id);
};

const createExpence = async (
userId,
spentAt,
title,
amount,
category,
note,
) => {
return Expense.create({
userId,
spentAt,
title,
amount,
category,
note,
});
};

const patchExpence = async (id, data) => {
await Expense.update(data, {
where: { id },
});

return Expense.findByPk(id);
};

const deleteExpence = async (id) => {
await Expense.destroy({
where: {
id,
},
});
};

module.exports = {
getAllExpenses,
getExpenceById,
createExpence,
patchExpence,
deleteExpence,
};
Loading

0 comments on commit df9a80a

Please sign in to comment.