Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix monthly APIs #52

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,15 @@ public WalkLog getNextData(long walkLogId, long userId) {
return WalkLogMapper.toDomain(entity, me);
}

public WalkLog getNextData(int year, int month, long userId) {
WalkLogEntity entity = walkLogRepository.getMaxIdLessThan(year, month, userId);
if (entity == null) {
return null;
}
UserEntity me = userRepository.getById(entity.getUserId());
return WalkLogMapper.toDomain(entity, me);
}

@Transactional
public WalkLog update(long id, long userId, PatchWalkLogRequest request, MultipartFile photo)
throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public ResponseEntity<GetWalkLogListGroupByMonthResponse> getListByUserIdAndMont
if (!walkLogList.isEmpty()) {
WalkLog lastData = walkLogList.getLast();
nextData = walkLogApplication.getNextData(lastData.getId(), userId);
} else {
nextData = walkLogApplication.getNextData(year, month, userId);
}
return ResponseEntity.ok(
GetWalkLogListGroupByMonthResponse.of(year, month, nextData, walkLogList));
Expand All @@ -100,6 +102,8 @@ public ResponseEntity<GetWalkLogListGroupByMonthResponse> getMyList(
if (walkLogList.size() > 0) {
WalkLog lastData = walkLogList.get(walkLogList.size() - 1);
nextData = walkLogApplication.getNextData(lastData.getId(), userInfo.getUserId());
} else {
nextData = walkLogApplication.getNextData(year, month, userInfo.getUserId());
}
return ResponseEntity.ok(
GetWalkLogListGroupByMonthResponse.of(year, month, nextData, walkLogList));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public interface CustomWalkLogRepository {

WalkLogEntity getMaxIdLessThan(long walkLogId, long userId);

WalkLogEntity getMaxIdLessThan(int year, int month, long userId);

boolean isTodayWalkLogExists(long walkLogId, LocalDate date);

long deleteByUserId(long userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@ public WalkLogEntity getMaxIdLessThan(long walkLogId, long userId) {
.fetchFirst();
}

@Override
public WalkLogEntity getMaxIdLessThan(int year, int month, long userId) {
int nextYear = month > 1 ? year : year - 1;
int nextMonth = month > 1 ? month - 1 : 12;
return jpaQueryFactory.selectFrom(walkLogEntity)
.where(
walkLogEntity.createdAt.year().loe(nextYear),
walkLogEntity.createdAt.month().loe(nextMonth),
walkLogEntity.userId.eq(userId),
walkLogEntity.deleted.isFalse()
)
.orderBy(walkLogEntity.id.desc())
.fetchFirst();
}

@Override
public boolean isTodayWalkLogExists(long userId, LocalDate date) {
Long id = jpaQueryFactory
Expand Down