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

server created #294

Open
wants to merge 1 commit 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
93 changes: 93 additions & 0 deletions src/controllers/expenses.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
const expenseService = require('../services/expenses.service');
const userService = require('../services/users.service');

const getAll = async (req, res) => {
try {
res.statusCode = 200;
res.send(await expenseService.getAll(req.query));
} catch (error) {
res.statusCode = 500;
res.send('Something went wrong');
}
};

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

const expense = await expenseService.getOne(id);

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

return;
}

try {
res.statusCode = 200;
res.send(expense);
} catch (error) {
res.statusCode = 500;
res.send('Something went wrong');
}
};

const createExpense = async (req, res) => {
const { userId } = req.body;

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

return;
}

try {
res.statusCode = 201;
res.send(await expenseService.createExpense(req.body));
} catch (error) {
res.statusCode = 500;
res.send('Something went wrong');
}
};

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

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

return;
}

try {
res.send(await expenseService.updateExpense(id, req.body));
} catch (error) {
res.statusCode = 500;
res.send('Something went wrong');
}
};

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

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

return;
}

try {
await expenseService.removeExpense(id);
res.sendStatus(204);
} catch (error) {
res.statusCode = 500;
res.send('Something went wrong');
}
};

module.exports = {
getAll,
getOne,
createExpense,
updateExpense,
removeExpense,
};
87 changes: 87 additions & 0 deletions src/controllers/users.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const userService = require('../services/users.service');

const getAll = async (req, res) => {
try {
res.send(await userService.getAll());
} catch (error) {
res.statusCode = 500;
res.send('Something went wrong');
}
};

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

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

return;
}

res.send(user);
} catch {
res.statusCode = 500;
res.send('Something went wrong');
}
};

const createUser = async (req, res) => {
if (!req.body.name) {
res.sendStatus(400);

return;
}

try {
res.statusCode = 201;
res.send(await userService.createUser(req.body));
} catch {
res.statusCode = 500;
res.send('Something went wrong');
}
};

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

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

return;
}

try {
res.send(await userService.updateUser(id, req.body));
} catch {
res.statusCode = 500;
res.send('Something went wrong');
}
};

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

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

return;
}

await userService.removeUser(id);
res.sendStatus(204);
} catch {
res.statusCode = 500;
res.send('Something went wrong');
}
};

module.exports = {
getAll,
getOne,
createUser,
updateUser,
removeUser,
};
17 changes: 14 additions & 3 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
'use strict';

const createServer = () => {
// your code goes here
};
const express = require('express');
const usersRouter = require('./routes/users.route');
const expensesRouter = require('./routes/expenses.route');

function createServer() {
const app = express();

app.use(express.json());

app.use('/users', usersRouter);
app.use('/expenses', expensesRouter);

return app;
}

module.exports = {
createServer,
Expand Down
8 changes: 1 addition & 7 deletions src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const { Sequelize } = require('sequelize');
const utils = require('util');

// Needed for testing purposes, do not remove
require('dotenv').config();
global.TextEncoder = utils.TextEncoder;

Expand All @@ -15,18 +14,13 @@ const {
POSTGRES_DB,
} = process.env;

/*
All credentials setted to default values (exsept password - it is exapmle)
replace if needed with your own
*/

const sequelize = new Sequelize({
database: POSTGRES_DB || 'postgres',
username: POSTGRES_USER || 'postgres',
host: POSTGRES_HOST || 'localhost',
dialect: 'postgres',
port: POSTGRES_PORT || 5432,
password: POSTGRES_PASSWORD || '123',
password: POSTGRES_PASSWORD || '69301410',
});

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,
autoIncrement: true,
primaryKey: true,
allowNull: false,
},
userId: {
type: DataTypes.INTEGER,
allowNull: false,
},
spentAt: {
type: DataTypes.DATE,
// defaultValue: DataTypes.NOW,
allowNull: false,
},
title: {
type: DataTypes.STRING,
allowNull: false,
},
amount: {
type: DataTypes.INTEGER,
allowNull: false,
},
category: {
type: DataTypes.STRING,
},
note: {
type: DataTypes.STRING,
},
},
{
tableName: 'expenses',
createdAt: false,
updatedAt: false,
},
);

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

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

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

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

router.get('/', expenseController.getAll);
router.get('/:id', expenseController.getOne);
router.post('/', expenseController.createExpense);
router.patch('/:id', expenseController.updateExpense);
router.delete('/:id', expenseController.removeExpense);

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

router.get('/', userController.getAll);
router.get('/:id', userController.getOne);
router.post('/', userController.createUser);
router.patch('/:id', userController.updateUser);
router.delete('/:id', userController.removeUser);

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

const getAll = async ({ userId, categories, from, to }) => {
const where = {};

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

if (categories) {
where.category = categories;
}

if (from || to) {
where.spentAt = { [Op.between]: [from, to] };
}

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

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

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

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

return Expense.findByPk(id);
};

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

module.exports = {
getAll,
getOne,
createExpense,
updateExpense,
removeExpense,
};
Loading
Loading