Skip to content

Commit

Permalink
[BE#190] 배포전 버그 수정들 (#191)
Browse files Browse the repository at this point in the history
* fix: 카테고리 삭제 body 상태코드응답

- HttpCode()를 사용하면 응답코드를 바꾼다는 것을 알았다.
- 204 넣으면 바디가 있더라도 응답 안줌

* feat: 학습기록 생성시 date 계산해서 저장

- 기준 시간과 learning_time을 빼서 이를 Date객체 .setTime을 통해 저장

* fix: accesstoken 인증시  async로 인한 실수

* feat: category를 기준으로 그룹화하여 today 기록 응답

- typeorm의 queryBuilder를 통해 쿼리 요청하여 그룹핑 및 합 계산

* fix: category_id 없어도 post

* fix: 카테고리 마다 시간 0이어도 출력

- 아우터 조인 실제 쿼리로 작성
  • Loading branch information
yeongbinim authored Nov 23, 2023
1 parent 442013c commit 8d9aef1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
1 change: 0 additions & 1 deletion BE/src/study-logs/study-logs.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export class StudyLogsController {
new Date(created_at),
learning_time,
);

return this.studyLogsService.create(studyLogsData, userId);
}

Expand Down
49 changes: 32 additions & 17 deletions BE/src/study-logs/study-logs.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class StudyLogsService {
): Promise<StudyLogsDto> {
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,
Expand All @@ -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<number> {
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<object> {
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;
Expand Down

0 comments on commit 8d9aef1

Please sign in to comment.