Skip to content

Commit

Permalink
Merge pull request #131 from MYONGSIK/infra/monitoring
Browse files Browse the repository at this point in the history
refactor: ํด๋ฐ”์…‹ ์ถ”๊ฐ€ ์ผ์ฃผ์ผ ๋‹จ์œ„ ์ถ”๊ฐ€ ๋กœ์ง ๋ฆฌํŒฉํ† ๋ง
  • Loading branch information
Qbeom0925 authored Aug 19, 2023
2 parents ed1ce85 + d0c768d commit c08013f
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 77 deletions.
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

0 comments on commit c08013f

Please sign in to comment.