|
1 | 1 | import logger from '@/configs/logger.config'; |
2 | 2 | import { PostRepository } from '@/repositories/post.repository'; |
| 3 | +import { RawPostType } from '@/types'; |
3 | 4 |
|
4 | 5 | export class PostService { |
5 | 6 | constructor(private postRepo: PostRepository) {} |
@@ -52,20 +53,44 @@ export class PostService { |
52 | 53 | return await this.postRepo.getTotalPostCounts(id); |
53 | 54 | } |
54 | 55 |
|
55 | | - async getPost(postId: number, start?: string, end?: string) { |
| 56 | + async getPostByPostId(postId: number, start?: string, end?: string) { |
56 | 57 | try { |
57 | 58 | const posts = await this.postRepo.findPostByPostId(postId, start, end); |
58 | 59 |
|
59 | | - const transformedPosts = posts.map((post) => ({ |
60 | | - date: post.date, |
61 | | - dailyViewCount: parseInt(post.daily_view_count), |
62 | | - dailyLikeCount: parseInt(post.daily_like_count), |
63 | | - })); |
| 60 | + const transformedPosts = this.transformPosts(posts); |
| 61 | + |
| 62 | + return transformedPosts; |
| 63 | + } catch (error) { |
| 64 | + logger.error('PostService getPost error : ', error); |
| 65 | + throw error; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + async getPostByPostUUID(postId: string) { |
| 70 | + try { |
| 71 | + const seoulNow = new Date(new Date().getTime() + 9 * 60 * 60 * 1000); |
| 72 | + const sevenDaysAgo = new Date(seoulNow); |
| 73 | + |
| 74 | + const end = seoulNow.toISOString().split('T')[0]; |
| 75 | + sevenDaysAgo.setDate(seoulNow.getDate() - 6); |
| 76 | + const start = sevenDaysAgo.toISOString().split('T')[0]; |
| 77 | + |
| 78 | + const posts = await this.postRepo.findPostByPostUUID(postId, start, end); |
| 79 | + |
| 80 | + const transformedPosts = this.transformPosts(posts); |
64 | 81 |
|
65 | 82 | return transformedPosts; |
66 | 83 | } catch (error) { |
67 | | - logger.error('PostService getTotalPostCounts error : ', error); |
| 84 | + logger.error('PostService getPostByPostUUID error : ', error); |
68 | 85 | throw error; |
69 | 86 | } |
70 | 87 | } |
| 88 | + |
| 89 | + private transformPosts(posts: RawPostType[]) { |
| 90 | + return posts.map((post) => ({ |
| 91 | + date: post.date, |
| 92 | + dailyViewCount: post.daily_view_count, |
| 93 | + dailyLikeCount: post.daily_like_count, |
| 94 | + })); |
| 95 | + } |
71 | 96 | } |
0 commit comments