Created
June 29, 2022 03:29
-
-
Save IvsonEmidio/022bf3ef674a83f94b604373d233ab85 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Request, Response } from "express"; | |
import { validationResult } from "express-validator"; | |
import { UserService } from "../services/UserService"; | |
export class UserController { | |
service: UserService; | |
constructor(){ | |
this.service = new UserService(); | |
} | |
public async create(req: Request, res: Response) { | |
try { | |
const errors = validationResult(req); | |
if (!errors.isEmpty()) { | |
return res.status(400).json({ | |
message: "Check the fields and try again...", | |
errors: errors.array(), | |
}); | |
} | |
const data = { | |
name: req.body.name, | |
email: req.body.email, | |
password: req.body.password | |
} | |
const addUser = await this.service.create(data) | |
if (addUser) { | |
return res.status(201).json({ message: "User created successfully" }); | |
} else { | |
return res.status(500).json({ message: "Internal server error" }); | |
} | |
} catch (err) { | |
return res.status(500).json({ | |
errors: { | |
message: "An unknown error has occurred, please, try again later", | |
}, | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment