diff --git a/src/main/java/com/example/myongsick/MyongsickApplication.java b/src/main/java/com/example/myongsick/MyongsickApplication.java index 1479509..354783a 100644 --- a/src/main/java/com/example/myongsick/MyongsickApplication.java +++ b/src/main/java/com/example/myongsick/MyongsickApplication.java @@ -1,5 +1,6 @@ package com.example.myongsick; +import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.PropertySource; diff --git a/src/main/java/com/example/myongsick/domain/food/repository/DinnerRepository.java b/src/main/java/com/example/myongsick/domain/food/repository/DinnerRepository.java index 2c575bf..b6096c6 100644 --- a/src/main/java/com/example/myongsick/domain/food/repository/DinnerRepository.java +++ b/src/main/java/com/example/myongsick/domain/food/repository/DinnerRepository.java @@ -15,4 +15,6 @@ public interface DinnerRepository extends JpaRepository { List findByWeek(Week week); + + } diff --git a/src/main/java/com/example/myongsick/domain/meal/controller/MealController.java b/src/main/java/com/example/myongsick/domain/meal/controller/MealController.java index a482a62..c8a20bf 100644 --- a/src/main/java/com/example/myongsick/domain/meal/controller/MealController.java +++ b/src/main/java/com/example/myongsick/domain/meal/controller/MealController.java @@ -10,11 +10,15 @@ import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; -import lombok.RequiredArgsConstructor; -import org.springframework.web.bind.annotation.*; - -import javax.validation.Valid; import java.util.List; +import javax.validation.Valid; +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; +import org.springframework.web.bind.annotation.RestController; @RestController @RequiredArgsConstructor @@ -28,7 +32,7 @@ public class MealController { */ @GetMapping("/week/{area}") @ApiOperation(value = "음식 주 단위 조회") - public ApplicationResponse> getWeekFoods(@PathVariable String area){ + public ApplicationResponse> getWeekFoods(@PathVariable String area) { return ApplicationResponse.ok(mealService.getWeekFoods(area)); } @@ -38,9 +42,10 @@ public ApplicationResponse> getWeekFoods(@PathVariable String @GetMapping("/{area}") @ApiOperation(value = "음식 일 단위 조회") @ApiResponses(value = { - @ApiResponse(code = 400, message = "(F0000) \n 공휴일은 식당을 운영하지 않습니다.") + @ApiResponse(code = 400, message = "(F0000) \n 공휴일은 식당을 운영하지 않습니다.") }) - public ApplicationResponse> getDaysFoods(@PathVariable(value = "area") String area){ + public ApplicationResponse> getDaysFoods( + @PathVariable(value = "area") String area) { return ApplicationResponse.ok(mealService.getDaysFoods(area)); } @@ -48,33 +53,36 @@ public ApplicationResponse> getDaysFoods(@PathVariable(value @PostMapping("") @ApiOperation(value = "음식 추가") @ApiResponses(value = { - @ApiResponse(code = 400, message = "(F0000) \n 공휴일은 식당을 운영하지 않습니다.") + @ApiResponse(code = 400, message = "(F0000) \n 공휴일은 식당을 운영하지 않습니다.") }) - public ApplicationResponse createMeal(@RequestBody @Valid MealCreateReq mealCreateReq){ + public ApplicationResponse createMeal( + @RequestBody @Valid MealCreateReq mealCreateReq) { return ApplicationResponse.ok(mealService.createMeal(mealCreateReq)); } @PostMapping("/area") @ApiOperation(value = "지역 추가") - public ApplicationResponse createArea(String area){ + public ApplicationResponse createArea(String area) { return ApplicationResponse.ok(mealService.createArea(area)); } @PostMapping("/week") @ApiOperation(value = "주 단위 추가") - public ApplicationResponse createWeek(String startDay, String endDay){ - return ApplicationResponse.ok(mealService.createWeek(startDay,endDay)); + public ApplicationResponse createWeek(String startDay, String endDay) { + return ApplicationResponse.ok(mealService.createWeek(startDay, endDay)); } @PostMapping("/once") @ApiOperation(value = "주 단위 '등록된 식당 내용이 없습니다.' 등록하기") - public ApplicationResponse notRegisterMeal(@RequestBody MealNotRegisterReq mealNotRegisterReq){ + public ApplicationResponse notRegisterMeal( + @RequestBody MealNotRegisterReq mealNotRegisterReq) { return ApplicationResponse.ok(mealService.notRegisterMeal(mealNotRegisterReq)); } @PostMapping("/evaluate") @ApiOperation(value = "해당 식단에 대해서 좋아요/싫어요를 남깁니다.") - public ApplicationResponse evaluate(@RequestBody @Valid MealEvaluateReq mealEvaluateReq){ + public ApplicationResponse evaluate( + @RequestBody @Valid MealEvaluateReq mealEvaluateReq) { return ApplicationResponse.ok(mealService.evaluate(mealEvaluateReq)); } @@ -83,7 +91,8 @@ public ApplicationResponse evaluate(@RequestBody @Valid MealEvaluateReq */ @GetMapping("/week/android/{area}") @ApiOperation(value = "음식 주 단위 조회") - public ApplicationResponse>> getWeekMealAndroid(@PathVariable String area){ + public ApplicationResponse>> getWeekMealAndroid( + @PathVariable String area) { return ApplicationResponse.ok(mealService.getWeekMealAndroid(area)); } diff --git a/src/main/java/com/example/myongsick/domain/meal/dto/request/MealCreateReq.java b/src/main/java/com/example/myongsick/domain/meal/dto/request/MealCreateReq.java index 307848f..aadf6a7 100644 --- a/src/main/java/com/example/myongsick/domain/meal/dto/request/MealCreateReq.java +++ b/src/main/java/com/example/myongsick/domain/meal/dto/request/MealCreateReq.java @@ -7,6 +7,7 @@ import lombok.NoArgsConstructor; import lombok.Setter; +import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; import java.time.LocalDate; import java.util.List; @@ -22,6 +23,7 @@ public class MealCreateReq { @ApiModelProperty(value = "중식A/중식B/석식", example = "LUNCH_A/LUNCH_B/DINNER", required = true, dataType = "String") @NotNull(message = "메뉴의 분류 값은 필수입니다.") + @NotBlank(message = "메뉴의 분류 값은 필수입니다.") private String type; //A형 B형 @ApiModelProperty(value = "운영/미운영", example = "OPEN/CLOSE", required = true, dataType = "String") diff --git a/src/main/java/com/example/myongsick/domain/meal/service/MealServiceImpl.java b/src/main/java/com/example/myongsick/domain/meal/service/MealServiceImpl.java index 1604c5c..981f099 100644 --- a/src/main/java/com/example/myongsick/domain/meal/service/MealServiceImpl.java +++ b/src/main/java/com/example/myongsick/domain/meal/service/MealServiceImpl.java @@ -1,23 +1,30 @@ package com.example.myongsick.domain.meal.service; -import com.example.myongsick.domain.meal.dto.request.MealEvaluateReq; -import com.example.myongsick.domain.meal.entity.*; -import com.example.myongsick.domain.meal.exception.excute.*; -import com.example.myongsick.domain.meal.repository.AreaRepository; import com.example.myongsick.domain.food.entity.Week; import com.example.myongsick.domain.food.repository.WeekRepository; import com.example.myongsick.domain.meal.dto.request.MealCreateReq; +import com.example.myongsick.domain.meal.dto.request.MealEvaluateReq; import com.example.myongsick.domain.meal.dto.request.MealNotRegisterReq; import com.example.myongsick.domain.meal.dto.response.MealResponse; +import com.example.myongsick.domain.meal.entity.Area; +import com.example.myongsick.domain.meal.entity.Meal; +import com.example.myongsick.domain.meal.entity.MealEvaluate; +import com.example.myongsick.domain.meal.entity.MealType; +import com.example.myongsick.domain.meal.entity.StatusType; +import com.example.myongsick.domain.meal.exception.excute.AlreadyAreaException; +import com.example.myongsick.domain.meal.exception.excute.NotFoundAreaException; +import com.example.myongsick.domain.meal.exception.excute.NotFoundWeekException; +import com.example.myongsick.domain.meal.exception.excute.NotOperatedException; +import com.example.myongsick.domain.meal.exception.excute.NotfoundMealException; +import com.example.myongsick.domain.meal.repository.AreaRepository; import com.example.myongsick.domain.meal.repository.MealRepository; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - import java.time.DayOfWeek; import java.time.LocalDate; import java.util.ArrayList; import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; @Service @RequiredArgsConstructor @@ -30,38 +37,47 @@ public class MealServiceImpl implements MealService { @Override public List getWeekFoods(String area) { - Week week = weekRepository.findByStartDayLessThanEqualAndEndDayGreaterThanEqual(LocalDate.now(), LocalDate.now()).get(); - return MealResponse.toEntity(mealRepository.findByWeekAndAreaOrderByArea(week, areaRepository.findByName(area).get())); + Week week = weekRepository.findByStartDayLessThanEqualAndEndDayGreaterThanEqual( + LocalDate.now(), + LocalDate.now()).get(); + return MealResponse.toEntity( + mealRepository.findByWeekAndAreaOrderByArea(week, + areaRepository.findByName(area).get())); } @Override public List getDaysFoods(String area) { DayOfWeek dayOfWeek = LocalDate.now().getDayOfWeek(); - if(dayOfWeek.equals(DayOfWeek.SATURDAY) || dayOfWeek.equals(DayOfWeek.SUNDAY)){ + if (dayOfWeek.equals(DayOfWeek.SATURDAY) || dayOfWeek.equals(DayOfWeek.SUNDAY)) { throw new NotOperatedException(); } - return MealResponse.toEntity(mealRepository.findByOfferedAtAndArea(LocalDate.now(),areaRepository.findByName(area).get())); + return MealResponse.toEntity(mealRepository.findByOfferedAtAndArea(LocalDate.now(), + areaRepository.findByName(area).get())); } @Override @Transactional public Boolean createMeal(MealCreateReq mealCreateReq) { mealRepository.save(Meal.builder() - .mealType(MealType.valueOf(mealCreateReq.getType())) - .area(areaRepository.findByName(mealCreateReq.getArea()).orElseThrow(NotFoundAreaException::new)) - .week(weekRepository.findByStartDayLessThanEqualAndEndDayGreaterThanEqual(mealCreateReq.getOfferedAt(), mealCreateReq.getOfferedAt()).orElseThrow( - NotFoundWeekException::new)) - .offeredAt(mealCreateReq.getOfferedAt()) - .statusType(StatusType.valueOf(mealCreateReq.getStatus())) - .menus(mealCreateReq.getMeals()) - .build()); + .mealType(MealType.valueOf(mealCreateReq.getType())) + .area(areaRepository.findByName(mealCreateReq.getArea()) + .orElseThrow(NotFoundAreaException::new)) + .week(weekRepository.findByStartDayLessThanEqualAndEndDayGreaterThanEqual( + mealCreateReq.getOfferedAt(), mealCreateReq.getOfferedAt()).orElseThrow( + NotFoundWeekException::new)) + .offeredAt(mealCreateReq.getOfferedAt()) + .statusType(StatusType.valueOf(mealCreateReq.getStatus())) + .menus(mealCreateReq.getMeals()) + .build()); return true; } @Override @Transactional public Boolean createArea(String area) { - if (areaRepository.findByName(area).isPresent()) throw new AlreadyAreaException(); + if (areaRepository.findByName(area).isPresent()) { + throw new AlreadyAreaException(); + } areaRepository.save(Area.builder().name(area).build()); return true; } @@ -70,68 +86,99 @@ public Boolean createArea(String area) { @Transactional public Boolean createWeek(String startDay, String endDay) { weekRepository.save(Week.builder() - .startDay(LocalDate.parse(startDay)) - .endDay(LocalDate.parse(endDay)) - .build()); + .startDay(LocalDate.parse(startDay)) + .endDay(LocalDate.parse(endDay)) + .build()); return true; } @Override @Transactional public Boolean notRegisterMeal(MealNotRegisterReq mealNotRegisterReq) { - Area area = areaRepository.findByName(mealNotRegisterReq.getArea()).orElseThrow(NotFoundAreaException::new); + Area area = areaRepository.findByName(mealNotRegisterReq.getArea()) + .orElseThrow(NotFoundAreaException::new); List meals = new ArrayList<>(); - for(int i = 0; i < 5; i++){ - Week week = weekRepository.findByStartDayLessThanEqualAndEndDayGreaterThanEqual(mealNotRegisterReq.getStartedAt().plusDays(i), mealNotRegisterReq.getStartedAt().plusDays(i)).orElseThrow(NotFoundAreaException::new); - for(int j = 0; j < MealType.values().length; j++){ - if (!area.getName().equals("MCC식당") && !area.getName().equals("명진당식당") && MealType.values()[j].equals(MealType.LUNCH_B)){ - continue; - } - addMeals(mealNotRegisterReq, area, meals, i, week, j); + for (int i = 0; i < 5; i++) { + Week week = weekRepository.findByStartDayLessThanEqualAndEndDayGreaterThanEqual( + mealNotRegisterReq.getStartedAt().plusDays(i), + mealNotRegisterReq.getStartedAt().plusDays(i)) + .orElseThrow(NotFoundAreaException::new); + switch (area.getName()) { + case "MCC식당": + case "명진당식당": + addMealsIfApplicable(mealNotRegisterReq, area, meals, i, week, MealType.LUNCH_A, MealType.LUNCH_B, MealType.DINNER); + break; + case "교직원식당": + case "생활관식당": + case "학생식당": + addMealsIfApplicable(mealNotRegisterReq, area, meals, i, week, MealType.LUNCH_A, MealType.DINNER); + break; + case "폴바셋": + addMealsIfApplicable(mealNotRegisterReq, area, meals, i, week, MealType.LUNCH_A); + break; } } mealRepository.saveAll(meals); return true; } - private void addMeals(MealNotRegisterReq mealNotRegisterReq, Area area, List meals, int i, Week week, int j) { + private void addMealsIfApplicable(MealNotRegisterReq mealNotRegisterReq, Area area, List meals, int i, Week week, MealType... mealTypes) { + for (MealType mealType : mealTypes) { + addMeals(mealNotRegisterReq, area, meals, i, week, mealType); + } + } + + private void addMeals(MealNotRegisterReq mealNotRegisterReq, Area area, List meals, int i, + Week week, MealType mealType) { meals.add( - Meal.builder() - .area(area) - .week(week) - .mealType(MealType.values()[j]) - .menus(List.of("","등록된 식단내용이(가) 없습니다.","","","","")) - .offeredAt(mealNotRegisterReq.getStartedAt().plusDays(i)) - .statusType(StatusType.OPEN) - .build() + Meal.builder() + .area(area) + .week(week) + .mealType(mealType) + .menus(List.of("", "등록된 식단내용이(가) 없습니다.", "", "", "", "")) + .offeredAt(mealNotRegisterReq.getStartedAt().plusDays(i)) + .statusType(StatusType.OPEN) + .build() ); } @Override @Transactional public Boolean evaluate(MealEvaluateReq mealEvaluateReq) { - Meal meal = mealRepository.findById(mealEvaluateReq.getMealId()).orElseThrow(NotfoundMealException::new); - if (mealEvaluateReq.getMealEvaluate().equals(MealEvaluate.LOVE)){ - if (mealEvaluateReq.getCalculation().equals("plus")) meal.addLove(); - if (mealEvaluateReq.getCalculation().equals("minus")) meal.reduceLove(); + Meal meal = mealRepository.findById(mealEvaluateReq.getMealId()) + .orElseThrow(NotfoundMealException::new); + if (mealEvaluateReq.getMealEvaluate().equals(MealEvaluate.LOVE)) { + if (mealEvaluateReq.getCalculation().equals("plus")) { + meal.addLove(); + } + if (mealEvaluateReq.getCalculation().equals("minus")) { + meal.reduceLove(); + } } - if (mealEvaluateReq.getMealEvaluate().equals(MealEvaluate.HATE)){ - if (mealEvaluateReq.getCalculation().equals("plus")) meal.addHate(); - if (mealEvaluateReq.getCalculation().equals("minus")) meal.reduceHate(); + if (mealEvaluateReq.getMealEvaluate().equals(MealEvaluate.HATE)) { + if (mealEvaluateReq.getCalculation().equals("plus")) { + meal.addHate(); + } + if (mealEvaluateReq.getCalculation().equals("minus")) { + meal.reduceHate(); + } } return true; } @Override public List> getWeekMealAndroid(String area) { - Week week = weekRepository.findByStartDayLessThanEqualAndEndDayGreaterThanEqual(LocalDate.now(), LocalDate.now()).get(); + Week week = weekRepository.findByStartDayLessThanEqualAndEndDayGreaterThanEqual( + LocalDate.now(), + LocalDate.now()).get(); Area areaEntity = areaRepository.findByName(area).get(); List> mealResponses = new ArrayList<>(); //하루씩 불러오기 - for(int i = 0; i < 5; i++){ + for (int i = 0; i < 5; i++) { LocalDate localDate = week.getStartDay().plusDays(i); - List byAreaAndWeek = mealRepository.findByAreaAndAndOfferedAt(areaEntity, localDate); - for (int j = 0; j < byAreaAndWeek.size(); j++){ + List byAreaAndWeek = mealRepository.findByAreaAndAndOfferedAt(areaEntity, + localDate); + for (int j = 0; j < byAreaAndWeek.size(); j++) { mealResponses.add((MealResponse.toEntity(byAreaAndWeek))); } } diff --git a/src/main/java/com/example/myongsick/domain/review/entity/Review.java b/src/main/java/com/example/myongsick/domain/review/entity/Review.java index c0e94b9..168f309 100644 --- a/src/main/java/com/example/myongsick/domain/review/entity/Review.java +++ b/src/main/java/com/example/myongsick/domain/review/entity/Review.java @@ -2,14 +2,9 @@ import com.example.myongsick.domain.user.entity.User; import com.example.myongsick.global.entity.BaseEntity; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.FetchType; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; + +import javax.persistence.*; + import lombok.AccessLevel; import lombok.Builder; import lombok.Getter; @@ -27,6 +22,7 @@ public class Review extends BaseEntity { private String registeredAt; private String content; + private String area; @ManyToOne(fetch = FetchType.LAZY) diff --git a/src/main/java/com/example/myongsick/global/exception/ApplicationException.java b/src/main/java/com/example/myongsick/global/exception/ApplicationException.java index 1a33ff5..8a9d4af 100644 --- a/src/main/java/com/example/myongsick/global/exception/ApplicationException.java +++ b/src/main/java/com/example/myongsick/global/exception/ApplicationException.java @@ -4,12 +4,12 @@ import org.springframework.http.HttpStatus; @Getter -public abstract class ApplicationException extends RuntimeException{ +public class ApplicationException extends RuntimeException{ private final String errorCode; private final HttpStatus httpStatus; - protected ApplicationException(String errorCode, HttpStatus httpStatus, String message){ + public ApplicationException(String errorCode, HttpStatus httpStatus, String message){ super(message); this.errorCode = errorCode; this.httpStatus = httpStatus;