Skip to content

Commit

Permalink
feat(api): add route PUT
Browse files Browse the repository at this point in the history
Signed-off-by: sylvain-pierrot <[email protected]>
  • Loading branch information
sylvain-pierrot committed Jun 12, 2024
1 parent 3cf4065 commit 7d572d7
Show file tree
Hide file tree
Showing 7 changed files with 971 additions and 934 deletions.
2 changes: 1 addition & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"express-validator": "^7.0.1",
"express-validator": "^7.1.0",
"pg": "^8.11.1"
}
}
29 changes: 26 additions & 3 deletions api/src/controllers/alcohol.controller.ts
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();
8 changes: 8 additions & 0 deletions api/src/routes/alcohol.routes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import express, { Router } from "express";
import alcoholController from "../controllers/alcohol.controller";
import { body } from "express-validator";

const router: Router = express.Router();

Expand All @@ -8,4 +9,11 @@ router.get(
alcoholController.getRandomAlcohol.bind(alcoholController)
);

router.post(
"/alcohol",
body("name").isString().bail().notEmpty().bail().exists(),
body("image_url").isURL().bail().notEmpty().bail().exists(),
alcoholController.postRandomAlcohol.bind(alcoholController)
);

export default router;
9 changes: 4 additions & 5 deletions api/src/routes/index.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ const router: Router = express.Router();
router.get("/", (req: Request, res: Response) => {
res.status(200).send({
success: "true",
message: "API REST Node.js + MongoDB",
message: "API REST Node.js + PostgreSQL",
version: "1.0.0",
});
});

router.get("/healthz", (req: Request, res: Response) => {
const data = {
uptime: process.uptime(),
message: 'Ok',
date: new Date()
}
message: "Ok",
date: new Date(),
};

res.status(200).send(data);
});


export default router;
Loading

0 comments on commit 7d572d7

Please sign in to comment.