Skip to content

Commit

Permalink
[feat] 월별 총 리뷰 갯수 조회 기능, 연도별 총 리뷰 갯수 조회 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
syyling committed May 29, 2024
1 parent 25e73cf commit 9d89325
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,15 @@ public RecordWeekResponse(String tmdbId, String title, String poster) {
this.poster = poster;
}
}

@Getter
@NoArgsConstructor
public static class RecordTotalResponse {
private int total;

@Builder
public RecordTotalResponse(int total) {
this.total = total;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.mookive.mookive_backend.record.application.service;

import com.mookive.mookive_backend.record.application.dto.response.RecordResponse;
import com.mookive.mookive_backend.record.domain.service.RecordQueryService;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Service
@RequiredArgsConstructor
@Transactional
public class RecordTotalGetService {

private final RecordQueryService recordQueryService;

public RecordResponse.RecordTotalResponse getRecordTotalOfMonth(Long userId) {
LocalDate now = LocalDate.now();
LocalDate startDate = LocalDate.of(now.getYear(), now.getMonthValue(), 1);
LocalTime startTime = LocalTime.of(0,0,0,0);
LocalDateTime startLocalDateTime = LocalDateTime.of(startDate, startTime);

LocalDate endDate = LocalDate.of(now.getYear(), now.getMonthValue(), now.lengthOfMonth());
LocalTime endTime = LocalTime.of(23,59,9,999999999);
LocalDateTime endLocalDateTime = LocalDateTime.of(endDate, endTime);

int total = recordQueryService.countByUserIdAndCreatedAtBetween(userId, startLocalDateTime, endLocalDateTime);
return RecordResponse.RecordTotalResponse.builder()
.total(total)
.build();
}

public RecordResponse.RecordTotalResponse getRecordTotalOfYear(Long userId) {
LocalDate now = LocalDate.now();
LocalDate startDate = LocalDate.of(now.getYear(), 1, 1);
LocalTime startTime = LocalTime.of(0,0,0,0);
LocalDateTime startLocalDateTime = LocalDateTime.of(startDate, startTime);

LocalDate endDate = LocalDate.of(now.getYear(), 12, 31);
LocalTime endTime = LocalTime.of(23,59,9,999999999);
LocalDateTime endLocalDateTime = LocalDateTime.of(endDate, endTime);

int total = recordQueryService.countByUserIdAndCreatedAtBetween(userId, startLocalDateTime, endLocalDateTime);
return RecordResponse.RecordTotalResponse.builder()
.total(total)
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ public interface RecordRepository extends JpaRepository<Record, Long> {
List<Record> findTop20ByOrderByUpdatedAtDesc();

List<MovieMapping> findAllByUserId(Long userId);

int countByUserIdAndCreatedAtBetween(Long userId, LocalDateTime startOfLocalDateTime, LocalDateTime endOfLocalDateTime);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public List<Record> findByUserIdAndUpdatedAtBetween(Long userId, LocalDateTime s
return recordRepository.findByUserIdAndUpdatedAtBetween(userId, startDate, endDate);
}

public int countByUserIdAndCreatedAtBetween(Long userId, LocalDateTime startOfLocalDateTime, LocalDateTime endOfLocalDateTime) {
return recordRepository.countByUserIdAndCreatedAtBetween(userId, startOfLocalDateTime, endOfLocalDateTime);
}

public List<Record> findTop5ByUserIdOrderByUpdatedAtDesc(Long userId) {
return recordRepository.findTop5ByUserIdOrderByUpdatedAtDesc(userId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.mookive.mookive_backend.record.application.service.RecordCreateService;
import com.mookive.mookive_backend.record.application.service.RecordDeleteService;
import com.mookive.mookive_backend.record.application.service.RecordGetService;
import com.mookive.mookive_backend.record.application.service.RecordTotalGetService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

Expand All @@ -17,6 +18,7 @@ public class RecordController {
private final RecordCreateService recordCreateService;
private final RecordGetService recordGetService;
private final RecordDeleteService recordDeleteService;
private final RecordTotalGetService recordTotalGetService;

@PostMapping("/record")
public void createRecord(@RequestBody RecordRequest.RecordCreateRequest recordCreateRequest) {
Expand Down Expand Up @@ -53,4 +55,14 @@ public List<RecordResponse.RecordWeekResponse> getRecordWithinWeek(@PathVariable
return recordGetService.getRecordWithinWeek(userId);
}

@GetMapping("/record/total/month/{userId}")
public RecordResponse.RecordTotalResponse getRecordTotalOfMonth(@PathVariable Long userId) {
return recordTotalGetService.getRecordTotalOfMonth(userId);
}

@GetMapping("/record/total/year/{userId}")
public RecordResponse.RecordTotalResponse getRecordTotalOfYear(@PathVariable Long userId) {
return recordTotalGetService.getRecordTotalOfYear(userId);
}

}

0 comments on commit 9d89325

Please sign in to comment.