Skip to content

Instantly share code, notes, and snippets.

@IvsonEmidio
Created June 29, 2022 03:29
Show Gist options
  • Save IvsonEmidio/022bf3ef674a83f94b604373d233ab85 to your computer and use it in GitHub Desktop.
Save IvsonEmidio/022bf3ef674a83f94b604373d233ab85 to your computer and use it in GitHub Desktop.
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