Skip to content

Commit

Permalink
Merge pull request #39 from MAKAR-iOS/feat/#38
Browse files Browse the repository at this point in the history
[Feat/#38] 홈 화면 조회 API
  • Loading branch information
daeun084 authored Jul 29, 2024
2 parents 3d2931f + a29efd2 commit db6fd60
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/main/java/makar/dev/common/status/SuccessStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ public enum SuccessStatus {
_MAKAR_NOTI_DELETE(HttpStatus.OK, 200, "막차 알림 삭제가 완료되었습니다."),
_GETOFF_NOTI_POST(HttpStatus.OK, 200, "하차 알림 추가가 완료되었습니다."),
_GETOFF_NOTI_DELETE(HttpStatus.OK, 200, "하차 알림 삭제가 완료되었습니다."),
_NOTI_LIST_GET(HttpStatus.OK, 200, "알림 리스트 조회가 완료되었습니다.");
_NOTI_LIST_GET(HttpStatus.OK, 200, "알림 리스트 조회가 완료되었습니다."),

// UserController
_HOME_GET(HttpStatus.OK, 200, "홈 화면 조회가 완료되었습니다.");

private final HttpStatus httpStatus;
private final int code;
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/makar/dev/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import makar.dev.common.response.ApiResponse;
import makar.dev.common.security.dto.TokenDto;
import makar.dev.common.status.SuccessStatus;
import makar.dev.service.UserService;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

@RestController
Expand All @@ -23,4 +25,13 @@ public ApiResponse initStation(){
return ApiResponse.SuccessResponse(SuccessStatus._OK);
}

@Operation(
summary = "홈 화면 조회",
description = "홈 화면을 조회합니다."
)
@GetMapping("/home")
public ApiResponse getHome(@AuthenticationPrincipal TokenDto tokenDto){
return ApiResponse.SuccessResponse(SuccessStatus._HOME_GET, userService.getHome(tokenDto));
}

}
9 changes: 9 additions & 0 deletions src/main/java/makar/dev/converter/RouteConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ public static RouteResponse.RouteDetailDto toRouteDetailDto(Route route) {
.build();
}

public static RouteResponse.BriefRouteDto toBriefRouteDto(Route route){
return RouteResponse.BriefRouteDto.builder()
.sourceStationName(route.getSourceStation().getStationName())
.sourceLineNum(route.getSourceStation().getLineNum())
.destinationStationName(route.getDestinationStation().getStationName())
.destinationLineNum(route.getDestinationStation().getLineNum())
.build();
}

public static SubRoute toSubRoute(RouteSearchResponse.SubPath subPath) {
RouteSearchResponse.Lane lane = subPath.getLane().get(0);

Expand Down
45 changes: 45 additions & 0 deletions src/main/java/makar/dev/converter/UserConverter.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,49 @@
package makar.dev.converter;

import makar.dev.domain.Noti;
import makar.dev.domain.Route;
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();

if (notiList.isEmpty())
return UserResponse.HomeDto.builder()
.isRouteSet(isRouteSet)
.sourceStationName(null)
.destinationStationName(null)
.makarTime(null)
.getoffTime(null)
.notiList(null)
.favoriteRouteList(favoriteRouteDtoList)
.recentRouteList(recentRouteDtoList)
.build();

Route route = notiList.get(0).getRoute();
List<NotiResponse.NotiDto> notiDtoList = notiList.stream()
.map(NotiConverter::toNotiDto)
.toList();
return UserResponse.HomeDto.builder()
.isRouteSet(isRouteSet)
.sourceStationName(route.getSourceStation().getStationName())
.destinationStationName(route.getDestinationStation().getStationName())
.makarTime(route.getSchedule().getSourceTime())
.getoffTime(route.getSchedule().getDestinationTime())
.notiList(notiDtoList)
.favoriteRouteList(favoriteRouteDtoList)
.recentRouteList(recentRouteDtoList)
.build();
}
}
10 changes: 10 additions & 0 deletions src/main/java/makar/dev/dto/response/RouteResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ public static class RouteDetailDto {
private List<SubRouteDetailDto> subRouteDtoList;
}

@Data @Builder
@NoArgsConstructor
@AllArgsConstructor
public static class BriefRouteDto {
private String sourceStationName;
private String sourceLineNum;
private String destinationStationName;
private String destinationLineNum;
}

@Data
@Builder
@NoArgsConstructor
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/makar/dev/dto/response/UserResponse.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
package makar.dev.dto.response;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import makar.dev.domain.Noti;

import java.util.List;

public class UserResponse {

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class HomeDto {
private boolean isRouteSet;
private String sourceStationName;
private String destinationStationName;
private String makarTime;
private String getoffTime;
private List<NotiResponse.NotiDto> notiList;
private List<RouteResponse.BriefRouteDto> favoriteRouteList;
private List<RouteResponse.BriefRouteDto> recentRouteList;
}
}
37 changes: 32 additions & 5 deletions src/main/java/makar/dev/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package makar.dev.service;

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.UserConverter;
import makar.dev.domain.Noti;
import makar.dev.domain.Route;
import makar.dev.domain.User;
import makar.dev.dto.response.UserResponse;
import makar.dev.manager.DataManager;
import makar.dev.repository.UserRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
Expand Down Expand Up @@ -42,11 +50,6 @@ public void initDatabase(){
// dataManager.saveReverseTransferInfo();
}

public void tmpCreateUser(String userName){
User user = new User(userName);
userRepository.save(user);
}

public Optional<User> getOptionalUserById(String id) {
return userRepository.findById(id);
}
Expand All @@ -64,4 +67,28 @@ public User createUser(String id, String password, String email, String username

return user;
}


// 홈 화면 조회
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);
}

private User findUserById(Long userId){
return userRepository.findById(userId)
.orElseThrow(() -> new GeneralException(ErrorStatus.NOT_FOUND_USER));
}

private boolean isRouteSet(User user){
List<Noti> notiList = user.getNotiList();
return !notiList.isEmpty();
}


}

0 comments on commit db6fd60

Please sign in to comment.