Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 44 additions & 0 deletions .github/workflows/test.yml-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Test

on:
pull_request:
branches: [master]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20.x]
services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: students
POSTGRES_PORT: 5432
POSTGRES_HOST: localhost
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v1
with:
node-version: '20'
- name: Install dependencies
run: npm install
- name: Run tests
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: students
POSTGRES_HOST: localhost
POSTGRES_PORT: 5432
run: npm test
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,3 @@ node_modules

# MacOS
.DS_Store

# env files
*.env
.env*
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"scripts": {
"init": "mate-scripts init",
"start": "node src/index.js",
"dev": "node --watch src/index.js",
"lint": "npm run format && mate-scripts lint",
"format": "prettier --ignore-path .prettierignore --write './src/**/*.{js,ts}'",
"test:only": "mate-scripts test",
Expand All @@ -23,7 +24,7 @@
},
"devDependencies": {
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^2.1.3",
"axios": "^1.7.2",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
Expand Down
79 changes: 79 additions & 0 deletions src/controllers/expenses.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const { usersService } = require('../services/users.service.js');
const { expensesService } = require('../services/expenses.service.js');

const create = async (req, res) => {
const { userId, spentAt, title, amount, category, note } = req.body;
const user = await usersService.getOne(userId);

if (!userId || !user) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/services/index.js is missing export for categoriesService

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/controllers/index.js is missing export for categoriesController

return res.sendStatus(400);
Comment on lines +4 to +8
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/routes/index.js is missing export for categoriesRouter

}

const expense = await expensesService.create({
Comment on lines +9 to +11
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/createServer.js is missing route registration for categoriesRouter

userId: userId,
spentAt,
title,
amount,
category,
note,
});

res.status(201).json(expensesService.normalize(expense));
};

const getAll = async (req, res) => {
const { userId, categories, from, to } = req.query;
const expenses = await expensesService.getAll({
userId: Number(userId),
categories,
from,
to,
});

res.json(expenses.map((expense) => expensesService.normalize(expense)));
};

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

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

res.json(expensesService.normalize(expense));
};

const update = async (req, res) => {
const expense = await expensesService.getOne(Number(req.params.id));

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

const updatedExpense = await expensesService.update({
id: Number(req.params.id),
...req.body,
});

res.json(expensesService.normalize(updatedExpense));
};

const remove = async (req, res) => {
const expensesRemoved = await expensesService.remove(Number(req.params.id));

if (!expensesRemoved) {
return res.sendStatus(404);
}

res.sendStatus(204);
};

const expensesController = {
create,
getAll,
getOne,
update,
remove,
};

exports.expensesController = expensesController;
7 changes: 7 additions & 0 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { usersController } = require('./users.controller');
const { expensesController } = require('./expenses.controller');

module.exports = {
usersController,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The categoriesService is not exported from src/services/index.js. Without exporting it, the controller cannot access the category service methods.

expensesController,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The categoriesRouter is not exported from src/routes/index.js. Only usersRouter and expensesRouter are exported, but categoriesRouter needs to be added to make it available for registration.

};
Comment on lines +5 to +9
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The task requires implementing CRUD operations for categories. This would involve creating a categoriesController, which should be imported and exported from this file.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The categoriesController is not imported or exported from src/controllers/index.js. Without exporting it, other parts of the application cannot access the category controller.

67 changes: 67 additions & 0 deletions src/controllers/users.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const { usersService } = require('../services/users.service.js');

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

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

const user = await usersService.create(name);

res.status(201).json(usersService.normalize(user));
};

const getAll = async (req, res) => {
const users = await usersService.getAll();

res.send(users.map((user) => usersService.normalize(user)));
};

const getOne = async (req, res) => {
const user = await usersService.getOne(Number(req.params.id));

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

return;
}

res.send(usersService.normalize(user));
};

const update = async (req, res) => {
const { id } = req.params;
const user = await usersService.getOne(Number(id));

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

const updatedUser = await usersService.update({
id: Number(id),
name: req.body.name,
});

res.json(usersService.normalize(updatedUser));
};

const remove = async (req, res) => {
const user = await usersService.remove(Number(req.params.id));

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

res.sendStatus(204);
};

const usersController = {
getAll,
getOne,
create,
update,
remove,
};

exports.usersController = usersController;
11 changes: 10 additions & 1 deletion src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
'use strict';

const express = require('express');
const { routes } = require('./routes/index');

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

app.use(express.json());
app.use('/users', routes.usersRouter);
app.use('/expenses', routes.expensesRouter);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new routes for managing categories are missing. Based on the task requirements, you should add a new router here for the /categories endpoint.

Comment on lines +10 to +11
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The categories router is NOT registered in the server. The task requires: 'Ensure the new routes are registered in src/createServer.js'. Currently only /users and /expenses are registered. Need to add: app.use('/categories', routes.categoriesRouter); after line 10.


return app;
};

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

module.exports = {
Expand Down
28 changes: 25 additions & 3 deletions src/models/Expense.model.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
'use strict';

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

const Expense = sequelize.define(
// your code goes here
);
const Expense = sequelize.define('Expense', {
userId: {
type: DataTypes.INTEGER,
allowNull: false,
},
title: {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The categories router is not registered in the server. The task explicitly requires: 'Ensure the new routes are registered in src/createServer.js'. Add: app.use('/categories', routes.categoriesRouter);

type: DataTypes.STRING,
allowNull: false,
},
amount: {
type: DataTypes.INTEGER,
allowNull: false,
},
spentAt: {
type: DataTypes.DATE,
allowNull: false,
},
category: {
type: DataTypes.STRING,
},
note: {
type: DataTypes.STRING,
},
});

module.exports = {
Expense,
Expand Down
10 changes: 7 additions & 3 deletions src/models/User.model.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
'use strict';

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

const User = sequelize.define(
// your code goes here
);
const User = sequelize.define('User', {
Comment on lines 1 to +6
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The categoriesRouter is NOT exported from src/routes/index.js. Currently only usersRouter and expensesRouter are exported. Need to add: const { categoriesRouter } = require('./categories.route'); and add categoriesRouter to the exports object.

name: {
type: DataTypes.STRING,
allowNull: false,
},
});

module.exports = {
User,
Expand Down
6 changes: 2 additions & 4 deletions src/models/models.js → src/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ const { User } = require('./User.model');
const { Expense } = require('./Expense.model');

module.exports = {
models: {
User,
Expense,
},
User,
Expense,
};
14 changes: 14 additions & 0 deletions src/routes/expenses.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { Router } = require('express');
const {
expensesController,
} = require('./../controllers/expenses.controller.js');

const expensesRouter = Router();

expensesRouter.post('/', expensesController.create);
Comment on lines +1 to +6
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The categories router is not imported and exported. Need to add: const { categoriesRouter } = require('./categories.route'); and add categoriesRouter to the exports object.

expensesRouter.get('/', expensesController.getAll);
expensesRouter.get('/:id', expensesController.getOne);
expensesRouter.delete('/:id', expensesController.remove);
expensesRouter.patch('/:id', expensesController.update);

exports.expensesRouter = expensesRouter;
7 changes: 7 additions & 0 deletions src/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { usersRouter } = require('./users.route');
const { expensesRouter } = require('./expenses.route');

exports.routes = {
usersRouter,
expensesRouter,
Comment on lines +1 to +7
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The categories router is not exported from src/routes/index.js. Currently only usersRouter and expensesRouter are exported. Need to add: const { categoriesRouter } = require('./categories.route'); and include it in exports.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to export categoriesRouter in the routes object to make it available for registration in createServer.js.

};
Comment on lines +5 to +9
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the task requirements, you need to implement CRUD for categories. This file should import the categoriesRouter from its corresponding route file and include it in the exported routes object.

12 changes: 12 additions & 0 deletions src/routes/users.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { Router } = require('express');
const { usersController } = require('./../controllers/users.controller');

const usersRouter = Router();

usersRouter.post('/', usersController.create);
Comment on lines +1 to +6
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The categoriesService is NOT exported from src/services/index.js. Currently only usersService and expensesService are exported. Need to add: const { categoriesService } = require('./categories.service'); and include it in the exports.

usersRouter.get('/', usersController.getAll);
usersRouter.get('/:id', usersController.getOne);
usersRouter.patch('/:id', usersController.update);
usersRouter.delete('/:id', usersController.remove);

exports.usersRouter = usersRouter;
Loading
Loading