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

[FEAT] 메인 다가올 일정(1개) 조회 API - #58 #59

Merged
merged 7 commits into from
Jul 11, 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 @@ -5,6 +5,7 @@
import org.dateroad.date.dto.request.DateCreateReq;
import org.dateroad.date.dto.response.DateDetailRes;
import org.dateroad.date.dto.response.DatesGetRes;
import org.dateroad.date.dto.response.DateGetNearestRes;
import org.dateroad.date.service.DateService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -43,4 +44,11 @@ public ResponseEntity<Void> deleteDate(@UserId final Long userId,
dateService.deleteDate(userId, dateId);
return ResponseEntity.ok().build();
}

@GetMapping("/nearest")
public ResponseEntity<DateGetNearestRes> getNearestDate(@UserId final Long userId) {
DateGetNearestRes dateGetNearestRes = dateService.getNearestDate(userId);
return ResponseEntity
.ok(dateGetNearestRes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.dateroad.date.dto.response;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AccessLevel;
import lombok.Builder;

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

@Builder(access = AccessLevel.PRIVATE)
public record DateGetNearestRes(
Long dateId,
int dDay,
String dateName,
int month,
int day,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "hh:mm a", timezone = "Asia/Seoul")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기도 a를 안받아주셔도 12:00 이렇게 매핑될 것 같습니다!

LocalTime startAt
) {
public static DateGetNearestRes of(Long dateId,
int dDay,
String dateName,
int month,
int day,
LocalTime startAt) {
return DateGetNearestRes.builder()
.dateId(dateId)
.dDay(dDay)
.dateName(dateName)
.month(month)
.day(day)
.startAt(startAt)
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@
import java.time.LocalDate;
import java.util.List;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Optional;

public interface DateRepository extends JpaRepository<Date, Long> {
Optional<Date> findFirstByUserIdAndDateAfterOrDateAndStartAtAfterOrderByDateAscStartAtAsc(
Long userId, LocalDate currentDate, LocalDate sameDay, LocalTime currentTime);
@Query("select d from Date d where d.user.id = :userId and d.date < :currentDate")
List<Date> findPastDatesByUserId(@Param("userId") Long userId, @Param("currentDate") LocalDate currentDate);

@Query("select d from Date d where d.user.id = :userId and d.date >= :currentDate")
List<Date> findFutureDatesByUserId(@Param("userId") Long userId, @Param("currentDate") LocalDate currentDate);
}


Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.dateroad.date.dto.response.DateDetailRes;
import org.dateroad.date.dto.response.DateGetRes;
import org.dateroad.date.dto.response.DatesGetRes;
import org.dateroad.date.dto.response.DateGetNearestRes;
import org.dateroad.date.repository.DatePlaceRepository;
import org.dateroad.date.repository.DateTagRepository;
import org.dateroad.exception.EntityNotFoundException;
Expand All @@ -22,6 +23,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.List;

Expand Down Expand Up @@ -70,7 +72,24 @@ public void deleteDate(final Long userId, final Long dateId) {
dateRepository.deleteById(dateId);
}

private User getUser(final Long userId) {
public DateGetNearestRes getNearestDate(final Long userId) {
LocalDate currentDate = LocalDate.now();
LocalTime currentTime = LocalTime.now();
User findUser = getUser(userId);
Date nearest = findNearestDate(findUser.getId(), currentDate, currentTime); ;
int dDay = calculateDDay(nearest.getDate(), currentDate);
return DateGetNearestRes
.of(
nearest.getId(),
dDay,
nearest.getTitle(),
nearest.getDate().getMonthValue(),
nearest.getDate().getDayOfMonth(),
nearest.getStartAt()
);
}

private User getUser(Long userId) {
return userRepository.findById(userId)
.orElseThrow(() -> new EntityNotFoundException(FailureCode.USER_NOT_FOUND));
}
Expand Down Expand Up @@ -141,4 +160,19 @@ private List<DatePlace> getDatePlace(final Date date) {
}
return datePlaces;
}
//dDay 계산
private int calculateDDay(LocalDate eventDate, LocalDate currentDate) {
if (eventDate.isEqual(currentDate)) {
return 0;
} else {
return (int) ChronoUnit.DAYS.between(currentDate, eventDate);
}
}

//가장 가까운 데이트 가져오기
private Date findNearestDate(Long userId, LocalDate currentDate, LocalTime currentTime) {
return dateRepository.findFirstByUserIdAndDateAfterOrDateAndStartAtAfterOrderByDateAscStartAtAsc(userId, currentDate, currentDate, currentTime)
.orElseThrow(() -> new EntityNotFoundException(FailureCode.DATE_NOT_FOUND));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public enum FailureCode {
DATE_NOT_FOUND(HttpStatus.NOT_FOUND, "e4044", "데이트를 찾을 수 없습니다."),
DATE_TAG_NOT_FOUND(HttpStatus.NOT_FOUND, "e4045", "데이트 태그를 찾을 수 없습니다."),
DATE_PLACE_NOT_FOUND(HttpStatus.NOT_FOUND, "e4046", "데이트 장소를 찾을 수 없습니다."),
NEAREST_DATE_NOT_FOUND(HttpStatus.NOT_FOUND, "e4047", "다가오는 데이트를 찾을 수 없습니다."),

/**
* 405 Method Not Allowed
Expand Down
Loading