Skip to content

Commit

Permalink
1st round of fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdan-tron committed Feb 26, 2024
1 parent 3e13258 commit 8fc4c34
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
14 changes: 13 additions & 1 deletion src/controllers/expenses.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const create = async(req, res) => {
|| typeof spentAt !== 'string'
|| !title
|| !amount
// || typeof category !== 'string'
|| !user
) {
return res.sendStatus(400);
Expand All @@ -45,12 +46,13 @@ const create = async(req, res) => {

const getOne = async(req, res) => {
const id = parseInt(req.params.id);
const product = await expensesService.getOne(id);

if (typeof id !== 'number' || !id) {
return res.sendStatus(400);
}

const product = await expensesService.getOne(id);

if (!product) {
return res.sendStatus(404);
}
Expand All @@ -60,6 +62,11 @@ const getOne = async(req, res) => {

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

if (typeof id !== 'number' || !id) {
return res.sendStatus(400);
}

const product = await expensesService.getOne(id);

if (!product) {
Expand All @@ -74,6 +81,11 @@ const remove = async(req, res) => {
const update = async(req, res) => {
const id = parseInt(req.params.id);
const { ...args } = req.body;

if (typeof id !== 'number' || !id) {
return res.sendStatus(400);
}

const product = await expensesService.getOne(id);

if (!product) {
Expand Down
20 changes: 18 additions & 2 deletions src/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ const create = async(req, res) => {

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

if (typeof id !== 'number' || !id) {
return res.sendStatus(400);
}

const neededUser = await userService.getOne(id);

if (!neededUser) {
Expand All @@ -31,6 +36,11 @@ const getOne = async(req, res) => {

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

if (typeof id !== 'number' || !id) {
return res.sendStatus(400);
}

const user = await userService.getOne(id);

if (!user) {
Expand All @@ -45,12 +55,18 @@ const remove = async(req, res) => {
const update = async(req, res) => {
const id = parseInt(req.params.id);
const { name } = req.body;
const user = await userService.getOne(id);

if (typeof name !== 'string' || !name.length) {
if (
typeof name !== 'string'
|| typeof id !== 'number'
|| !name.length
|| !id
) {
return res.sendStatus(400);
}

const user = await userService.getOne(id);

if (!user) {
return res.sendStatus(404);
}
Expand Down
4 changes: 1 addition & 3 deletions src/services/expenses.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@ const create = async(
category = 'uncategorized',
note = null,
) => {
const expense = await Expense.create({
return Expense.create({
userId,
spentAt,
title,
amount,
category,
note,
});

return expense;
};

const update = async(id, args) => {
Expand Down
4 changes: 1 addition & 3 deletions src/services/user.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ const getAll = async() => User.findAll();
const remove = async(id) => User.destroy({ where: { id } });

const create = async(name) => {
const user = await User.create({
return User.create({
name,
});

return user;
};

const update = (id, name) => {
Expand Down

0 comments on commit 8fc4c34

Please sign in to comment.