feat: complete day 15
This commit is contained in:
+100
@@ -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}`));
|
||||||
@@ -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;
|
||||||
@@ -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 };
|
||||||
@@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user