new routers article overhaul complete CRUD
This commit is contained in:
+9
-55
@@ -1,69 +1,23 @@
|
|||||||
import 'dotenv/config';
|
import 'dotenv/config';
|
||||||
import { drizzle } from 'drizzle-orm/node-postgres';
|
import express, { Response } from 'express';
|
||||||
import { eq } from 'drizzle-orm';
|
import { articlesRouter } from './routes/articles.ts';
|
||||||
import express, { Request, Response } from "express";
|
import { blocksRouter } from './routes/blocks.ts';
|
||||||
import { articles } from './db/schema.ts';
|
import { imagesRouter } from './routes/images.ts';
|
||||||
|
|
||||||
// Create a connection to the database
|
|
||||||
const db = drizzle(process.env.DATABASE_URL!);
|
|
||||||
|
|
||||||
// Create a new express application instance
|
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
// Set the network port
|
app.get("/status", (_, res: Response) => {
|
||||||
const port = process.env.PORT || 3000;
|
|
||||||
|
|
||||||
// Define the root path with a greeting message
|
|
||||||
app.get("/status", (req: Request, res: Response) => {
|
|
||||||
res.json({ message: "Backend Operational" });
|
res.json({ message: "Backend Operational" });
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/getAllArticles", async (req: Request, res: Response) => {
|
app.use("/articles", articlesRouter);
|
||||||
const resp = await db.select().from(articles);
|
app.use("/blocks", blocksRouter);
|
||||||
res.json({ resp });
|
app.use("/images", imagesRouter);
|
||||||
});
|
|
||||||
|
|
||||||
app.get("/getArticle/:articleId", async (req: Request, res: Response) => {
|
|
||||||
const { articleId } = req.params;
|
|
||||||
|
|
||||||
const resp = await db.select()
|
const port = process.env.PORT || 3000;
|
||||||
.from(articles)
|
|
||||||
.where(eq(articles.id, articleId));
|
|
||||||
res.json({ resp });
|
|
||||||
});
|
|
||||||
|
|
||||||
app.post("/postArticle", async (req: Request, res: Response) => {
|
|
||||||
const resp = await db
|
|
||||||
.insert(articles)
|
|
||||||
.values({
|
|
||||||
...req.body,
|
|
||||||
date: new Date(req.body.date)
|
|
||||||
});
|
|
||||||
res.json({ resp });
|
|
||||||
});
|
|
||||||
|
|
||||||
app.put("/putArticle/:articleId", async (req: Request, res: Response) => {
|
|
||||||
const { articleId } = req.params;
|
|
||||||
const resp = await db.update(articles)
|
|
||||||
.set({
|
|
||||||
name: req.body.name,
|
|
||||||
date: new Date(req.body.date),
|
|
||||||
text: req.body.text
|
|
||||||
})
|
|
||||||
.where(eq(articles.id, articleId));
|
|
||||||
res.json({ resp });
|
|
||||||
});
|
|
||||||
|
|
||||||
app.delete("/deleteArticle/:articleId", async (req: Request, res: Response) => {
|
|
||||||
const { articleId } = req.params;
|
|
||||||
const resp = await db.delete(articles)
|
|
||||||
.where(eq(articles.id, articleId));
|
|
||||||
|
|
||||||
res.json({ resp });
|
|
||||||
});
|
|
||||||
|
|
||||||
// Start the Express server
|
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
console.log(`The server is running at http://localhost:${port}`);
|
console.log(`The server is running at http://localhost:${port}`);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user