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

refactor: 폴바셋 추가 일주일 단위 추가 로직 리팩토링 #131

Merged
merged 1 commit into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public interface DinnerRepository extends JpaRepository<Dinner,Long> {
List<Dinner> findByWeek(Week week);




}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,7 +32,7 @@ public class MealController {
*/
@GetMapping("/week/{area}")
@ApiOperation(value = "음식 주 단위 조회")
public ApplicationResponse<List<MealResponse>> getWeekFoods(@PathVariable String area){
public ApplicationResponse<List<MealResponse>> getWeekFoods(@PathVariable String area) {
return ApplicationResponse.ok(mealService.getWeekFoods(area));
}

Expand All @@ -38,43 +42,47 @@ public ApplicationResponse<List<MealResponse>> getWeekFoods(@PathVariable String
@GetMapping("/{area}")
@ApiOperation(value = "음식 일 단위 조회")
@ApiResponses(value = {
@ApiResponse(code = 400, message = "(F0000) \n 공휴일은 식당을 운영하지 않습니다.")
@ApiResponse(code = 400, message = "(F0000) \n 공휴일은 식당을 운영하지 않습니다.")
})
public ApplicationResponse<List<MealResponse>> getDaysFoods(@PathVariable(value = "area") String area){
public ApplicationResponse<List<MealResponse>> getDaysFoods(
@PathVariable(value = "area") String area) {
return ApplicationResponse.ok(mealService.getDaysFoods(area));
}


@PostMapping("")
@ApiOperation(value = "음식 추가")
@ApiResponses(value = {
@ApiResponse(code = 400, message = "(F0000) \n 공휴일은 식당을 운영하지 않습니다.")
@ApiResponse(code = 400, message = "(F0000) \n 공휴일은 식당을 운영하지 않습니다.")
})
public ApplicationResponse<Boolean> createMeal(@RequestBody @Valid MealCreateReq mealCreateReq){
public ApplicationResponse<Boolean> createMeal(
@RequestBody @Valid MealCreateReq mealCreateReq) {
return ApplicationResponse.ok(mealService.createMeal(mealCreateReq));
}

@PostMapping("/area")
@ApiOperation(value = "지역 추가")
public ApplicationResponse<Boolean> createArea(String area){
public ApplicationResponse<Boolean> createArea(String area) {
return ApplicationResponse.ok(mealService.createArea(area));
}

@PostMapping("/week")
@ApiOperation(value = "주 단위 추가")
public ApplicationResponse<Boolean> createWeek(String startDay, String endDay){
return ApplicationResponse.ok(mealService.createWeek(startDay,endDay));
public ApplicationResponse<Boolean> createWeek(String startDay, String endDay) {
return ApplicationResponse.ok(mealService.createWeek(startDay, endDay));
}

@PostMapping("/once")
@ApiOperation(value = "주 단위 '등록된 식당 내용이 없습니다.' 등록하기")
public ApplicationResponse<Boolean> notRegisterMeal(@RequestBody MealNotRegisterReq mealNotRegisterReq){
public ApplicationResponse<Boolean> notRegisterMeal(
@RequestBody MealNotRegisterReq mealNotRegisterReq) {
return ApplicationResponse.ok(mealService.notRegisterMeal(mealNotRegisterReq));
}

@PostMapping("/evaluate")
@ApiOperation(value = "해당 식단에 대해서 좋아요/싫어요를 남깁니다.")
public ApplicationResponse<Boolean> evaluate(@RequestBody @Valid MealEvaluateReq mealEvaluateReq){
public ApplicationResponse<Boolean> evaluate(
@RequestBody @Valid MealEvaluateReq mealEvaluateReq) {
return ApplicationResponse.ok(mealService.evaluate(mealEvaluateReq));
}

Expand All @@ -83,7 +91,8 @@ public ApplicationResponse<Boolean> evaluate(@RequestBody @Valid MealEvaluateReq
*/
@GetMapping("/week/android/{area}")
@ApiOperation(value = "음식 주 단위 조회")
public ApplicationResponse<List<List<MealResponse>>> getWeekMealAndroid(@PathVariable String area){
public ApplicationResponse<List<List<MealResponse>>> getWeekMealAndroid(
@PathVariable String area) {
return ApplicationResponse.ok(mealService.getWeekMealAndroid(area));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -30,38 +37,47 @@ public class MealServiceImpl implements MealService {

@Override
public List<MealResponse> 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<MealResponse> 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;
}
Expand All @@ -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<Meal> 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<Meal> meals, int i, Week week, int j) {
private void addMealsIfApplicable(MealNotRegisterReq mealNotRegisterReq, Area area, List<Meal> 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<Meal> 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<List<MealResponse>> 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<List<MealResponse>> mealResponses = new ArrayList<>();
//하루씩 불러오기
for(int i = 0; i < 5; i++){
for (int i = 0; i < 5; i++) {
LocalDate localDate = week.getStartDay().plusDays(i);
List<Meal> byAreaAndWeek = mealRepository.findByAreaAndAndOfferedAt(areaEntity, localDate);
for (int j = 0; j < byAreaAndWeek.size(); j++){
List<Meal> byAreaAndWeek = mealRepository.findByAreaAndAndOfferedAt(areaEntity,
localDate);
for (int j = 0; j < byAreaAndWeek.size(); j++) {
mealResponses.add((MealResponse.toEntity(byAreaAndWeek)));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,6 +22,7 @@ public class Review extends BaseEntity {
private String registeredAt;

private String content;

private String area;

@ManyToOne(fetch = FetchType.LAZY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading