Skip to content

Commit e326550

Browse files
DafloresdiazDaniel Flores Diaz
andauthored
Feat: Create CRUD endpoints (#3)
Co-authored-by: Daniel Flores Diaz <[email protected]>
1 parent f86930f commit e326550

File tree

18 files changed

+12244
-689
lines changed

18 files changed

+12244
-689
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules/
22
coverage/
33
dist/
4+
.vscode/

app/internal/adapters/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { MainRoute } from './routes';
1+
import { MainRoute, UsersRoute} from './routes';
22

33
export const Adapters = {
4-
Routes: { MainRoute },
4+
Routes: { MainRoute, UsersRoute},
55
};

app/internal/adapters/routes/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './main.routes';
2+
export * from './users.routes';
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import express from "express";
2+
import { UsersController } from "../../core/controllers";
3+
4+
/**
5+
* Create a new instance of UserRoutes.
6+
* @param {express.Application} app - The Express application instance.
7+
*/
8+
export const UsersRoute = (app) =>{
9+
const router = express.Router();
10+
app.use('/users', router);
11+
12+
//List by id
13+
router.get('/:id', UsersController.getById);
14+
// Create a new user
15+
router.post('/', UsersController.create)
16+
//Edit User
17+
router.put('/:id', UsersController.update);
18+
//Delete user
19+
router.delete('/:id', UsersController.delete);
20+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './main.controller';
2+
export * from './users.controller';
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { UserServices } from '../services';
2+
import { constants } from 'http2';
3+
import { Domain } from '../domain';
4+
5+
const {
6+
Models: { Responses },
7+
} = Domain;
8+
9+
/**
10+
* Callback function for performing a CRUD.
11+
* @param {import('express').Request} req - Express request object.
12+
* @param {import('express').Response} res - Express response object.
13+
* @param {import('express').NextFunction} next - Express next middleware function.
14+
* @function getByID - Obtain the information of a user using an ID.
15+
* @function create - Create a new user.
16+
* @function update - RRefresh the information of a user using his ID.
17+
* @function delete - Delete the information of a user using his ID.
18+
*/
19+
export const UsersController = {
20+
getById: (req, res, next) => {
21+
try {
22+
const message = UserServices.getById();
23+
res.status(constants.HTTP_STATUS_OK).json(message);
24+
} catch (err) {
25+
next(err);
26+
}
27+
},
28+
create: async (req, res, next) => {
29+
{
30+
try {
31+
const message = UserServices.create();
32+
res.status(constants.HTTP_STATUS_OK).json(message);
33+
} catch (err) {
34+
next(err);
35+
}
36+
}
37+
},
38+
update: async (req, res, next) => {
39+
try {
40+
const message = UserServices.update();
41+
res.status(constants.HTTP_STATUS_OK).json(message);
42+
} catch (err) {
43+
next(err);
44+
}
45+
},
46+
delete: async (req, res, next) => {
47+
try {
48+
const message = UserServices.delete();
49+
res.status(constants.HTTP_STATUS_OK).json(message);
50+
} catch (err) {
51+
next(err);
52+
}
53+
},
54+
}

app/internal/core/models/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './users';

app/internal/core/models/users.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {model, Schema} from 'mongoose';
2+
/**
3+
* Create User Schema is the schema for the request body.
4+
* @const {mongoose.Schema} createUserSchema
5+
*/
6+
const createUserSchema = new Schema(
7+
{
8+
Id: {
9+
type: Number,
10+
required: true,
11+
},
12+
email: {
13+
type: String,
14+
required: true,
15+
},
16+
first_name: {
17+
type: String,
18+
required: true,
19+
},
20+
last_name: {
21+
type: String,
22+
required: true,
23+
},
24+
company: String,
25+
url: String,
26+
text: String,
27+
},
28+
{ timestamps: true, versionKey: false }
29+
)
30+
31+
export const UserModel = model('User', createUserSchema)

app/internal/core/services/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './main.services';
2+
export * from './users.services'
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {UserModel} from '../models';
2+
import { Domain } from '../domain';
3+
4+
const {
5+
Models: { Responses },
6+
} = Domain;
7+
8+
/**
9+
* Get by ID response.
10+
* @returns {import('../domain/models/responses.models.js').SimpleResponse} The getByID response.
11+
* Create response.
12+
* @returns {import('../domain/models/responses.models.js').SimpleResponse} The create response.
13+
* Update response.
14+
* @returns {import('../domain/models/responses.models.js').SimpleResponse} The update response.
15+
* Delete response.
16+
* @returns {import('../domain/models/responses.models.js').SimpleResponse} The delete response.
17+
*/
18+
export const UserServices = {
19+
getById: () =>{
20+
return Responses.simple('This is a response from a get request');
21+
},
22+
create: () =>{
23+
return Responses.simple('This is a response from a post request');
24+
},
25+
update: () =>{
26+
return Responses.simple('This is a response from a update request');
27+
},
28+
delete: () =>{
29+
return Responses.simple('This is a response from a delete request');
30+
},
31+
}

0 commit comments

Comments
 (0)