Skip to content

Commit

Permalink
Merge pull request #83 from thectogeneral/feat/68-blog-post-listing
Browse files Browse the repository at this point in the history
Feat/68 blog post listing
  • Loading branch information
Idimmusix authored Jul 22, 2024
2 parents 2f57ba0 + efe9beb commit bf4e756
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 10 deletions.
34 changes: 34 additions & 0 deletions src/controllers/BlogController.ts
Original file line number Diff line number Diff line change
@@ -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 };
1 change: 1 addition & 0 deletions src/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// silence is golden
export * from "./AuthController";
export * from "./UserController";
export * from "./BlogController"
export * from "./HelpController";
export * from "./NotificationController";
10 changes: 2 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@ import express, { Express, Request, Response } from "express";
import config from "./config";
import dotenv from "dotenv";
import cors from "cors";
import {
userRouter,
authRoute,
helpRouter,
testimonialRoute,
notificationRouter,
smsRouter,
} from "./routes";
import { userRouter, authRoute, testimonialRoute, notificationRouter, smsRouter, blogRoute } from "./routes";
import { routeNotFound, errorHandler } from "./middleware";
import { orgRouter } from "./routes/organisation";
import swaggerUi from "swagger-ui-express";
Expand Down Expand Up @@ -47,6 +40,7 @@ server.use("/api/v1/auth", authRoute);
server.use("/api/v1/help-center", helpRouter);
server.use("/api/v1/sms", smsRouter);
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);
Expand Down
2 changes: 2 additions & 0 deletions src/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from "./error";
export * from "./auth";
export * from "./methodNotAllowed";

8 changes: 8 additions & 0 deletions src/middleware/methodNotAllowed.ts
Original file line number Diff line number Diff line change
@@ -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 };
12 changes: 12 additions & 0 deletions src/routes/blog.ts
Original file line number Diff line number Diff line change
@@ -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 };
4 changes: 3 additions & 1 deletion src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ export * from "./auth";
export * from "./user";
export * from "./help-center";
export * from "./testimonial";
export * from "./notificationsettings"
export * from "./blog";
export * from "./sms";
export * from "./notificationsettings";

22 changes: 22 additions & 0 deletions src/services/blog.services.ts
Original file line number Diff line number Diff line change
@@ -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 };
}

}
3 changes: 2 additions & 1 deletion src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./auth.services";
export * from "./user.services";
export * from "./help.services";
export * from "./blog.services";
export * from "./help.services";
11 changes: 11 additions & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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<PaginatedBlogResponse>;
}

export interface IUserService {
getUserById(id: string): Promise<User | null>;
getAllUsers(): Promise<User[]>;
Expand Down

0 comments on commit bf4e756

Please sign in to comment.