Skip to content

Instantly share code, notes, and snippets.

@IvsonEmidio
Last active June 29, 2022 15:17
Show Gist options
  • Save IvsonEmidio/b96f5e32a41dcf2d6f5238f00aa43b12 to your computer and use it in GitHub Desktop.
Save IvsonEmidio/b96f5e32a41dcf2d6f5238f00aa43b12 to your computer and use it in GitHub Desktop.
import { Request, Response } from "express";
import { pool } from "database";
import { validationResult } from "express-validator";
export class UserController {
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 addUser = await pool.query(`INSERT...`);
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