From cbbb0ed4c474f151e16cfcd1f7f1ef3472a824cc Mon Sep 17 00:00:00 2001 From: Ayobami Date: Fri, 18 Jul 2025 21:34:29 +0100 Subject: [PATCH] feat: complete day 15 --- day15/app.js | 100 +++++++++++++++++++++++++++++++++++++++++++++ day15/db.js | 9 ++++ day15/models.js | 15 +++++++ day15/package.json | 19 +++++++++ 4 files changed, 143 insertions(+) create mode 100644 day15/app.js create mode 100644 day15/db.js create mode 100644 day15/models.js create mode 100644 day15/package.json diff --git a/day15/app.js b/day15/app.js new file mode 100644 index 0000000..f9738ed --- /dev/null +++ b/day15/app.js @@ -0,0 +1,100 @@ +require("dotenv").config(); +const express = require("express"); +const bodyParser = require("body-parser"); +const { User, Post, sequelize } = require("./models"); + +const app = express(); +app.use(bodyParser.json()); + +// Helper to get model by resource name +const models = { users: User, posts: Post }; + +// Sync DB +sequelize.sync(); + +// TreeQL-style dynamic GET +app.get("/:resource/:id?/:subresource?", async (req, res) => { + const { resource, id, subresource } = req.params; + const Model = models[resource]; + if (!Model) return res.status(404).json({ error: "Resource not found" }); + + try { + if (id) { + const instance = await Model.findByPk( + id, + subresource ? { include: subresource } : {} + ); + if (!instance) return res.status(404).json({ error: "Not found" }); + if (subresource && instance[subresource]) { + return res.json(instance[subresource]); + } + return res.json(instance); + } else { + const all = await Model.findAll(); + return res.json(all); + } + } catch (e) { + return res.status(500).json({ error: e.message }); + } +}); + +// TreeQL-style POST (create resource or subresource) +app.post("/:resource/:id?/:subresource?", async (req, res) => { + const { resource, id, subresource } = req.params; + const Model = models[resource]; + if (!Model) return res.status(404).json({ error: "Resource not found" }); + + try { + if (id && subresource) { + // e.g. POST /users/1/posts + const parent = await models[resource].findByPk(id); + if (!parent) return res.status(404).json({ error: "Parent not found" }); + const childModel = models[subresource]; + if (!childModel) + return res.status(404).json({ error: "Subresource not found" }); + const child = await childModel.create({ ...req.body, UserId: id }); + return res.status(201).json(child); + } else { + // e.g. POST /users + const instance = await Model.create(req.body); + return res.status(201).json(instance); + } + } catch (e) { + return res.status(500).json({ error: e.message }); + } +}); + +// TreeQL-style PUT (update resource) +app.put("/:resource/:id", async (req, res) => { + const { resource, id } = req.params; + const Model = models[resource]; + if (!Model) return res.status(404).json({ error: "Resource not found" }); + + try { + const instance = await Model.findByPk(id); + if (!instance) return res.status(404).json({ error: "Not found" }); + await instance.update(req.body); + return res.json(instance); + } catch (e) { + return res.status(500).json({ error: e.message }); + } +}); + +// TreeQL-style DELETE (delete resource) +app.delete("/:resource/:id", async (req, res) => { + const { resource, id } = req.params; + const Model = models[resource]; + if (!Model) return res.status(404).json({ error: "Resource not found" }); + + try { + const instance = await Model.findByPk(id); + if (!instance) return res.status(404).json({ error: "Not found" }); + await instance.destroy(); + return res.json({ success: true }); + } catch (e) { + return res.status(500).json({ error: e.message }); + } +}); + +const PORT = process.env.PORT || 3000; +app.listen(PORT, () => console.log(`TreeQL API running on port ${PORT}`)); diff --git a/day15/db.js b/day15/db.js new file mode 100644 index 0000000..52f6d33 --- /dev/null +++ b/day15/db.js @@ -0,0 +1,9 @@ +require("dotenv").config(); +const { Sequelize } = require("sequelize"); + +const sequelize = new Sequelize("day_15", "root", process.env.DB_PASSWORD, { + host: "localhost", + dialect: "mysql", +}); + +module.exports = sequelize; diff --git a/day15/models.js b/day15/models.js new file mode 100644 index 0000000..a5779a0 --- /dev/null +++ b/day15/models.js @@ -0,0 +1,15 @@ +const { DataTypes } = require("sequelize"); +const sequelize = require("./db"); + +const User = sequelize.define("User", { + name: DataTypes.STRING, +}); + +const Post = sequelize.define("Post", { + title: DataTypes.STRING, +}); + +User.hasMany(Post, { as: "posts" }); +Post.belongsTo(User); + +module.exports = { User, Post, sequelize }; diff --git a/day15/package.json b/day15/package.json new file mode 100644 index 0000000..a3c8bc0 --- /dev/null +++ b/day15/package.json @@ -0,0 +1,19 @@ +{ + "name": "day15", + "version": "1.0.0", + "description": "- setup project\r - implement https://www.treeql.org/ manually", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "body-parser": "^2.2.0", + "dotenv": "^17.2.0", + "express": "^5.1.0", + "mysql2": "^3.14.2", + "sequelize": "^6.37.7" + } +}