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

[Fix/meal service] universityIdx 통일화 작업 #98

Merged
merged 3 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
38 changes: 16 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/{universityIdx}")
@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 = "universityIdx")
Long universityIdx) {
return ApplicationResponse.ok(mealService.getRestaurantList(universityIdx));
}

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

@GetMapping("/day/v2")
@GetMapping("/day/{universityIdx}")
@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 universityIdx,
@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(universityIdx, offeredAt));
}

@GetMapping("/week/v2")
@GetMapping("/week/{universityIdx}")
@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 universityIdx,
@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(universityIdx, 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 universityIdx,
@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.universityIdx());
// 식당 등록
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/{universityIdx}", 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/{universityIdx}", 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/{universityIdx}", 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
Loading