From 4935c0a50939176bfe3918939f4407236e7007ab Mon Sep 17 00:00:00 2001 From: Pedro_Everton Date: Wed, 21 Aug 2024 15:20:41 -0300 Subject: [PATCH] added api.js First try at building the api --- api.js | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 api.js diff --git a/api.js b/api.js new file mode 100644 index 0000000..91e6cb3 --- /dev/null +++ b/api.js @@ -0,0 +1,90 @@ +const express = require('express'); +const app = express(); +const path = require('path'); +// const home = require('./crudehtml/index.html') + +app.use(express.json()); +app.use(express.static('./crudehtml')); + +app.get('/', (req, res) => { + res.status(200).sendFile(path.resolve(__dirname, './crudehtml/login.html')); +}); + +app.get('/newuser', (req, res) => { + res.status(200).sendFile(path.resolve(__dirname, './crudehtml/cadastro.html')); +}); + +// Get all rooms +app.get('/api/v1/rooms', async (req, res) => { + try{ + const results = await db.query("SELECT * FROM Quarto"); + console.log(results); + res.status(200).json({ + status: "success", + results: results.rows.lenght, + data: { + rooms: results.rows, + } + }); + } + catch(err) { + console.log(err) + } + +}); + +// Get one room +app.get('/api/v1/rooms/:id', (req, res) => { + console.log(req.params); + res.status(200).json({ + status: "success", + data: { + room: "005" + } + }); +}); + +// Create one room +app.post("/api/v1/rooms", async (req, res) => { + try { + + } catch (error) { + console.log(error) + } + console.log(req.body); + res.status(201).json({ + status: "success", + data: { + room: "005" + } + }); +}); + +// Update one room +app.put("/api/v1/rooms/:id", (req, res) => { + console.log(req.params.id); + console.log(req.body); + res.status(200).json({ + status: "success", + data: { + room: "005" + } + }); +}); + +// Delete one room +app.delete("api/v1/rooms/:id", (req, res) => { + res.status(204).json({ + status: "success" + }) +}) + +// Handle all other routes +//app.all('*', (req, res) => { +// res.status(404).sendFile(path.resolve(__dirname, './crudehtml/error.html')); +//}); + +const port = process.env.PORT || 5000; +app.listen(port, () => { + console.log(`Server running on port ${port}`); +}); \ No newline at end of file