Skip to content

Commit

Permalink
Merge pull request #43 from MAKAR-iOS/feat/#42
Browse files Browse the repository at this point in the history
[Feat/#42] 즐겨찾는 경로 기능 구현
  • Loading branch information
daeun084 authored Jul 29, 2024
2 parents b4c81ad + 48fa5ed commit 75c8c31
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 27 deletions.
5 changes: 5 additions & 0 deletions src/main/java/makar/dev/common/status/ErrorStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ public enum ErrorStatus {
INVALID_API_KEY(HttpStatus.BAD_REQUEST, 400, "유효하지 않은 API Key입니다."),
INVALID_REQUEST(HttpStatus.BAD_REQUEST, 400, "유효하지 않은 요청입니다."),
INVALID_SOURCE_TIME_FORMAT(HttpStatus.BAD_REQUEST, 400, "유효하지 않은 막차 시간 형식으로 인해 도착 시간 계산 중 오류가 발생했습니다."),
// route
ALREADY_ROUTE_SET(HttpStatus.BAD_REQUEST, 400, "이미 설정된 경로가 존재합니다."),
INVALID_ROUTE_DELETE(HttpStatus.BAD_REQUEST, 400, "설정된 경로가 존재하지 않습니다."),
INVALID_ROUTE_SET(HttpStatus.BAD_REQUEST, 400, "설정된 경로가 존재하지 않습니다."),
ALREADY_FAVORITE_ROUTE_SET(HttpStatus.BAD_REQUEST, 400, "해당 경로는 이미 즐겨찾는 경로로 설정되었습니다."),
INVALID_FAVORITE_ROUTE_DELETE(HttpStatus.BAD_REQUEST, 400, "해당 경로는 즐겨찾는 경로로 설정되지 않았습니다."),

// noti
INVALID_NOTI_MINUTE(HttpStatus.BAD_REQUEST, 400, "이미 설정된 알림입니다."),
INVALID_NOTI_DELETE(HttpStatus.BAD_REQUEST, 400, "잘못된 타입의 알림 삭제 요청입니다."),
INVALID_GETOFF_NOTI_MINUTE(HttpStatus.BAD_REQUEST, 400, "하차 알림 시간이 열차 탑승 시간보다 클 수 없습니다."),
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/makar/dev/common/status/SuccessStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public enum SuccessStatus {
_ROUTE_DELETE(HttpStatus.OK, 200, "경로 삭제가 완료되었습니다."),
_ROUTE_POST(HttpStatus.OK, 200, "경로 설정이 완료되었습니다."),
_SET_ROUTE_GET(HttpStatus.OK, 200, "설정된 경로 조회가 완료되었습니다."),
_FAVORITE_ROUTE_LIST_GET(HttpStatus.OK, 200, "즐겨찾는 경로 리스트 조회가 완료되었습니다."),
_FAVORITE_ROUTE_POST(HttpStatus.OK, 200, "즐겨찾는 경로 추가가 완료되었습니다."),
_FAVORITE_ROUTE_DELETE(HttpStatus.OK, 200, "즐겨찾는 경로 삭제가 완료되었습니다."),

// NotiController
_MAKAR_NOTI_POST(HttpStatus.OK, 200, "막차 알림 추가가 완료되었습니다."),
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/makar/dev/controller/NotiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public ApiResponse postMakarNoti(@RequestBody NotiRequest.NotiDto notiDto,
summary = "막차 알림 삭제",
description = "설정된 경로의 막차 알림을 삭제하고 유저의 알림 리스트를 반환합니다."
)
@DeleteMapping("/makar")
public ApiResponse deleteMakarNoti(@RequestParam(value = "notiId") Long notiId,
@DeleteMapping("/makar/{notiId}")
public ApiResponse deleteMakarNoti(@PathVariable(name = "notiId") Long notiId,
@AuthenticationPrincipal TokenDto tokenDto){
return ApiResponse.SuccessResponse(SuccessStatus._MAKAR_NOTI_DELETE, notiService.deleteNoti(notiId, tokenDto, Notification.MAKAR));
}
Expand All @@ -51,8 +51,8 @@ public ApiResponse postGetOffNoti(@RequestBody NotiRequest.NotiDto notiDto,
summary = "하차 알림 삭제",
description = "설정된 경로의 하차 알림을 삭제하고 유저의 알림 리스트를 반환합니다."
)
@DeleteMapping("/getoff")
public ApiResponse deleteGetOffNoti(@RequestParam(value = "notiId") Long notiId,
@DeleteMapping("/getoff/{notiId}")
public ApiResponse deleteGetOffNoti(@PathVariable(name = "notiId") Long notiId,
@AuthenticationPrincipal TokenDto tokenDto){
return ApiResponse.SuccessResponse(SuccessStatus._GETOFF_NOTI_DELETE, notiService.deleteNoti(notiId, tokenDto, Notification.GETOFF));
}
Expand Down
30 changes: 28 additions & 2 deletions src/main/java/makar/dev/controller/RouteController.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public ApiResponse searchRoute(@RequestParam(value = "fromStationName") String f
summary = "경로 설정",
description = "경로를 설정합니다."
)
@PostMapping()
public ApiResponse setRoute(@AuthenticationPrincipal TokenDto tokenDto, @RequestParam(value = "routeId") Long routeId){
@PostMapping("/{routeId}")
public ApiResponse setRoute(@AuthenticationPrincipal TokenDto tokenDto, @PathVariable(name = "routeId") Long routeId){
return ApiResponse.SuccessResponse(SuccessStatus._ROUTE_POST, routeService.setRoute(tokenDto.getUserId(), routeId));
}

Expand All @@ -54,5 +54,31 @@ public ApiResponse getSetRoute(@AuthenticationPrincipal TokenDto tokenDto){
return ApiResponse.SuccessResponse(SuccessStatus._SET_ROUTE_GET, routeService.getSetRoute(tokenDto.getUserId()));
}

@Operation(
summary = "즐겨찾는 경로 조회",
description = "즐겨찾는 경로 리스트를 조회합니다."
)
@GetMapping("/favorite")
public ApiResponse getFavoriteRouteList(@AuthenticationPrincipal TokenDto tokenDto) {
return ApiResponse.SuccessResponse(SuccessStatus._FAVORITE_ROUTE_LIST_GET, routeService.getFavoriteRouteList(tokenDto));
}

@Operation(
summary = "즐겨찾는 경로 추가",
description = "즐겨찾는 경로를 추가합니다."
)
@PostMapping("/favorite/{routeId}")
public ApiResponse addFavoriteRoute(@AuthenticationPrincipal TokenDto tokenDto, @PathVariable(name = "routeId") Long routeId) {
routeService.addFavoriteRoute(tokenDto, routeId);
return ApiResponse.SuccessResponse(SuccessStatus._FAVORITE_ROUTE_POST);
}

@Operation(
summary = "즐겨찾는 경로 삭제",
description = "즐겨찾는 경로를 삭제합니다."
)
@DeleteMapping("/favorite/{routeId}")
public ApiResponse deleteFavoriteRoute(@AuthenticationPrincipal TokenDto tokenDto, @PathVariable(name = "routeId") Long routeId) {
return ApiResponse.SuccessResponse(SuccessStatus._FAVORITE_ROUTE_DELETE, routeService.deleteFavoriteRoute(tokenDto, routeId));
}
}
2 changes: 1 addition & 1 deletion src/main/java/makar/dev/controller/StationController.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public ApiResponse updateFavoriteSchoolStation(@RequestBody StationRequest.Favor
}

@Operation(
summary = "즐겨찾는 역() 조회",
summary = "즐겨찾는 역() 조회",
description = "즐겨찾는 역(집)의 정보를 조회합니다."
)
@GetMapping("/favorite/home")
Expand Down
1 change: 1 addition & 0 deletions src/main/java/makar/dev/converter/RouteConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public static RouteResponse.RouteDetailDto toRouteDetailDto(Route route) {

public static RouteResponse.BriefRouteDto toBriefRouteDto(Route route){
return RouteResponse.BriefRouteDto.builder()
.routeId(route.getRouteId())
.sourceStationName(route.getSourceStation().getStationName())
.sourceLineNum(route.getSourceStation().getLineNum())
.destinationStationName(route.getDestinationStation().getStationName())
Expand Down
14 changes: 1 addition & 13 deletions src/main/java/makar/dev/converter/UserConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,13 @@
import makar.dev.domain.Route;
import makar.dev.domain.User;
import makar.dev.dto.response.NotiResponse;
import makar.dev.dto.response.RouteResponse;
import makar.dev.dto.response.UserResponse;

import java.util.List;

public class UserConverter {

public static UserResponse.HomeDto toHomeDto(boolean isRouteSet, List<Noti> notiList,
List<Route> favoriteRouteList, List<Route> recentRouteList){
List<RouteResponse.BriefRouteDto> favoriteRouteDtoList = favoriteRouteList.stream()
.map(RouteConverter::toBriefRouteDto)
.toList();
List<RouteResponse.BriefRouteDto> recentRouteDtoList = recentRouteList.stream()
.map(RouteConverter::toBriefRouteDto)
.toList();
public static UserResponse.HomeDto toHomeDto(boolean isRouteSet, List<Noti> notiList){

if (notiList.isEmpty())
return UserResponse.HomeDto.builder()
Expand All @@ -28,8 +20,6 @@ public static UserResponse.HomeDto toHomeDto(boolean isRouteSet, List<Noti> noti
.makarTime(null)
.getoffTime(null)
.notiList(null)
.favoriteRouteList(favoriteRouteDtoList)
.recentRouteList(recentRouteDtoList)
.build();

Route route = notiList.get(0).getRoute();
Expand All @@ -43,8 +33,6 @@ public static UserResponse.HomeDto toHomeDto(boolean isRouteSet, List<Noti> noti
.makarTime(route.getSchedule().getSourceTime())
.getoffTime(route.getSchedule().getDestinationTime())
.notiList(notiDtoList)
.favoriteRouteList(favoriteRouteDtoList)
.recentRouteList(recentRouteDtoList)
.build();
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/makar/dev/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ public class User {
@OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
private List<Noti> notiList; //알림 설정

@OneToMany(cascade = CascadeType.ALL)
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Route> recentRouteList; //최근 경로 리스트

@OneToMany(cascade = CascadeType.ALL)
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Route> favoriteRouteList; //즐겨찾는 경로 리스트

public User(String userName){
Expand All @@ -59,6 +59,7 @@ public User(String userName){
public boolean isFavoriteHomeStationExist(){return this.favoriteHomeStation != null;}
public boolean isFavoriteSchoolStationExist(){return this.favoriteSchoolStation != null;}
public void addNotiList(Noti noti){this.notiList.add(noti);}
public void addFavoriteRoute(Route route){this.favoriteRouteList.add(favoriteRouteList.size(), route);}


@Builder
Expand Down
1 change: 1 addition & 0 deletions src/main/java/makar/dev/dto/response/RouteResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public static class RouteDetailDto {
@NoArgsConstructor
@AllArgsConstructor
public static class BriefRouteDto {
private Long routeId;
private String sourceStationName;
private String sourceLineNum;
private String destinationStationName;
Expand Down
43 changes: 42 additions & 1 deletion src/main/java/makar/dev/service/RouteService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import makar.dev.common.exception.GeneralException;
import makar.dev.common.security.dto.TokenDto;
import makar.dev.common.status.ErrorStatus;
import makar.dev.converter.NotiConverter;
import makar.dev.converter.RouteConverter;
Expand Down Expand Up @@ -95,6 +96,47 @@ public RouteResponse.RouteDetailDto getSetRoute(Long userId){
return RouteConverter.toRouteDetailDto(route);
}

// 즐겨찾는 경로 조회
public List<RouteResponse.BriefRouteDto> getFavoriteRouteList(TokenDto tokenDto) {
User user = findUserById(tokenDto.getUserId());
List<Route> favoriteRouteList = user.getFavoriteRouteList();
// TODO: fix routeId order
return favoriteRouteList.stream()
.map(RouteConverter::toBriefRouteDto)
.toList();
}

// 즐겨찾는 경로 추가
@Transactional
public void addFavoriteRoute(TokenDto tokenDto, Long routeId) {
User user = findUserById(tokenDto.getUserId());
Route route = findRouteById(routeId);

// 해당 경로가 이미 즐겨찾는 경로로 설정된 경우
List<Route> favoriteRouteList = user.getFavoriteRouteList();
if (favoriteRouteList.contains(route))
throw new GeneralException(ErrorStatus.ALREADY_FAVORITE_ROUTE_SET);

user.addFavoriteRoute(route);
}

// 즐겨찾는 경로 삭제
@Transactional
public List<RouteResponse.BriefRouteDto> deleteFavoriteRoute(TokenDto tokenDto, Long routeId) {
User user = findUserById(tokenDto.getUserId());
Route route = findRouteById(routeId);

// 해당 경로가 즐겨찾는 경로로 설정되지 않은 경우
List<Route> favoriteRouteList = user.getFavoriteRouteList();
if (!favoriteRouteList.contains(route))
throw new GeneralException(ErrorStatus.INVALID_FAVORITE_ROUTE_DELETE);

favoriteRouteList.remove(route);
return favoriteRouteList.stream()
.map(RouteConverter::toBriefRouteDto)
.toList();
}

private Station findStation(String stationName, String lineNum){
return stationRepository.findByStationNameAndLineNum(stationName, lineNum)
.orElseThrow(() -> new GeneralException(ErrorStatus.NOT_FOUND_STATION));
Expand Down Expand Up @@ -269,5 +311,4 @@ private String getDestinationTime(String sourceTime, int totalTime) {
}
return sdf.format(calendar.getTime());
}

}
5 changes: 1 addition & 4 deletions src/main/java/makar/dev/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ public UserResponse.HomeDto getHome(TokenDto tokenDto) {
User user = findUserById(tokenDto.getUserId());
boolean isRouteSet = isRouteSet(user);

List<Route> favoriteRouteList = user.getFavoriteRouteList();
List<Route> recentRouteList = user.getRecentRouteList();

return UserConverter.toHomeDto(isRouteSet, user.getNotiList(), favoriteRouteList, recentRouteList);
return UserConverter.toHomeDto(isRouteSet, user.getNotiList());
}

private User findUserById(Long userId){
Expand Down

0 comments on commit 75c8c31

Please sign in to comment.