underrate commited on
Commit
86cd98c
·
verified ·
1 Parent(s): 239b4ae

Upload 25 files

Browse files
src/controllers/blog-related.controller.ts CHANGED
@@ -1,6 +1,23 @@
1
  import { Request, Response } from 'express';
2
  import prisma from '../models/prisma';
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  // GET /api/blog/:slug/related (Public)
5
  export const getRelatedPosts = async (req: Request, res: Response) => {
6
  try {
@@ -56,7 +73,7 @@ export const getRelatedPosts = async (req: Request, res: Response) => {
56
  });
57
 
58
  // Score based on shared tags
59
- const scoredPosts = allOtherPosts.map(post => {
60
  let postTags: string[] = [];
61
  try {
62
  if (post.tags) postTags = JSON.parse(post.tags);
@@ -67,10 +84,10 @@ export const getRelatedPosts = async (req: Request, res: Response) => {
67
  });
68
 
69
  // Sort by score (desc), then date (desc) implicitly from the DB query
70
- scoredPosts.sort((a, b) => b.score - a.score);
71
 
72
  // Take top 3
73
- const related = scoredPosts.slice(0, 3).map(s => s.post);
74
 
75
  return res.status(200).json(related);
76
  } catch (error) {
 
1
  import { Request, Response } from 'express';
2
  import prisma from '../models/prisma';
3
 
4
+ // Type for blog post with author
5
+ type BlogPostWithAuthor = {
6
+ id: string;
7
+ title: string;
8
+ slug: string;
9
+ content: string;
10
+ excerpt: string | null;
11
+ featuredImage: string | null;
12
+ tags: string | null;
13
+ status: string;
14
+ publishedAt: Date | null;
15
+ createdAt: Date;
16
+ updatedAt: Date;
17
+ authorId: string;
18
+ author: { name: string | null } | null;
19
+ };
20
+
21
  // GET /api/blog/:slug/related (Public)
22
  export const getRelatedPosts = async (req: Request, res: Response) => {
23
  try {
 
73
  });
74
 
75
  // Score based on shared tags
76
+ const scoredPosts: { post: BlogPostWithAuthor; score: number }[] = allOtherPosts.map((post: BlogPostWithAuthor) => {
77
  let postTags: string[] = [];
78
  try {
79
  if (post.tags) postTags = JSON.parse(post.tags);
 
84
  });
85
 
86
  // Sort by score (desc), then date (desc) implicitly from the DB query
87
+ scoredPosts.sort((a: { post: BlogPostWithAuthor; score: number }, b: { post: BlogPostWithAuthor; score: number }) => b.score - a.score);
88
 
89
  // Take top 3
90
+ const related = scoredPosts.slice(0, 3).map((s: { post: BlogPostWithAuthor; score: number }) => s.post);
91
 
92
  return res.status(200).json(related);
93
  } catch (error) {