From ada42c3c0ca31fd8a743b088c9e0b7d7b0e9c76a Mon Sep 17 00:00:00 2001 From: thectogeneral Date: Mon, 22 Jul 2024 01:14:12 +0100 Subject: [PATCH] merge pull --- src/controllers/BlogController.ts | 34 ++++++++++++++++++++++++++++++ src/controllers/index.ts | 2 ++ src/index.ts | 3 ++- src/middleware/index.ts | 2 ++ src/middleware/methodNotAllowed.ts | 8 +++++++ src/routes/blog.ts | 12 +++++++++++ src/routes/index.ts | 2 ++ src/services/blog.services.ts | 22 +++++++++++++++++++ src/services/index.ts | 2 ++ src/types/index.d.ts | 11 ++++++++++ 10 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 src/controllers/BlogController.ts create mode 100644 src/middleware/methodNotAllowed.ts create mode 100644 src/routes/blog.ts create mode 100644 src/services/blog.services.ts diff --git a/src/controllers/BlogController.ts b/src/controllers/BlogController.ts new file mode 100644 index 00000000..9af1e146 --- /dev/null +++ b/src/controllers/BlogController.ts @@ -0,0 +1,34 @@ +import { Request, Response, NextFunction } from "express"; +import { BlogService } from "../services/blog.services"; + +const blogService = new BlogService(); + +const getPaginatedBlogs = async (req: Request, res: Response, next: NextFunction) => { + try { + const page = parseInt(req.query.page as string) || 1; + const pageSize = parseInt(req.query.page_size as string) || 10; + + if (page <= 0 || pageSize <= 0) { + return res.status(400).json({ error: "Invalid page or page_size parameter" }); + } + + const { count, next, previous, results } = await blogService.getPaginatedBlogPosts(page, pageSize); + + const summaries = results.map(blog => ({ + id: blog.id, + title: blog.title, + excerpt: blog.content.substring(0, 100), // Example: first 100 characters as excerpt + image_url: blog.imageUrl, + tags: blog.tags, + created_at: blog.createdAt, + })); + + res.status(200).json({ count, next, previous, results: summaries }); + } catch (error) { + next(error); + } +}; + + + +export { getPaginatedBlogs }; diff --git a/src/controllers/index.ts b/src/controllers/index.ts index 3891a868..28cfeb41 100644 --- a/src/controllers/index.ts +++ b/src/controllers/index.ts @@ -2,3 +2,5 @@ export * from "./AuthController"; export * from "./UserController"; export * from "./NotificationController" +export * from "./BlogController" + diff --git a/src/index.ts b/src/index.ts index 913ff95d..da0f90fe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,7 @@ import express, { Express, Request, Response } from "express"; import config from "./config"; import dotenv from "dotenv"; import cors from "cors"; -import { userRouter, authRoute, testimonialRoute, notificationRouter } from "./routes"; +import { userRouter, authRoute, testimonialRoute, notificationRouter, blogRoute } from "./routes"; import { routeNotFound, errorHandler } from "./middleware"; import { seed } from "./seeder"; @@ -40,6 +40,7 @@ server.get("/", (req: Request, res: Response) => { server.use("/api/v1", userRouter, orgRouter); server.use("/api/v1/auth", authRoute); server.use("/api/v1", testimonialRoute); +server.use("/api/v1/blogs", blogRoute); server.use("/api/v1/docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec)); server.use(routeNotFound); server.use(errorHandler); diff --git a/src/middleware/index.ts b/src/middleware/index.ts index d0d8e7b7..b204764a 100644 --- a/src/middleware/index.ts +++ b/src/middleware/index.ts @@ -1,2 +1,4 @@ export * from "./error"; export * from "./auth"; +export * from "./methodNotAllowed"; + diff --git a/src/middleware/methodNotAllowed.ts b/src/middleware/methodNotAllowed.ts new file mode 100644 index 00000000..dec686cc --- /dev/null +++ b/src/middleware/methodNotAllowed.ts @@ -0,0 +1,8 @@ +// src/middleware/methodNotAllowed.ts +import { Request, Response, NextFunction } from 'express'; + +const methodNotAllowed = (req: Request, res: Response, next: NextFunction) => { + res.status(405).json({ error: 'This method is not allowed.' }); +}; + +export { methodNotAllowed }; diff --git a/src/routes/blog.ts b/src/routes/blog.ts new file mode 100644 index 00000000..85bcf9f0 --- /dev/null +++ b/src/routes/blog.ts @@ -0,0 +1,12 @@ +// src/routes/blogRoute.ts +import { Router } from "express"; +import { getPaginatedBlogs } from "../controllers/BlogController"; +import { methodNotAllowed } from "../middleware"; + +const blogRoute = Router(); + +blogRoute.post("/"); +blogRoute.get("/", getPaginatedBlogs); +blogRoute.all("/", methodNotAllowed); + +export { blogRoute }; diff --git a/src/routes/index.ts b/src/routes/index.ts index a665db2c..8f3b6322 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -3,3 +3,5 @@ export * from "./auth"; export * from "./user"; export * from "./testimonial"; export * from "./notificationsettings" +export * from "./blog"; + diff --git a/src/services/blog.services.ts b/src/services/blog.services.ts new file mode 100644 index 00000000..12e75a9c --- /dev/null +++ b/src/services/blog.services.ts @@ -0,0 +1,22 @@ +import { Blog } from "../models"; +import { IBlogService } from "../types"; +import AppDataSource from '../data-source'; + +export class BlogService implements IBlogService { + private blogRepository = AppDataSource.getRepository(Blog); + + async getPaginatedBlogPosts(page: number = 1, pageSize: number = 10): Promise<{ count: number, next: string | null, previous: string | null, results: BlogPost[] }> { + const [results, count] = await this.blogRepository.findAndCount({ + relations: ['author'], + skip: (page - 1) * pageSize, + take: pageSize, + order: { createdAt: "DESC" } + }); + + const next = page * pageSize < count ? `/api/v1/blogs?page=${page + 1}&page_size=${pageSize}` : null; + const previous = page > 1 ? `/api/v1/blogs?page=${page - 1}&page_size=${pageSize}` : null; + + return { count, next, previous, results }; + } + +} diff --git a/src/services/index.ts b/src/services/index.ts index 1578dc12..c318e2c1 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -1,2 +1,4 @@ export * from "./auth.services"; export * from "./user.services"; +export * from "./blog.services"; + diff --git a/src/types/index.d.ts b/src/types/index.d.ts index 67d1bb76..76185596 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -1,5 +1,16 @@ import { User } from "../models"; +export interface PaginatedBlogResponse { + count: number; + next: string | null; + previous: string | null; + results: Blog[]; +} + +export interface IBlogService { + getPaginatedBlogPosts(page: number, pageSize: number): Promise; +} + export interface IUserService { getUserById(id: string): Promise; getAllUsers(): Promise;