|
| 1 | +import { Request, Response } from "express"; |
| 2 | +import { ERROR_GENERIC, ERROR_VALIDATION } from "../util/Errors.js"; |
| 3 | +import { FrontendRoutesGroups, rerenderFrontend } from "../util/Frontend.js"; |
| 4 | + |
| 5 | +import { validationResult } from "express-validator"; |
| 6 | +import { selectFields } from "express-validator/src/select-fields.js"; |
| 7 | +import { connect } from "http2"; |
| 8 | +import Core from "../Core.js"; |
| 9 | + |
| 10 | +class BlogController { |
| 11 | + private core: Core; |
| 12 | + |
| 13 | + constructor(core: Core) { |
| 14 | + this.core = core; |
| 15 | + } |
| 16 | + |
| 17 | + public async getBlogPosts(req: Request, res: Response) { |
| 18 | + const errors = validationResult(req); |
| 19 | + if (!errors.isEmpty()) { |
| 20 | + return ERROR_VALIDATION(req, res, errors.array()); |
| 21 | + } |
| 22 | + if (req.query && req.query.page) { |
| 23 | + let page = parseInt(req.query.page as string); |
| 24 | + const blogPosts = await this.core.getPrisma().blog.findMany({ |
| 25 | + skip: page * 10, |
| 26 | + take: 10, |
| 27 | + include: { |
| 28 | + author: { |
| 29 | + select: { |
| 30 | + id: true, |
| 31 | + discordId: true, |
| 32 | + avatar: true, |
| 33 | + username: true, |
| 34 | + minecraft: true, |
| 35 | + ssoId: true, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }); |
| 40 | + let count = await this.core.getPrisma().blog.count(); |
| 41 | + res.send({ pages: Math.ceil(count / 10), data: blogPosts }); |
| 42 | + } else { |
| 43 | + const blogPosts = await this.core.getPrisma().blog.findMany({ |
| 44 | + include: { |
| 45 | + author: { |
| 46 | + select: { |
| 47 | + id: true, |
| 48 | + discordId: true, |
| 49 | + avatar: true, |
| 50 | + username: true, |
| 51 | + minecraft: true, |
| 52 | + ssoId: true, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }); |
| 57 | + res.send(blogPosts); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public async getBlogPost(req: Request, res: Response) { |
| 62 | + const errors = validationResult(req); |
| 63 | + if (!errors.isEmpty()) { |
| 64 | + return ERROR_VALIDATION(req, res, errors.array()); |
| 65 | + } |
| 66 | + const blogPost = await this.core.getPrisma().blog.findUnique({ |
| 67 | + where: req.query.slug |
| 68 | + ? { slug: req.params.id } |
| 69 | + : { |
| 70 | + id: req.params.id, |
| 71 | + }, |
| 72 | + include: { |
| 73 | + author: { |
| 74 | + select: { |
| 75 | + id: true, |
| 76 | + discordId: true, |
| 77 | + avatar: true, |
| 78 | + username: true, |
| 79 | + minecraft: true, |
| 80 | + ssoId: true, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }); |
| 85 | + if (blogPost) { |
| 86 | + res.send(blogPost); |
| 87 | + } else { |
| 88 | + ERROR_GENERIC(req, res, 404, "Blog Post does not exist."); |
| 89 | + } |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + public async createBlogPost(req: Request, res: Response) { |
| 94 | + const errors = validationResult(req); |
| 95 | + if (!errors.isEmpty()) { |
| 96 | + return ERROR_VALIDATION(req, res, errors.array()); |
| 97 | + } |
| 98 | + |
| 99 | + const blogPost = await this.core.getPrisma().blog.create({ |
| 100 | + data: { |
| 101 | + title: req.body.title, |
| 102 | + publishedAt: new Date(), |
| 103 | + public: req.body.public ? req.body.public : true, |
| 104 | + content: req.body.content, |
| 105 | + summary: req.body.summary, |
| 106 | + slug: req.body.slug, |
| 107 | + author: { |
| 108 | + connect: { |
| 109 | + id: req.body.authorId, |
| 110 | + }, |
| 111 | + }, |
| 112 | + thumbnail: { |
| 113 | + connect: { |
| 114 | + id: req.body.thumbnailId, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + include: { |
| 119 | + author: { |
| 120 | + select: { |
| 121 | + id: true, |
| 122 | + discordId: true, |
| 123 | + avatar: true, |
| 124 | + username: true, |
| 125 | + minecraft: true, |
| 126 | + ssoId: true, |
| 127 | + }, |
| 128 | + }, |
| 129 | + thumbnail: { select: {} }, |
| 130 | + }, |
| 131 | + }); |
| 132 | + |
| 133 | + rerenderFrontend(FrontendRoutesGroups.FAQ, { |
| 134 | + slug: blogPost.slug, |
| 135 | + }); |
| 136 | + res.send(blogPost); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +export default BlogController; |
0 commit comments