|
1 | | -import { Injectable } from '@nestjs/common'; |
| 1 | +import { Injectable, NotFoundException } from '@nestjs/common'; |
2 | 2 | import { ActivityRepository } from '../repository/activity.repository'; |
3 | | -import { User } from '../../user/entity/user.entity'; |
| 3 | +import { UserRepository } from '../../user/repository/user.repository'; |
| 4 | +import { |
| 5 | + ActivityReadResponseDto, |
| 6 | + DailyActivityDto, |
| 7 | +} from '../dto/response/activity-read.dto'; |
4 | 8 |
|
5 | 9 | @Injectable() |
6 | 10 | export class ActivityService { |
7 | | - constructor(private readonly activityRepository: ActivityRepository) {} |
| 11 | + constructor( |
| 12 | + private readonly activityRepository: ActivityRepository, |
| 13 | + private readonly userRepository: UserRepository, |
| 14 | + ) {} |
8 | 15 |
|
9 | | - async readActivities(userId: number, year: number) { |
10 | | - // TODO: 연도별 활동 데이터 전체 조회 |
11 | | - // 1. 연도별 활동 데이터 전체 반환 (일, 활동수 쌍) |
| 16 | + async readActivities( |
| 17 | + userId: number, |
| 18 | + year: number, |
| 19 | + ): Promise<ActivityReadResponseDto> { |
| 20 | + const user = await this.userRepository.findOneBy({ id: userId }); |
| 21 | + if (!user) { |
| 22 | + throw new NotFoundException('존재하지 않는 사용자입니다.'); |
| 23 | + } |
12 | 24 |
|
13 | | - // + metric 수집 및 반환 (User Entity에 존재함.) |
14 | | - // 1. 최장 읽기 스트릭 |
15 | | - // 2. 현재 읽기 스트릭 |
16 | | - // 3. 총 읽은 횟수 |
17 | | - return 'foo'; |
| 25 | + const activities = |
| 26 | + await this.activityRepository.findActivitiesByUserIdAndYear(userId, year); |
| 27 | + |
| 28 | + const dailyActivities = activities.map( |
| 29 | + (activity) => |
| 30 | + new DailyActivityDto({ |
| 31 | + date: activity.activityDate.toISOString().split('T')[0], |
| 32 | + viewCount: activity.viewCount, |
| 33 | + }), |
| 34 | + ); |
| 35 | + |
| 36 | + return new ActivityReadResponseDto({ |
| 37 | + dailyActivities, |
| 38 | + maxStreak: user.maxStreak, |
| 39 | + currentStreak: user.currentStreak, |
| 40 | + totalViews: user.totalViews, |
| 41 | + }); |
18 | 42 | } |
19 | 43 |
|
20 | 44 | async upsertActivity(userId: number) { |
|
0 commit comments