Skip to content
Snippets Groups Projects
Commit 2a3a1dd3 authored by Bodor Máté's avatar Bodor Máté
Browse files

Create user response

parent fa68f38d
No related branches found
No related tags found
2 merge requests!10Feature/12 dev auto deploy,!8[Review]Feature/user
...@@ -22,7 +22,7 @@ const addUser = () => (req: Request, res: Response, next: NextFunction) => { ...@@ -22,7 +22,7 @@ const addUser = () => (req: Request, res: Response, next: NextFunction) => {
res.status(201); res.status(201);
res.data = {profile}; res.data = {profile};
} }
res.json(profile); next()
}); });
} }
......
...@@ -5,11 +5,10 @@ const deletUser = () => (req: Request, res: Response, next: NextFunction) => { ...@@ -5,11 +5,10 @@ const deletUser = () => (req: Request, res: Response, next: NextFunction) => {
Profile.findByIdAndDelete(req.params.id, (error) => { Profile.findByIdAndDelete(req.params.id, (error) => {
if (error) { if (error) {
res.status(400); res.status(400);
} else { } else {
res.status(204); res.status(204);
} }
res.json({}); next();
}); });
} }
......
...@@ -3,14 +3,14 @@ import Profile from '../../models/ProfileSchema'; ...@@ -3,14 +3,14 @@ import Profile from '../../models/ProfileSchema';
const getUser = () => (req: Request, res: Response, next: NextFunction) => { const getUser = () => (req: Request, res: Response, next: NextFunction) => {
Profile.findById(req.params.id, (error, profile) => { Profile.findById(req.params.id, (error, profile) => {
if (!error) { if (error) {
res.status(200);
res.data = {profile};
} else {
console.warn(error); console.warn(error);
res.status(400); res.status(400);
} else {
res.status(200);
res.data = { profile };
} }
res.json(profile); next();
}); });
} }
......
...@@ -10,7 +10,7 @@ const getUsersList = () => (req: Request, res: Response, next: NextFunction) =>{ ...@@ -10,7 +10,7 @@ const getUsersList = () => (req: Request, res: Response, next: NextFunction) =>{
res.status(200); res.status(200);
res.data = {profiles} res.data = {profiles}
} }
res.json(profiles); next();
}) })
} }
......
import { Request, Response, NextFunction, response } from 'express';
const responseUser = () => (req: Request, res: Response) => {
res.json(res.data.profile);
}
export default responseUser;
\ No newline at end of file
import { Request, Response, NextFunction, response } from 'express';
const responseUserList = () => (req: Request, res: Response) => {
res.json(res.data.profiles);
}
export default responseUserList;
\ No newline at end of file
...@@ -6,26 +6,22 @@ const updateUser = () => (req: Request, res: Response, next: NextFunction) => { ...@@ -6,26 +6,22 @@ const updateUser = () => (req: Request, res: Response, next: NextFunction) => {
Profile.findOne( Profile.findOne(
{_id : req.params.id}, {_id : req.params.id},
(error, profile) => { (error, profile) => {
res.status(400); res.status(200);
if (error) { if (error) {
res.status(400); res.status(400);
} }
else { else {
if(profile){ if(profile){
validFields.forEach(field => { validFields.forEach(field => {
const value = req.body[field] const value = req.body[field]
if(value) if(value)
profile.set(field, value); profile.set(field, value);
}); });
res.status(200);
profile.save(); profile.save();
res.data = {profile}; res.data = {profile};
} }
} }
res.json(profile); next();
} }
); );
} }
......
import { Request, Response } from 'express';
const emptyResponse = () => (req: Request, res: Response) => res.json({});
\ No newline at end of file
...@@ -10,14 +10,18 @@ import updateUser from "../middlewares/user/updateUser"; ...@@ -10,14 +10,18 @@ import updateUser from "../middlewares/user/updateUser";
import updateWarning from "../middlewares/user/updateWarning"; import updateWarning from "../middlewares/user/updateWarning";
import addUser from '../middlewares/user/addUser'; import addUser from '../middlewares/user/addUser';
import authenticated from '../middlewares/auth/authenticated'; import authenticated from '../middlewares/auth/authenticated';
import emptyResponse from 'src/middlewares/utils/emptyResponse';
import responseUser from 'src/middlewares/user/responseUser';
import responseUserList from 'src/middlewares/user/responseUserList';
const usersRoute = (app: Application): void => { const usersRoute = (app: Application): void => {
app.get('/users', authenticated(), getUsersList() ); app.get('/users', authenticated(), getUsersList(), responseUserList());
app.post('/users', authenticated(), addUser() ); app.post('/users', authenticated(), addUser(), responseUser());
app.get('/users/:id', getUser() ); app.get('/users/:id', getUser(), responseUser() );
app.get('/users/:id/warnings', getWarningsList() ); app.get('/users/:id/warnings', getWarningsList() );
...@@ -25,11 +29,11 @@ import authenticated from '../middlewares/auth/authenticated'; ...@@ -25,11 +29,11 @@ import authenticated from '../middlewares/auth/authenticated';
app.post('/users/:id/warnings', addWarning()); app.post('/users/:id/warnings', addWarning());
app.put('/users/:id', updateUser()); app.put('/users/:id', updateUser(), responseUser() );
app.put('/users/:userId/warnings/:warningId', updateWarning()); app.put('/users/:userId/warnings/:warningId', updateWarning());
app.delete('/users/:id', deleteUser()); app.delete('/users/:id', deleteUser(), emptyResponse());
app.delete('/users/:userId/warnings/:warningId', deleteWarning()); app.delete('/users/:userId/warnings/:warningId', deleteWarning());
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment