Skip to content

Commit

Permalink
adding task solution
Browse files Browse the repository at this point in the history
  • Loading branch information
eduvinagre committed Jul 11, 2024
1 parent 3a2abbf commit a442fc0
Show file tree
Hide file tree
Showing 11 changed files with 423 additions and 7 deletions.
117 changes: 117 additions & 0 deletions src/controllers/expenseController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/* eslint-disable no-console */
'use strict';

const { ExpenseService } = require('../services/expenseService');
const expenseService = new ExpenseService();
const userService = require('../services/userService');

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

if (!spentAt || !title || !amount || !userId) {
return res.status(400).send('All fields are required');
}

const user = await userService.getAllUsers({ id: userId });

if (user.length === 0) {
return res.status(400).send('Bad Request');
}

const newExpense = await expenseService.createExpense(
userId,
spentAt,
title,
amount,
category,
note,
);

res.status(201).json(newExpense);
} catch (error) {
console.error(error);
res.status(500).send('Error creating expense');
}
}

async function getAllExpenses(req, res) {
try {
const userId = req.query.userId;
const category = req.query.categories;
const filters = {};

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

if (category) {
filters.category = category;
}

const expenses = await expenseService.getAllExpenses(filters);

res.json(expenses);
} catch (error) {
console.error(error);
res.status(500).send('Error getting expenses');
}
}

async function getExpense(req, res) {
try {
const [expense] = await expenseService.getAllExpenses({
id: parseInt(req.params.id),
});

if (!expense) {
return res.status(404).send('Expense not found');
}

res.json(expense);
} catch (error) {
console.error(error);
res.status(500).send('Error getting expense');
}
}

async function updateExpense(req, res) {
try {
const expense = await expenseService.updateExpense(
parseInt(req.params.id),
req.body,
);

if (!expense) {
return res.status(404).send('Expense not found');
}

res.json(expense);
} catch (error) {
console.error(error);
res.status(500).send('Error updating expense');
}
}

async function deleteExpense(req, res) {
try {
const success = await expenseService.deleteExpense(parseInt(req.params.id));

if (!success) {
return res.status(404).send('Expense not found');
}

res.status(204).send();
} catch (error) {
console.error(error);
res.status(500).send('Error deleting expense');
}
}

module.exports = {
createExpense,
getAllExpenses,
getExpense,
updateExpense,
deleteExpense,
};
90 changes: 90 additions & 0 deletions src/controllers/userController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
'use strict';

const userService = require('../services/userService');

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

if (!name) {
return res.status(400).send('Name and are required');
}

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

res.status(201).json(newUser);
} catch (error) {
res.status(500).send('Error creating user');
}
}

async function getAllUsers(req, res) {
try {
const users = await userService.getAllUsers(req.query);

res.json(users);
} catch (error) {
res.status(500).send('Error fetching users');
}
}

async function getUser(req, res) {
try {
const users = await userService.getAllUsers({
id: parseInt(req.params.id),
});
const user = users[0];

if (!user) {
return res.status(404).send('User not found');
}

res.json(user);
} catch (error) {
res.status(500).send('Error fetching user');
}
}

async function updateUser(req, res) {
const { name } = req.body;

if (!name) {
return res.status(400).send('Name and are required');
}

try {
const user = await userService.updateUser(parseInt(req.params.id), {
name,
});

if (!user) {
return res.status(404).send('User not found');
}

res.json(user);
} catch (error) {
res.status(500).send('Error updating user');
}
}

async function deleteUser(req, res) {
try {
const success = await userService.deleteUser(parseInt(req.params.id));

if (!success) {
return res.status(404).send('User not found');
}

res.status(204).send();
} catch (error) {
res.status(500).send('Error deleting user');
}
}

module.exports = {
createUser,
getAllUsers,
getUser,
updateUser,
deleteUser,
};
16 changes: 13 additions & 3 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
'use strict';

const createServer = () => {
// your code goes here
};
const express = require('express');
const userRoutes = require('./routes/userRoutes');
const expenseRoutes = require('./routes/expenseRoutes');

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

app.use(express.json());
app.use('/users', userRoutes);
app.use('/expenses', expenseRoutes);

return app;
}

module.exports = {
createServer,
Expand Down
4 changes: 2 additions & 2 deletions src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const {
} = process.env;

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

Expand All @@ -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 || 'yasmart',
});

module.exports = {
Expand Down
13 changes: 13 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,21 @@

'use strict';

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

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

createServer().listen(5700, () => {
console.log('Server is running on localhost:5700');
});

(async () => {
try {
await sequelize.sync({ force: true });
console.log('Database synchronized');
} catch (error) {
console.error('Unable to synchronize the database:', error);
} finally {
await sequelize.close();
}
})();
32 changes: 31 additions & 1 deletion src/models/Expense.model.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,39 @@
'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.FLOAT,
allowNull: false,
},
category: {
type: DataTypes.STRING,
allowNull: true,
},
note: {
type: DataTypes.STRING,
allowNull: true,
},
},
{
timestamps: false,
},
);

module.exports = {
Expand Down
18 changes: 17 additions & 1 deletion src/models/User.model.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
'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,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
},
{
tableName: 'users',
timestamps: false,
},
);

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

const express = require('express');
const expenseController = require('../controllers/expenseController');

const router = express.Router();

router.post('/', expenseController.createExpense);
router.get('/', expenseController.getAllExpenses);
router.get('/:id', expenseController.getExpense);
router.patch('/:id', expenseController.updateExpense);
router.delete('/:id', expenseController.deleteExpense);

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

const express = require('express');
const userController = require('../controllers/userController');

const router = express.Router();

router.post('/', userController.createUser);
router.get('/', userController.getAllUsers);
router.get('/:id', userController.getUser);
router.patch('/:id', userController.updateUser);
router.delete('/:id', userController.deleteUser);

module.exports = router;
Loading

0 comments on commit a442fc0

Please sign in to comment.