-
Notifications
You must be signed in to change notification settings - Fork 449
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a2abbf
commit b5216ab
Showing
12 changed files
with
399 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.