diff --git a/src/controllers/News.ts b/src/controllers/News.ts index 2972434..1c8a733 100644 --- a/src/controllers/News.ts +++ b/src/controllers/News.ts @@ -32,6 +32,7 @@ export const GetArticlesHandler = async (req: any, rep: any) => { count: articleCount, }) } + export const GetArticleHandler = async (req: any, rep: any) => { const articles = await prisma.article.findFirst({ select: { @@ -54,4 +55,40 @@ export const GetArticleHandler = async (req: any, rep: any) => { } }); rep.send(articles) +} + +export const PutArticleHandler = async (req: any, rep: any) => { + const articles = await prisma.article.update({ + where: { + id: +req.params.id + }, + data: { + slug: req.body.slug, + title: req.body.title, + description: req.body.description, + imageUrl: req.body.imageUrl, + readMore: req.body.readMore, + timestampUpdated: new Date(), + } + }); + rep.send(articles) +} + +export const PostArticleHandler = async (req: any, rep: any) => { + const articles = await prisma.article.create({ + data: { + slug: req.body.slug, + title: req.body.title, + description: req.body.description, + imageUrl: req.body.imageUrl, + readMore: req.body.readMore, + timestampCreated: new Date(), + author: { + connect: { + id: +req.user.id || 0 + } + }, + } + }); + rep.send(articles) } \ No newline at end of file diff --git a/src/routers/Main.ts b/src/routers/Main.ts index 7e35b59..6395f7f 100644 --- a/src/routers/Main.ts +++ b/src/routers/Main.ts @@ -8,10 +8,10 @@ import { AdminMatchRouter, PublicMatchRouter } from './Match'; import { AdminMatchEventRouter, PublicMatchEventRouter } from './MatchEvent'; import { AdminMatchStatisticRouter, PublicMatchStatisticRouter, PublicPlayerStatisticRouter } from './Statistic'; import { AdminWeekRouter, PublicWeekRouter } from './Week'; -import { PublicPageRouter } from './Page'; +import { AdminPageRouter, PublicPageRouter } from './Page'; import { AdminGeneralRouter } from './Admin'; import { NotificationRouter } from './Notification'; -import { PublicNewsRouter } from './News'; +import { AdminNewsRouter, PublicNewsRouter } from './News'; export const PublicRouter: FastifyPluginAsync = async server => { server.register(NotificationRouter, { prefix: '/notifications' }) @@ -37,4 +37,6 @@ export const AdminRouter: FastifyPluginAsync = async server => { server.register(AdminMatchEventRouter, { prefix: '/matches/:matchId/events' }) server.register(AdminMatchStatisticRouter, { prefix: '/matches/:matchId/stats' }) server.register(AdminWeekRouter, { prefix: '/weeks' }) + server.register(AdminPageRouter, { prefix: '/pages' }) + server.register(AdminNewsRouter, {prefix: '/news'}) } \ No newline at end of file diff --git a/src/routers/News.ts b/src/routers/News.ts index 872d622..fecdbfa 100644 --- a/src/routers/News.ts +++ b/src/routers/News.ts @@ -1,21 +1,41 @@ import { FastifyPluginAsync } from "fastify"; -import { GetArticleHandler, GetArticlesHandler } from "../controllers/News"; +import { GetArticleHandler, GetArticlesHandler, PostArticleHandler, PutArticleHandler } from "../controllers/News"; +import { PostArticleSchema, PutArticleSchema } from "../types/body-schema"; export const PublicNewsRouter: FastifyPluginAsync = async server => { server.route({ - method: 'GET', - url: '', - handler: GetArticlesHandler, - schema: { - querystring: { - page: { type: 'number' } - } + method: 'GET', + url: '', + handler: GetArticlesHandler, + schema: { + querystring: { + page: { type: 'number' } } + } }); server.route({ - method: 'GET', - url: '/:slug', - handler: GetArticleHandler + method: 'GET', + url: '/:slug', + handler: GetArticleHandler + }); +} + +export const AdminNewsRouter: FastifyPluginAsync = async server => { + server.route({ + method: 'POST', + url: '', + handler: PostArticleHandler, + schema: { + body: PostArticleSchema, + } + }); + server.route({ + method: 'PUT', + url: '/:id', + handler: PutArticleHandler, + schema: { + body: PutArticleSchema, + } }); } \ No newline at end of file diff --git a/src/routers/Page.ts b/src/routers/Page.ts index a584d04..63945a8 100644 --- a/src/routers/Page.ts +++ b/src/routers/Page.ts @@ -14,7 +14,9 @@ export const PublicPageRouter: FastifyPluginAsync = async server => { } } }); +} +export const AdminPageRouter: FastifyPluginAsync = async server => { server.route({ method: 'POST', url: '', diff --git a/src/types/body-schema.ts b/src/types/body-schema.ts index f65e47c..0a80054 100644 --- a/src/types/body-schema.ts +++ b/src/types/body-schema.ts @@ -287,3 +287,26 @@ export const PutPageSchema = { }, }, }; + +export const PutArticleSchema = { + type: "object", + properties: { + id: { type: "number" }, + slug: { type: "string" }, + title: { type: "string" }, + description: { type: "string" }, + imageUrl: { type: "string" }, + readMore: { type: "boolean" }, + }, +}; + +export const PostArticleSchema = { + type: "object", + properties: { + slug: { type: "string" }, + title: { type: "string" }, + description: { type: "string" }, + imageUrl: { type: "string" }, + readMore: { type: "boolean" }, + }, +};