-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from thectogeneral/feat/68-blog-post-listing
Feat/68 blog post listing
- Loading branch information
Showing
10 changed files
with
97 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from "./error"; | ||
export * from "./auth"; | ||
export * from "./methodNotAllowed"; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters