Skip to content

Commit

Permalink
add_solution
Browse files Browse the repository at this point in the history
  • Loading branch information
metrakonon committed Jul 17, 2024
1 parent 3a2abbf commit b5216ab
Show file tree
Hide file tree
Showing 12 changed files with 399 additions and 7 deletions.
112 changes: 112 additions & 0 deletions src/controllers/expenses.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
const expensesService = require('./../services/expenses.service.js');
const userService = require('./../services/users.service.js');

const STATUS_CODES = {
OK: 200,
CREATED: 201,
NO_CONTENT: 204,
BAD_REQUEST: 400,
NOT_FOUND: 404,
};

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

res.statusCode = STATUS_CODES.OK;
res.send(await expensesService.getAll(userId, categories, from, to));
};

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

if (!id) {
res.sendStatus(STATUS_CODES.BAD_REQUEST);

return;
}

const expenses = await expensesService.getById(id);

if (!expenses) {
res.sendStatus(STATUS_CODES.NOT_FOUND);

return;
}

res.statusCode = STATUS_CODES.OK;
res.send(expenses);
};

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

if (!(userId && spentAt && title && amount)) {
res.sendStatus(STATUS_CODES.BAD_REQUEST);

return;
}

const user = await userService.getById(userId);

if (!user) {
res.sendStatus(STATUS_CODES.BAD_REQUEST);

return;
}

const item = await expensesService.create(
userId,
spentAt,
title,
amount,
category,
note,
);

res.statusCode = STATUS_CODES.CREATED;
res.send(item);
};

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

if (!(await expensesService.getById(id))) {
res.sendStatus(STATUS_CODES.NOT_FOUND);

return;
}

await expensesService.remove(id);

res.sendStatus(STATUS_CODES.NO_CONTENT);
};

const patch = async (req, res) => {
const { id } = req.params;
const { title } = req.body;

if (!(await expensesService.getById(id))) {
res.sendStatus(STATUS_CODES.NOT_FOUND);

return;
}

if (typeof title !== 'string') {
res.sendStatus(STATUS_CODES.BAD_REQUEST);

return;
}

const user = await expensesService.change(id, title);

res.statusCode = STATUS_CODES.OK;
res.send(user);
};

module.exports = {
get,
getOne,
post,
remove,
patch,
};
86 changes: 86 additions & 0 deletions src/controllers/users.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const userService = require('./../services/users.service.js');

const STATUS_CODES = {
OK: 200,
CREATED: 201,
NO_CONTENT: 204,
BAD_REQUEST: 400,
NOT_FOUND: 404,
};

const get = async (req, res) => {
res.statusCode = STATUS_CODES.OK;

res.send(await userService.getAll());
};

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

if (!user) {
res.sendStatus(STATUS_CODES.NOT_FOUND);

return;
}

res.statusCode = STATUS_CODES.OK;
res.send(user);
};
const post = async (req, res) => {
const { name } = req.body;

if (!name) {
res.sendStatus(STATUS_CODES.BAD_REQUEST);

return;
}

const item = await userService.create(name);

if (!item) {
res.sendStatus(STATUS_CODES.BAD_REQUEST);

return;
}

res.statusCode = STATUS_CODES.CREATED;
res.send(item);
};
const remove = async (req, res) => {
const { id } = req.params;

if (!(await userService.getById(id))) {
res.sendStatus(STATUS_CODES.NOT_FOUND);

return;
}

await userService.remove(id);

res.sendStatus(STATUS_CODES.NO_CONTENT);
};

const patch = async (req, res) => {
const { id } = req.params;
const { name } = req.body;

if (!(await userService.getById(id))) {
res.sendStatus(STATUS_CODES.NOT_FOUND);

return;
}

const user = await userService.change(id, name);

res.statusCode = STATUS_CODES.OK;
res.send(user);
};

module.exports = {
get,
getOne,
post,
remove,
patch,
};
15 changes: 14 additions & 1 deletion src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
'use strict';

const cors = require('cors');
const express = require('express');
const usersRouter = require('./routes/users.route');
const expensesRouter = require('./routes/expenses.route');

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

app.use(cors());

app.use('/users', express.json(), usersRouter);

app.use('/expenses', express.json(), expensesRouter);

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 || '1234',
});

module.exports = {
Expand Down
9 changes: 9 additions & 0 deletions src/helper/generateId.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

const generateId = (arr) => {
const newId = Math.max(0, ...arr.map((item) => item.id)) + 1;

return newId;
};

module.exports = { generateId };
8 changes: 8 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

const { createServer } = require('./createServer');

createServer().listen(3000, () => {
// eslint-disable-next-line no-console
console.log('Server is running on localhost:3000');
});
31 changes: 30 additions & 1 deletion src/models/Expense.model.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
'use strict';

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

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

module.exports = {
Expand Down
15 changes: 11 additions & 4 deletions src/models/User.model.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
'use strict';

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

const User = sequelize.define(
// your code goes here
'User',
{
name: {
type: DataTypes.STRING,
allowNull: false,
},
},
{
updatedAt: false,
},
);

module.exports = {
Expand Down
19 changes: 19 additions & 0 deletions src/routes/expenses.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

const EXPENSES_ROUTES = {
BASE: '/',
ID: '/:id',
};

const expensesController = require('./../controllers/expenses.controller.js');

const express = require('express');
const expensesRouter = express.Router();

expensesRouter.get(EXPENSES_ROUTES.BASE, expensesController.get);
expensesRouter.get(EXPENSES_ROUTES.ID, expensesController.getOne);
expensesRouter.post(EXPENSES_ROUTES.BASE, expensesController.post);
expensesRouter.delete(EXPENSES_ROUTES.ID, expensesController.remove);
expensesRouter.patch(EXPENSES_ROUTES.ID, expensesController.patch);

module.exports = expensesRouter;
18 changes: 18 additions & 0 deletions src/routes/users.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const userController = require('./../controllers/users.controller.js');
const express = require('express');
const userRouter = express.Router();

const ROUTES = {
BASE: '/',
ID: '/:id',
};

userRouter.get(ROUTES.BASE, userController.get);
userRouter.get(ROUTES.ID, userController.getOne);
userRouter.post(ROUTES.BASE, userController.post);
userRouter.delete(ROUTES.ID, userController.remove);
userRouter.patch(ROUTES.ID, userController.patch);

module.exports = userRouter;
Loading

0 comments on commit b5216ab

Please sign in to comment.