Skip to content

Commit

Permalink
v2024.05.20:
Browse files Browse the repository at this point in the history
commit e8a3c9c
Author: Jonas Simoen <[email protected]>
Date:   Mon May 20 16:54:42 2024 +0200

    feat(news): article management
  • Loading branch information
jonassimoen committed May 20, 2024
1 parent 6dd6d1d commit dc51cdc
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 13 deletions.
37 changes: 37 additions & 0 deletions src/controllers/News.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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)
}
6 changes: 4 additions & 2 deletions src/routers/Main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand All @@ -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'})
}
42 changes: 31 additions & 11 deletions src/routers/News.ts
Original file line number Diff line number Diff line change
@@ -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,
}
});
}
2 changes: 2 additions & 0 deletions src/routers/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export const PublicPageRouter: FastifyPluginAsync = async server => {
}
}
});
}

export const AdminPageRouter: FastifyPluginAsync = async server => {
server.route({
method: 'POST',
url: '',
Expand Down
23 changes: 23 additions & 0 deletions src/types/body-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
},
};

0 comments on commit dc51cdc

Please sign in to comment.