Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node_accounting-app-with-db #239

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x]
node-version: [20.x]
services:
postgres:
image: postgres:latest
Expand All @@ -31,7 +31,7 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@v1
with:
node-version: '14'
node-version: '20'
- name: Install dependencies
run: npm install
- name: Run tests
Expand Down
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,3 @@ node_modules

# MacOS
.DS_Store

.env.local
289 changes: 165 additions & 124 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.2",
"pg": "^8.11.3",
"sequelize": "^6.35.2"
"pg": "^8.11.5",
"pg-hstore": "^2.3.4",
"sequelize": "^6.37.3"
},
"devDependencies": {
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.7.3",
"@mate-academy/scripts": "^1.8.1",
"axios": "^1.6.7",
"eslint": "^7.32.0",
"eslint-plugin-jest": "^22.4.1",
Expand Down
104 changes: 104 additions & 0 deletions src/controllers/expenses.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
const expensesService = require('./../services/expenses.service.js');
const userService = require('./../services/user.service.js');

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

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

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

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

return;
}

const expenses = await expensesService.getById(id);

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

return;
}

res.statusCode = 200;
res.send(expenses);
};

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

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

return;
}

const user = await userService.getById(userId);

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

return;
}

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

res.statusCode = 201;
res.send(item);
};

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

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

return;
}

await expensesService.remove(id);

res.sendStatus(204);
};

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

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

return;
}

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

return;
}

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

res.statusCode = 200;
res.send(user);
};

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

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

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

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

const user = await userService.getById(id);

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

return;
}

res.statusCode = 200;
res.send(user);
};

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

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

return;
}

const item = await userService.create(name);

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

return;
}

res.statusCode = 201;
res.send(item);
};

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

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

return;
}

await userService.remove(id);

res.sendStatus(204);
};

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

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

return;
}

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

res.statusCode = 200;
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 userRouter = require('./routes/user.router');
const expensesRouter = require('./routes/expenses.router');

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

app.use(cors());

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

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

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
'Expense',
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
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,
allowNull: true,
},
note: {
type: DataTypes.STRING,
allowNull: true,
},
},
{
tableName: 'expenses',
createdAt: false,
updatedAt: 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',
createdAt: false,
updatedAt: false,
},
);

module.exports = {
Expand Down
16 changes: 16 additions & 0 deletions src/routes/expenses.router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const expensesController = require('./../controllers/expenses.controller.js');

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

router.get('/', expensesController.get);

router.get('/:id', expensesController.getOne);

router.post('/', expensesController.post);

router.delete('/:id', expensesController.remove);

router.patch('/:id', expensesController.patch);

module.exports = router;
16 changes: 16 additions & 0 deletions src/routes/user.router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const userController = require('./../controllers/user.controller.js');

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

router.get('/', userController.get);

router.get('/:id', userController.getOne);

router.post('/', userController.post);

router.delete('/:id', userController.remove);

router.patch('/:id', userController.patch);

module.exports = router;
Loading
Loading