-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from Team-Going/feature/26
[feat] 여행 대시보드 전체 조회 API 구현
- Loading branch information
Showing
8 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
doorip-api/src/main/java/org/doorip/trip/api/TripApiController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.doorip.trip.api; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.doorip.auth.UserId; | ||
import org.doorip.common.ApiResponse; | ||
import org.doorip.common.ApiResponseUtil; | ||
import org.doorip.message.SuccessMessage; | ||
import org.doorip.trip.dto.response.TripGetResponse; | ||
import org.doorip.trip.service.TripService; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
@RequiredArgsConstructor | ||
@RequestMapping("/api/trips") | ||
@Controller | ||
public class TripApiController { | ||
private final TripService tripService; | ||
|
||
@GetMapping | ||
public ResponseEntity<ApiResponse<?>> getTrips(@UserId final Long userId, @RequestParam final String progress) { | ||
final TripGetResponse response = tripService.getTrips(userId, progress); | ||
return ApiResponseUtil.success(SuccessMessage.OK, response); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
doorip-api/src/main/java/org/doorip/trip/dto/response/TripGetResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.doorip.trip.dto.response; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import org.doorip.trip.domain.Trip; | ||
|
||
import java.util.List; | ||
|
||
@Builder(access = AccessLevel.PRIVATE) | ||
public record TripGetResponse( | ||
String name, | ||
List<TripResponse> trips | ||
) { | ||
public static TripGetResponse of(String name, List<Trip> trips) { | ||
return TripGetResponse.builder() | ||
.name(name) | ||
.trips(trips.stream() | ||
.map(TripResponse::of) | ||
.toList()) | ||
.build(); | ||
|
||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
doorip-api/src/main/java/org/doorip/trip/dto/response/TripResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.doorip.trip.dto.response; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import org.doorip.trip.domain.Trip; | ||
|
||
import java.time.LocalDate; | ||
|
||
import static java.time.Period.between; | ||
|
||
@Builder(access = AccessLevel.PRIVATE) | ||
public record TripResponse( | ||
Long tripId, | ||
String title, | ||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Seoul") | ||
LocalDate startDate, | ||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Seoul") | ||
LocalDate endDate, | ||
int day | ||
) { | ||
public static TripResponse of(Trip trip) { | ||
return TripResponse.builder() | ||
.tripId(trip.getId()) | ||
.title(trip.getTitle()) | ||
.startDate(trip.getStartDate()) | ||
.endDate(trip.getEndDate()) | ||
.day(between(LocalDate.now(), trip.getStartDate()).getDays()) | ||
.build(); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
doorip-api/src/main/java/org/doorip/trip/service/TripService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.doorip.trip.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.doorip.common.Constants; | ||
import org.doorip.exception.EntityNotFoundException; | ||
import org.doorip.exception.InvalidValueException; | ||
import org.doorip.message.ErrorMessage; | ||
import org.doorip.trip.domain.Trip; | ||
import org.doorip.trip.dto.response.TripGetResponse; | ||
import org.doorip.trip.repository.TripRepository; | ||
import org.doorip.user.domain.User; | ||
import org.doorip.user.repository.UserRepository; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
@Service | ||
public class TripService { | ||
private final UserRepository userRepository; | ||
private final TripRepository tripRepository; | ||
|
||
public TripGetResponse getTrips(Long userId, String progress) { | ||
User findUser = getUser(userId); | ||
List<Trip> trips = getTripsAccordingToProgress(userId, progress); | ||
return TripGetResponse.of(findUser.getName(), trips); | ||
} | ||
|
||
private User getUser(Long userId) { | ||
return userRepository.findById(userId) | ||
.orElseThrow(() -> new EntityNotFoundException(ErrorMessage.USER_NOT_FOUND)); | ||
} | ||
|
||
private List<Trip> getTripsAccordingToProgress(Long userId, String progress) { | ||
if (progress.equals(Constants.INCOMPLETE)) { | ||
return tripRepository.findInCompleteTripsByUserId(userId, LocalDate.now()); | ||
} else if (progress.equals(Constants.COMPLETE)) { | ||
return tripRepository.findCompleteTripsByUserId(userId, LocalDate.now()); | ||
} | ||
throw new InvalidValueException(ErrorMessage.INVALID_REQUEST_PARAMETER_VALUE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
alter table participant drop foreign key participant_ibfk_1; | ||
drop index user_id on participant; | ||
alter table participant add constraint participant_ibfk_1 foreign key (user_id) references users (user_id); | ||
alter table participant drop foreign key participant_ibfk_2; | ||
drop index trip_id on participant; | ||
alter table participant add constraint participant_ibfk_2 foreign key (trip_id) references trip (trip_id); | ||
alter table todo drop foreign key todo_ibfk_1; | ||
drop index trip_id on todo; | ||
alter table todo add constraint todo_ibfk_1 foreign key (trip_id) references trip (trip_id); | ||
alter table allocator drop foreign key allocator_ibfk_2; | ||
drop index todo_id on allocator; | ||
alter table allocator add constraint allocator_ibfk_2 foreign key (todo_id) references todo (todo_id); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
doorip-domain/src/main/java/org/doorip/trip/repository/TripRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.doorip.trip.repository; | ||
|
||
import io.lettuce.core.dynamic.annotation.Param; | ||
import org.doorip.trip.domain.Trip; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
public interface TripRepository extends JpaRepository<Trip, Long> { | ||
@Query("select t " + | ||
"from Trip t " + | ||
"join Participant p " + | ||
"on p.trip = t " + | ||
"join User u " + | ||
"on p.user = u " + | ||
"where u.id = :userId " + | ||
"and datediff(t.endDate, :now) >= 0 " + | ||
"order by datediff(t.startDate, :now)") | ||
List<Trip> findInCompleteTripsByUserId(@Param("userId") Long userId, @Param("now") LocalDate now); | ||
|
||
@Query("select t " + | ||
"from Trip t " + | ||
"join Participant p " + | ||
"on p.trip = t " + | ||
"join User u " + | ||
"on p.user = u " + | ||
"where u.id = :userId " + | ||
"and datediff(t.endDate, :now) < 0 " + | ||
"order by datediff(:now, t.endDate)") | ||
List<Trip> findCompleteTripsByUserId(@Param("userId") Long userId, @Param("now") LocalDate now); | ||
} |