From 98e32ece27b3b2296e502ffa3c43a472e7ddd68d Mon Sep 17 00:00:00 2001 From: SySagar Date: Mon, 13 Nov 2023 23:23:52 +0530 Subject: [PATCH] chore:testing --- .../migrations/20231113164424_/migration.sql | 12 ++++++++ prisma/schema.prisma | 11 +++++++ src/app/blog/controller.js | 30 +++++++++++++++++++ src/app/blog/index.js | 1 + src/app/blog/repository.js | 19 ++++++++++++ src/app/blog/routes.js | 10 +++++++ src/app/index.js | 4 ++- 7 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 prisma/migrations/20231113164424_/migration.sql create mode 100644 src/app/blog/controller.js create mode 100644 src/app/blog/index.js create mode 100644 src/app/blog/repository.js create mode 100644 src/app/blog/routes.js diff --git a/prisma/migrations/20231113164424_/migration.sql b/prisma/migrations/20231113164424_/migration.sql new file mode 100644 index 0000000..6be9a1e --- /dev/null +++ b/prisma/migrations/20231113164424_/migration.sql @@ -0,0 +1,12 @@ +-- CreateTable +CREATE TABLE "Blog" ( + "id" STRING NOT NULL, + "title" STRING NOT NULL, + "tags" STRING[], + "content" STRING NOT NULL, + "date" STRING NOT NULL, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "Blog_pkey" PRIMARY KEY ("id") +); diff --git a/prisma/schema.prisma b/prisma/schema.prisma index abe1998..cf9f894 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -88,3 +88,14 @@ model Induction { preferred_primary_domain String preferred_secondary_domain String } + + +model Blog{ + id String @id @default(cuid()) + title String + tags String[] + content String + date String + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt +} \ No newline at end of file diff --git a/src/app/blog/controller.js b/src/app/blog/controller.js new file mode 100644 index 0000000..4cf2d8e --- /dev/null +++ b/src/app/blog/controller.js @@ -0,0 +1,30 @@ +import {saveToDatabase} from './repository.js' + +export const saveBlog = async (req, res) => { + const { title, content, tags,date } = req.body; + const blog = { + title, + tags, + content, + date + }; + const result = await saveToDatabase(blog); + if(result) { + return res.send({ + status: 201, + message: "blog saved successfully" + }); + } + else + { + return res.send({ + status: 500, + message: "blog could not be saved" + }); + } +} + +// const getBlogs = async (req, res) => { +// const blogs = await Blog.findAll(); +// return res.json(blogs); +// } \ No newline at end of file diff --git a/src/app/blog/index.js b/src/app/blog/index.js new file mode 100644 index 0000000..49a7ede --- /dev/null +++ b/src/app/blog/index.js @@ -0,0 +1 @@ +export { default as blogRouter } from './routes.js' \ No newline at end of file diff --git a/src/app/blog/repository.js b/src/app/blog/repository.js new file mode 100644 index 0000000..3d05de1 --- /dev/null +++ b/src/app/blog/repository.js @@ -0,0 +1,19 @@ +import db from "../../db/client.js" + +export const saveToDatabase = (blog) => { + // Save to database + return db.Blog.create({ + data:blog + }); + +} + +// export const getAllBlogs = async () => { +// try { +// const blogs = await db.Blog.findAll(); +// return blogs; +// } catch (error) { +// console.log(error); +// return []; +// } +// } \ No newline at end of file diff --git a/src/app/blog/routes.js b/src/app/blog/routes.js new file mode 100644 index 0000000..6c9679a --- /dev/null +++ b/src/app/blog/routes.js @@ -0,0 +1,10 @@ +import { Router } from "express"; +import { saveBlog } from "./controller.js"; + + +const router = Router() + +router.post('/create', saveBlog) +// router.get('/', getAllInductionController) + +export default router; \ No newline at end of file diff --git a/src/app/index.js b/src/app/index.js index 8edb811..5bbeb10 100644 --- a/src/app/index.js +++ b/src/app/index.js @@ -4,7 +4,8 @@ import { eventRouter } from "./event/index.js"; import { mediaRouter } from "./media/index.js"; import { inductionRouter } from "./induction/index.js"; import { githubRouter } from "./github/index.js"; -import {classicRouter} from"./classic/index.js"; +import { classicRouter } from"./classic/index.js"; +import { blogRouter } from "./blog/index.js"; const appRouter = Router() @@ -14,5 +15,6 @@ appRouter.use('/event', eventRouter) appRouter.use('/media', mediaRouter) appRouter.use('/induction', inductionRouter) appRouter.use('/github', githubRouter) +appRouter.use('/blog', blogRouter) export default appRouter