Last active
June 29, 2022 15:17
-
-
Save IvsonEmidio/b96f5e32a41dcf2d6f5238f00aa43b12 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 { 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