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

solution #249

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
175 changes: 175 additions & 0 deletions src/controllers/expense.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
const expenseService = require('../services/expense.service');
const userService = require('../services/user.service');

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

let { userId } = query;
const { categories } = query;
const filter = {};

if (userId) {
userId = Number(userId);

if (!Number.isInteger(userId)) {
res.sendStatus(400);

return;
}

filter.userId = userId;
}

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

try {
const expenses = await expenseService.get(filter);

res.send(expenses);
} catch {
res.sendStatus(500);
}
};

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

id = Number(id);

try {
const expense = await expenseService.getExpenseById(id);

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

return;
}

res.send(expense);
} catch {
res.sendStatus(500);
}
};

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

const expenseBody = {
userId,
spentAt,
title,
amount,
category: category || null,
note: note || null,
};

if (!Object.values(expenseBody).every((v) => v !== undefined)) {
res.sendStatus(400);

return;
}

const isUser = await userService.getUserById(Number(userId));

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

return;
}

try {
const expense = await expenseService.createExpense(expenseBody);

res.statusCode = 201;
res.send(expense);
} catch {
res.sendStatus(400);
}
};

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

id = Number(id);

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

return;
}

try {
const isExpense = await expenseService.getExpenseById(id);

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

return;
}

await expenseService.removeExpense(id);

res.sendStatus(204);
} catch {
res.sendStatus(400);
}
};

const updateExpense = async (req, res) => {
let { id } = req.params;
const { spentAt, title, amount, category, note } = req.body;

id = Number(id);

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

return;
}

const isExpense = await expenseService.getExpenseById(id);

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

return;
}

const newExpenseBody = {
spentAt,
title,
amount,
category,
note,
};

for (const key in newExpenseBody) {
if (!newExpenseBody[key]) {
delete newExpenseBody[key];
}
}

if (!Object.values(newExpenseBody).length) {
res.sendStatus(422);

return;
}

try {
const expense = await expenseService.updateExpense(id, newExpenseBody);

res.send(expense[1][0]);
} catch {
res.sendStatus(400);
}
};

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

const get = async (req, res) => {
try {
const data = await userService.getUsers();

res.send(data);
} catch {
res.sendStatus(500);
}
};

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

id = Number(id);

try {
const user = await userService.getUserById(id);

if (user === null) {
res.sendStatus(404);

return;
}

res.send(user);
} catch {
res.sendStatus(500);
}
};

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

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

return;
}

try {
const user = await userService.createUser(name);

res.statusCode = 201;
res.send(user);
} catch {
res.sendStatus(400);
}
};

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

id = Number(id);

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

return;
}

try {
const deletedUser = await userService.getUserById(id);

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

return;
}

await userService.deleteUser(id);

res.sendStatus(204);
} catch {
res.sendStatus(400);
}
};

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

id = Number(id);

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

return;
}

try {
const isUser = await userService.getUserById(id);

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

return;
}

const updatedUser = await userService.updateUser(id, name);

res.send(updatedUser[1][0]);
} catch {
res.sendStatus(400);
}
};

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

const createServer = () => {
// your code goes here
};
const express = require('express');
const { router: userRouter } = require('./routes/user.route');
const { router: expenseRouter } = require('./routes/expense.route');

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

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

return app;
}

module.exports = {
createServer,
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 || '123456',
});

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 { sequelize } = require('../db.js');
const { DataTypes } = require('sequelize');

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,
defaultValue: DataTypes.NOW,
},
title: {
type: DataTypes.STRING,
allowNull: false,
},
amount: {
type: DataTypes.INTEGER,
defaultValue: 0,
allowNull: false,
},
category: {
type: DataTypes.STRING,
},
note: {
type: DataTypes.STRING,
},
},
{
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 { sequelize } = require('../db.js');
const { DataTypes } = require('sequelize');

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
Loading
Loading