Skip to content

Commit

Permalink
Merge pull request #98 from everymeals/fix/meal-service
Browse files Browse the repository at this point in the history
[Fix/meal service] universityIdx 통일화 작업
  • Loading branch information
dldmsql authored Feb 18, 2024
2 parents 983f82b + 12cb46e commit 41d540a
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 110 deletions.
36 changes: 14 additions & 22 deletions src/main/java/everymeal/server/meal/controller/MealController.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -46,16 +47,13 @@ public ApplicationResponse<Boolean> createRestaurant(
* 학생식당 조회 API
* ============================================================================================
*/
@GetMapping("/restaurant")
@GetMapping("/restaurant/{campusIdx}")
@Operation(summary = "학교별 학생 식당 목록 조회")
public ApplicationResponse<List<RestaurantListGetRes>> getRestaurants(
@Schema(title = "대학 이름", defaultValue = "명지대학교", example = "명지대학교")
@RequestParam(value = "universityName")
String universityName,
@Schema(title = "캠퍼스 이름", defaultValue = "인문캠퍼스", example = "인문캠퍼스")
@RequestParam(value = "campusName")
String campusName) {
return ApplicationResponse.ok(mealService.getRestaurantList(universityName, campusName));
@Schema(title = "대학 캠퍼스 IDX", defaultValue = "1", example = "1")
@PathVariable(value = "campusIdx")
Long campusIdx) {
return ApplicationResponse.ok(mealService.getRestaurantList(campusIdx));
}

/**
Expand All @@ -70,31 +68,25 @@ public ApplicationResponse<Boolean> createWeekMeal(
return ApplicationResponse.create(mealService.createWeekMeal(weekMealRegisterReq));
}

@GetMapping("/day/v2")
@GetMapping("/day/{campusIdx}")
@Operation(summary = "당일 식단 조회")
public ApplicationResponse<Map<String, Map<String, List<DayMealGetRes>>>> getDayMealV2(
@RequestParam @Schema(description = "학교 이름", defaultValue = "명지대학교")
String universityName,
@RequestParam @Schema(description = "캠퍼스 이름", defaultValue = "인문캠퍼스") String campusName,
public ApplicationResponse<Map<String, Map<String, List<DayMealGetRes>>>> getDayMeal(
@PathVariable @Schema(description = "대학교 캠퍼스 idx", defaultValue = "1") Long campusIdx,
@RequestParam
@Schema(description = "조회하고자 하는 날짜 ( yyyy-MM-dd )", defaultValue = "2023-10-01")
String offeredAt) {
return ApplicationResponse.ok(
mealService.getDayMealListV2(universityName, campusName, offeredAt));
return ApplicationResponse.ok(mealService.getDayMealList(campusIdx, offeredAt));
}

@GetMapping("/week/v2")
@GetMapping("/week/{campusIdx}")
@Operation(summary = "주간 식단 조회")
public ApplicationResponse<List<Map<String, Map<String, List<DayMealGetRes>>>>> getWeekMealV2(
@RequestParam @Schema(description = "학교 이름", defaultValue = "명지대학교")
String universityName,
@RequestParam @Schema(description = "캠퍼스 이름", defaultValue = "인문캠퍼스") String campusName,
public ApplicationResponse<List<Map<String, Map<String, List<DayMealGetRes>>>>> getWeekMeal(
@PathVariable @Schema(description = "대학교 캠퍼스 idx", defaultValue = "1") Long campusIdx,
@RequestParam
@Schema(
description = "조회하고자 하는 시작 날짜 ( yyyy-MM-dd )",
defaultValue = "2023-10-01")
String offeredAt) {
return ApplicationResponse.ok(
mealService.getWeekMealList(universityName, campusName, offeredAt));
return ApplicationResponse.ok(mealService.getWeekMealList(campusIdx, offeredAt));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
import java.time.LocalTime;

public record RestaurantRegisterReq(
@Schema(title = "대학 이름", description = "학교 한글명", defaultValue = "명지대학교") @NotBlank
String universityName,
@Schema(title = "캠퍼스 이름", description = "학교 캠퍼스명", defaultValue = "인문캠퍼스") @NotBlank
String campusName,
@Schema(title = "대학 캠퍼스 아이디", description = "대학 캠퍼스 아이디", defaultValue = "1")
Long campusIdx,
@Schema(
title = "학교 주소",
description = "시/구/동 도로명 주소 모두 기입",
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/everymeal/server/meal/entity/Restaurant.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class Restaurant {

@Builder
public Restaurant(RestaurantRegisterReq restaurantRegisterReq, University university) {
this.name = restaurantRegisterReq.campusName();
this.name = restaurantRegisterReq.restaurantName();
this.address = restaurantRegisterReq.address();
this.university = university;
this.isDeleted = Boolean.TRUE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
@Repository
public interface MealMapper {
List<Map<String, Object>> findDayList(
@Param("offeredAt") String offeredAt,
@Param("universityName") String universityName,
@Param("campusName") String campusName);
@Param("offeredAt") String offeredAt, @Param("universityIdx") Long universityIdx);

List<Meal> findAllByOfferedAtOnDateAndMealType(
@Param("offeredAt") String offeredAt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public void saveAll(List<Meal> mealList) {
mealDao.saveAll(mealList);
}

public List<Map<String, Object>> getDayList(
String offeredAt, String universityName, String campusName) {
return mealMapper.findDayList(offeredAt, universityName, campusName);
public List<Map<String, Object>> getDayList(String offeredAt, Long universityIdx) {
return mealMapper.findDayList(offeredAt, universityIdx);
}
}
8 changes: 4 additions & 4 deletions src/main/java/everymeal/server/meal/service/MealService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ public interface MealService {

Boolean createWeekMeal(WeekMealRegisterReq weekMealRegisterReq);

List<RestaurantListGetRes> getRestaurantList(String universityName, String campusName);
List<RestaurantListGetRes> getRestaurantList(Long universityIdx);

Map<String, Map<String, List<DayMealGetRes>>> getDayMealListV2(
String universityName, String campusName, String offeredAt);
Map<String, Map<String, List<DayMealGetRes>>> getDayMealList(
Long universityIdx, String offeredAt);

List<Map<String, Map<String, List<DayMealGetRes>>>> getWeekMealList(
String universityName, String campusName, String offeredAt);
Long universityIdx, String offeredAt);

Boolean createRestaurant(RestaurantRegisterReq restaurantRegisterReq);
}
17 changes: 7 additions & 10 deletions src/main/java/everymeal/server/meal/service/MealServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,23 @@ public Boolean createWeekMeal(WeekMealRegisterReq request) {
}

@Override
public List<RestaurantListGetRes> getRestaurantList(String universityName, String campusName) {
public List<RestaurantListGetRes> getRestaurantList(Long universityIdx) {
// 학교와 식당 폐업 여부를 키로 조회
List<Restaurant> restaurants =
restaurantServiceImpl.getAllByUniversityAndIsDeletedFalse(
universityName, campusName);
restaurantServiceImpl.getAllByUniversityAndIsDeletedFalse(universityIdx);
return RestaurantListGetRes.of(restaurants);
}

@Override
public Map<String, Map<String, List<DayMealGetRes>>> getDayMealListV2(
String universityName, String campusName, String offeredAt) {
var result = mealCommServiceImpl.getDayList(offeredAt, universityName, campusName);
public Map<String, Map<String, List<DayMealGetRes>>> getDayMealList(
Long universityIdx, String offeredAt) {
var result = mealCommServiceImpl.getDayList(offeredAt, universityIdx);
return Map.of(offeredAt, DayMealGetRes.of(result));
}

@Override
public List<Map<String, Map<String, List<DayMealGetRes>>>> getWeekMealList(
String universityName, String campusName, String offeredAt) { // 현재 날짜와 시간을 가져옵니다.
Long universityIdx, String offeredAt) { // 현재 날짜와 시간을 가져옵니다.
LocalDate ldOfferedAt = LocalDate.parse(offeredAt);

// 현재 요일을 가져옵니다.
Expand All @@ -98,9 +97,7 @@ public List<Map<String, Map<String, List<DayMealGetRes>>>> getWeekMealList(
List<Map<String, Map<String, List<DayMealGetRes>>>> result = new ArrayList<>();
for (LocalDate i = monday; i.isBefore(sunday); i = i.plusDays(1)) {
Map<String, List<DayMealGetRes>> row =
DayMealGetRes.of(
mealCommServiceImpl.getDayList(
i.toString(), universityName, campusName));
DayMealGetRes.of(mealCommServiceImpl.getDayList(i.toString(), universityIdx));
result.add(Map.of(i.toString(), row));
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ public interface RestaurantService {

Boolean createRestaurant(RestaurantRegisterReq restaurantRegisterReq);

List<Restaurant> getAllByUniversityAndIsDeletedFalse(String universityName, String campusName);
List<Restaurant> getAllByUniversityAndIsDeletedFalse(Long universityIdx);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public Restaurant getRestaurant(Long restaurantIdx) {
public Boolean createRestaurant(RestaurantRegisterReq restaurantRegisterReq) {
// 학교 조회
University university =
universityServiceImpl.getUniversity(
restaurantRegisterReq.universityName(), restaurantRegisterReq.campusName());
universityServiceImpl.getUniversity(restaurantRegisterReq.campusIdx());
// 식당 등록
Restaurant restaurant =
Restaurant.builder()
Expand All @@ -44,10 +43,9 @@ public Boolean createRestaurant(RestaurantRegisterReq restaurantRegisterReq) {
}

@Override
public List<Restaurant> getAllByUniversityAndIsDeletedFalse(
String universityName, String campusName) {
public List<Restaurant> getAllByUniversityAndIsDeletedFalse(Long universityIdx) {
// 학교 등록 여부 판단
University university = universityServiceImpl.getUniversity(universityName, campusName);
University university = universityServiceImpl.getUniversity(universityIdx);

return restaurantCommServiceImpl.getAllByUniversityAndIsDeletedFalse(university);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public interface UniversityRepository extends JpaRepository<University, Long> {
List<University> findByNameAndCampusNameAndIsDeletedFalse(
String universityName, String campusName);

List<University> findByIdxAndIsDeletedFalse(Long idx);

List<University> findAllByIsDeletedFalse();
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ public interface UniversityService {

List<UniversityListGetRes> getUniversities();

University getUniversity(String universityName, String campusName);
University getUniversity(Long universityIdx);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ public List<UniversityListGetRes> getUniversities() {
return UniversityListGetRes.of(universities);
}

public University getUniversity(String universityName, String campusName) {
return universityRepository
.findByNameAndCampusNameAndIsDeletedFalse(universityName, campusName)
.stream()
public University getUniversity(Long universityIdx) {
return universityRepository.findByIdxAndIsDeletedFalse(universityIdx).stream()
.findFirst()
.orElseThrow(() -> new ApplicationException(ExceptionList.UNIVERSITY_NOT_FOUND));
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/mybatis/mappers/MealMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
r.name AS restaurantName,
CONCAT(u.name, ' ', u.campus_name) AS universityName,
r.review_count AS reviewCount,
r.grade AS grade
ROUND(r.grade,1) AS grade
FROM restaurant r
JOIN (SELECT DISTINCT meal_type AS mealType FROM meal) AS meal_types ON 1=1
LEFT JOIN meal m ON m.restaurant_idx = r.idx AND m.meal_type = meal_types.mealType AND m.offered_at = DATE(#{offeredAt})
INNER JOIN university u ON r.university_idx = u.idx AND u.is_deleted = false
WHERE
u.name = #{universityName} AND u.campus_name = #{campusName}
u.idx = #{universityIdx}
AND (
(meal_types.mealType = 'BREAKFAST' AND r.is_open_breakfast = true) OR
(meal_types.mealType = 'LUNCH' AND r.is_open_lunch = true) OR
Expand Down
16 changes: 14 additions & 2 deletions src/test/java/everymeal/server/meal/MealData.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,20 @@ public static MealRegisterReq getMealRegisterReq(int i) {

public static RestaurantRegisterReq getRestaurantRegisterReq() {
return new RestaurantRegisterReq(
"명지대학교",
"인문캠퍼스",
1L,
"서울시 서대문구 거북골로 34",
"MCC 식당",
LocalTime.of(8, 0),
LocalTime.of(10, 30),
LocalTime.of(11, 0),
LocalTime.of(14, 30),
LocalTime.of(17, 0),
LocalTime.of(18, 30));
}

public static RestaurantRegisterReq getRestaurantRegisterReq(Long universityIdx) {
return new RestaurantRegisterReq(
universityIdx,
"서울시 서대문구 거북골로 34",
"MCC 식당",
LocalTime.of(8, 0),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,9 @@ void createRestaurant() throws Exception {
@Test
void getRestaurants() throws Exception {
// given
String universityName = "명지대학교";
String campusName = "인문캠퍼스";

// when-then
mockMvc.perform(
get("/api/v1/meals/restaurant")
.param("universityName", universityName)
.param("campusName", campusName)
get("/api/v1/meals/restaurant/{campusIdx}", 1)
.contentType(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk());
Expand All @@ -85,15 +80,11 @@ void getRestaurants() throws Exception {
@Test
void getDayMeal() throws Exception {
// given
String universityName = "명지대학교";
String campusName = "인문캠퍼스";
String offeredAt = "2023-10-01";

// when-then
mockMvc.perform(
get("/api/v1/meals/day/v2")
.param("universityName", universityName)
.param("campusName", campusName)
get("/api/v1/meals/day/{campusIdx}", 1)
.param("offeredAt", offeredAt)
.contentType(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultHandlers.print())
Expand All @@ -104,14 +95,11 @@ void getDayMeal() throws Exception {
@Test
void getWeekMeal() throws Exception {
// given
String universityName = "명지대학교";
String offeredAt = "2023-10-01";

// when-then
mockMvc.perform(
get("/api/v1/meals/week/v2")
.param("universityName", universityName)
.param("campusName", "인문캠퍼스")
get("/api/v1/meals/week/{campusIdx}", 1)
.param("offeredAt", offeredAt)
.contentType(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultHandlers.print())
Expand All @@ -120,8 +108,7 @@ void getWeekMeal() throws Exception {

private RestaurantRegisterReq getRestaurantRegisterReq() {
return new RestaurantRegisterReq(
"명지대학교",
"인문캠퍼스",
1L,
"서울시 서대문구 남가좌동 거북골로 34",
"MCC 식당",
LocalTime.of(8, 0),
Expand Down
Loading

0 comments on commit 41d540a

Please sign in to comment.