-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: sylvain-pierrot <[email protected]>
- Loading branch information
1 parent
3cf4065
commit 7d572d7
Showing
7 changed files
with
971 additions
and
934 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,37 @@ | ||
import { Request, Response } from "express"; | ||
import pool from "../config/database" | ||
import pool from "../config/database"; | ||
import { log } from "console"; | ||
import { validationResult } from "express-validator"; | ||
|
||
class AlcoholController { | ||
constructor() { } | ||
constructor() {} | ||
|
||
async getRandomAlcohol(req: Request, res: Response) { | ||
const result = await pool.query('SELECT name, image_url FROM alcohols ORDER BY RANDOM() LIMIT 1;') | ||
const result = await pool.query( | ||
"SELECT name, image_url FROM alcohols ORDER BY RANDOM() LIMIT 1;" | ||
); | ||
const alcohol = result.rows[0]; | ||
return res.status(200).send(alcohol); | ||
} | ||
|
||
async postRandomAlcohol(req: Request, res: Response) { | ||
// validation | ||
const errors = validationResult(req); | ||
if (!errors.isEmpty()) { | ||
return res.status(400).json({ errors: errors.array() }); | ||
} | ||
|
||
const insert = | ||
"INSERT INTO alcohols (name, image_url) VALUES($1, $2) ON CONFLICT (name) DO NOTHING;"; | ||
const values = [req.body["name"], req.body["image_url"]]; | ||
|
||
try { | ||
await pool.query(insert, values); | ||
res.status(201).send("Successfully inserted"); | ||
} catch (e) { | ||
res.status(500).send("Insert failed"); | ||
} | ||
} | ||
} | ||
|
||
export = new AlcoholController(); |
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
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
Oops, something went wrong.