From 78d07df8443d0f3b3ae3a7bd10a9e5624ec14dbb Mon Sep 17 00:00:00 2001 From: mukimovd <41473651-mukimovd@users.noreply.replit.com> Date: Thu, 1 May 2025 03:40:07 +0000 Subject: [PATCH] Enhance blog posts with author info, comments, likes, and tags Improves blog post API endpoints to fetch author details, comment/like counts, and tags using storage methods. Replit-Commit-Author: Agent Replit-Commit-Session-Id: cbacfb18-842a-4116-a907-18c0105ad8ec Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/39b5c689-6e8a-4d5a-9792-69cc81a56534/809eda1d-d63f-4926-be8e-d300221b59bf.jpg --- server/routes.ts | 95 ++++++++++++++++++++++++++++++++++++++++++++--- server/storage.ts | 4 ++ 2 files changed, 93 insertions(+), 6 deletions(-) diff --git a/server/routes.ts b/server/routes.ts index d5df7e3..9a7fa98 100644 --- a/server/routes.ts +++ b/server/routes.ts @@ -410,16 +410,99 @@ export async function registerRoutes(app: Express): Promise { app.get("/api/blog", async (req: Request, res: Response) => { const limit = req.query.limit ? Number(req.query.limit) : undefined; const offset = req.query.offset ? Number(req.query.offset) : undefined; - const posts = await storage.getBlogPosts(limit, offset); - res.json(posts); + + try { + // Get basic blog posts + const posts = await storage.getBlogPosts(limit, offset); + + // Enhance each post with author details, comment count, and like count + const enhancedPosts = await Promise.all(posts.map(async (post) => { + const author = post.authorId ? await storage.getUser(post.authorId) : undefined; + const authorInfo = author ? { + id: author.id, + displayName: author.displayName, + avatar: author.avatar + } : undefined; + + // Get comment count + const comments = await storage.getCommentsByEntityId('blogPost', post.id); + const commentCount = comments.length; + + // Get like count + const likes = await storage.getLikesByEntity('blogPost', post.id); + const likeCount = likes.length; + + // Get tags for the post + const tags = await storage.getBlogPostTags(post.id); + + return { + ...post, + author: authorInfo, + commentCount, + likeCount, + tags + }; + })); + + res.json(enhancedPosts); + } catch (error) { + console.error("Error fetching blog posts:", error); + res.status(500).json({ message: "Failed to fetch blog posts" }); + } }); app.get("/api/blog/:slug", async (req: Request, res: Response) => { - const post = await storage.getBlogPostBySlug(req.params.slug); - if (!post) { - return res.status(404).json({ message: "Blog post not found" }); + try { + const post = await storage.getBlogPostBySlug(req.params.slug); + if (!post) { + return res.status(404).json({ message: "Blog post not found" }); + } + + // Get author details + const author = post.authorId ? await storage.getUser(post.authorId) : undefined; + + // Remove password from author if it exists + const authorInfo = author ? { + ...author, + password: undefined + } : undefined; + + // Get comments for the post + const comments = await storage.getCommentsByEntityId('blogPost', post.id); + + // Get enhanced comments with user info + const enhancedComments = await Promise.all(comments.map(async (comment) => { + const user = await storage.getUser(comment.userId); + return { + ...comment, + user: user ? { + id: user.id, + displayName: user.displayName, + avatar: user.avatar, + role: user.role + } : undefined + }; + })); + + // Get like count + const likes = await storage.getLikesByEntity('blogPost', post.id); + const likeCount = likes.length; + + // Get tags for the post + const tags = await storage.getBlogPostTags(post.id); + + res.json({ + ...post, + author: authorInfo, + comments: enhancedComments, + commentCount: comments.length, + likeCount, + tags + }); + } catch (error) { + console.error("Error fetching blog post:", error); + res.status(500).json({ message: "Failed to fetch blog post" }); } - res.json(post); }); app.post("/api/blog", async (req: Request, res: Response) => { diff --git a/server/storage.ts b/server/storage.ts index 0dd139d..ed2c6f9 100644 --- a/server/storage.ts +++ b/server/storage.ts @@ -46,6 +46,7 @@ export interface IStorage { createTag(tag: InsertTag): Promise; addTagToWork(workId: number, tagId: number): Promise; getWorkTags(workId: number): Promise; + getBlogPostTags(postId: number): Promise; // Bookmark methods getBookmarksByUserId(userId: number): Promise; @@ -57,6 +58,7 @@ export interface IStorage { getCommentsByWorkId(workId: number): Promise; getCommentsByTranslationId(translationId: number): Promise; getCommentsByUserId(userId: number): Promise; + getCommentsByEntityId(entityType: string, entityId: number): Promise; createComment(comment: InsertComment): Promise; // Like methods @@ -101,6 +103,7 @@ export class MemStorage implements IStorage { private translations: Map; private tags: Map; private workTagsRelations: Map>; + private blogPostTagsRelations: Map>; private bookmarks: Map; private comments: Map; private likes: Map; @@ -131,6 +134,7 @@ export class MemStorage implements IStorage { this.translations = new Map(); this.tags = new Map(); this.workTagsRelations = new Map(); + this.blogPostTagsRelations = new Map(); this.bookmarks = new Map(); this.comments = new Map(); this.likes = new Map();