From 8d9aef1425fda26d1e7d070eed0051eb6fb0558c Mon Sep 17 00:00:00 2001 From: Yeongbin Im <56269396+yeongbinim@users.noreply.github.com> Date: Fri, 24 Nov 2023 01:05:18 +0900 Subject: [PATCH] =?UTF-8?q?[BE#190]=20=EB=B0=B0=ED=8F=AC=EC=A0=84=20?= =?UTF-8?q?=EB=B2=84=EA=B7=B8=20=EC=88=98=EC=A0=95=EB=93=A4=20(#191)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 카테고리 삭제 body 상태코드응답 - HttpCode()를 사용하면 응답코드를 바꾼다는 것을 알았다. - 204 넣으면 바디가 있더라도 응답 안줌 * feat: 학습기록 생성시 date 계산해서 저장 - 기준 시간과 learning_time을 빼서 이를 Date객체 .setTime을 통해 저장 * fix: accesstoken 인증시 async로 인한 실수 * feat: category를 기준으로 그룹화하여 today 기록 응답 - typeorm의 queryBuilder를 통해 쿼리 요청하여 그룹핑 및 합 계산 * fix: category_id 없어도 post * fix: 카테고리 마다 시간 0이어도 출력 - 아우터 조인 실제 쿼리로 작성 --- BE/src/study-logs/study-logs.controller.ts | 1 - BE/src/study-logs/study-logs.service.ts | 49 ++++++++++++++-------- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/BE/src/study-logs/study-logs.controller.ts b/BE/src/study-logs/study-logs.controller.ts index a53eab9..dd334c7 100644 --- a/BE/src/study-logs/study-logs.controller.ts +++ b/BE/src/study-logs/study-logs.controller.ts @@ -39,7 +39,6 @@ export class StudyLogsController { new Date(created_at), learning_time, ); - return this.studyLogsService.create(studyLogsData, userId); } diff --git a/BE/src/study-logs/study-logs.service.ts b/BE/src/study-logs/study-logs.service.ts index 77826e5..a238268 100644 --- a/BE/src/study-logs/study-logs.service.ts +++ b/BE/src/study-logs/study-logs.service.ts @@ -21,7 +21,7 @@ export class StudyLogsService { ): Promise { const { category_id, ...data } = studyLogsData; const user = { id: user_id } as UsersModel; - const category = { id: category_id } as Categories; + const category = { id: category_id ?? null } as Categories; const studyLog = this.studyLogsRepository.create({ ...data, @@ -44,31 +44,46 @@ export class StudyLogsService { return started_at; } - private calculateTotalTime(studyLogs): number { - return studyLogs.reduce((accumulator, studyLog) => { - return accumulator + studyLog.today_time; - }, 0); + private async calculateTotalTime( + user_id: number, + date: string, + ): Promise { + const sum = await this.studyLogsRepository + .createQueryBuilder('study_logs') + .select('SUM(study_logs.learning_time)', 'sum') + .where('study_logs.user_id = :user_id', { user_id }) + .andWhere('study_logs.date = :date', { date }) + .getRawOne(); + return parseInt(sum.sum); } async groupByCategory(user_id: number, date: string): Promise { - const studyLogsByCategory = await this.studyLogsRepository - .createQueryBuilder('study_logs') - .select('study_logs.category_id', 'category_id') - .addSelect('SUM(study_logs.learning_time)', 'today_time') - .addSelect('categories.name', 'name') - .addSelect('categories.color_code', 'color_code') - .leftJoin('study_logs.category_id', 'categories') - .where('study_logs.date = :today', { today: date }) - .andWhere('study_logs.user_id = :user_id', { user_id }) - .groupBy('study_logs.category_id') - .getRawMany(); + const studyLogsByCategory = await this.studyLogsRepository.query( + `SELECT + c.id as category_id, + c.name, + c.color_code, + IFNULL(SUM(s.learning_time), 0) as today_time + FROM + study_logs s + + RIGHT OUTER JOIN + categories c ON c.id = s.category_id + AND s.user_id = ? + AND DATE(s.date) = ? + GROUP BY + c.id, c.name, c.color_code; + `, + [user_id, date], + ); + const categories = studyLogsByCategory.map((studyLog) => ({ ...studyLog, today_time: parseInt(studyLog.today_time), })); const result = { - total_time: this.calculateTotalTime(categories), + total_time: await this.calculateTotalTime(user_id, date), categories, }; return result;