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();