Skip to content

Commit

Permalink
Merge pull request #15 from CAUSOLDOUTMEN/feature/9-feat-foodService
Browse files Browse the repository at this point in the history
feat: FoodService 기본 로직 구현 (#9)
  • Loading branch information
win-luck authored Oct 5, 2023
2 parents 8d37c57 + e48cf96 commit 60e12ed
Show file tree
Hide file tree
Showing 9 changed files with 337 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.diareat.diareat.food.dto;

import com.diareat.diareat.food.domain.FavoriteFood;
import com.diareat.diareat.user.domain.BaseNutrition;
import lombok.AllArgsConstructor;
import lombok.Getter;

Expand All @@ -10,18 +11,18 @@ public class ResponseFavoriteFoodDto {

private Long favoriteFoodId;
private String name;
private String baseNutrition;
private BaseNutrition baseNutrition;
private int count;

public static ResponseFavoriteFoodDto of(Long favoriteFoodId, String name, String baseNutrition, int count) {
public static ResponseFavoriteFoodDto of(Long favoriteFoodId, String name, BaseNutrition baseNutrition, int count) {
return new ResponseFavoriteFoodDto(favoriteFoodId, name, baseNutrition, count);
}

public static ResponseFavoriteFoodDto from(FavoriteFood favoriteFood) {
return new ResponseFavoriteFoodDto(
favoriteFood.getId(),
favoriteFood.getName(),
favoriteFood.getBaseNutrition().toString(),
favoriteFood.getBaseNutrition(),
favoriteFood.getCount()
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.diareat.diareat.food.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class ResponseNutritionSumByDateDto {
int totalKcal;
int totalCarbohydrate;
int totalProtein;
int totalFat;

double ratioKcal;
double ratioCarbohydrate;
double ratioProtein;
double ratioFat;

public static ResponseNutritionSumByDateDto of (int totalKcal, int totalCarbohydrate, int totalProtein, int totalFat, double ratioKcal, double ratioCarbohydrate, double ratioProtein, double ratioFat){
return new ResponseNutritionSumByDateDto(totalKcal, totalCarbohydrate, totalProtein, totalFat, ratioKcal, ratioCarbohydrate, ratioProtein, ratioFat);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
import com.diareat.diareat.food.domain.FavoriteFood;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface FavoriteFoodRepository extends JpaRepository<FavoriteFood, Long> {
List<FavoriteFood> findAllByUserId(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
import com.diareat.diareat.food.domain.Food;
import org.springframework.data.jpa.repository.JpaRepository;

import java.time.LocalDate;
import java.util.List;

public interface FoodRepository extends JpaRepository<Food, Long> {
List<Food> findAllByUserIdAndDate(Long userId, LocalDate date); //유저가 특정 날짜에 먹은 음식 반환
}
136 changes: 125 additions & 11 deletions src/main/java/com/diareat/diareat/food/service/FoodService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
package com.diareat.diareat.food.service;

import com.diareat.diareat.food.domain.FavoriteFood;
import com.diareat.diareat.food.domain.Food;
import com.diareat.diareat.food.dto.*;
import com.diareat.diareat.food.repository.FavoriteFoodRepository;
import com.diareat.diareat.food.repository.FoodRepository;
import com.diareat.diareat.user.domain.BaseNutrition;
import com.diareat.diareat.user.domain.User;
import com.diareat.diareat.user.repository.UserRepository;
import com.diareat.diareat.util.api.ResponseCode;
import com.diareat.diareat.util.exception.FoodException;
import com.diareat.diareat.util.exception.UserException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
Expand All @@ -12,36 +26,136 @@ public class FoodService {
private final FoodRepository foodRepository; // 유저:음식은 1:다
private final FavoriteFoodRepository favoriteFoodRepository; // 유저:즐찾음식은 1:다

private final UserRepository userRepository;

// 촬영 후, 음식 정보 저장
public Long saveFood() {
return null;
@Transactional
public Long saveFood(CreateFoodDto createFoodDto) {
User user = getUserById(createFoodDto.getUserId());
Food food = Food.createFood(createFoodDto.getName(), user, createFoodDto.getBaseNutrition());
return foodRepository.save(food).getId();
}

// 회원이 특정 날짜에 먹은 음식 반환
@Transactional(readOnly = true)
public List<ResponseFoodDto> getFoodListByDate(Long userId, LocalDate date){
List<Food> foodList = foodRepository.findAllByUserIdAndDate(userId, date);
return foodList.stream()
.map(food -> ResponseFoodDto.of(food.getId(), food.getUser().getId(), food.getName(), food.getDate(), food.getTime(), food.getBaseNutrition(), food.isFavorite())).collect(Collectors.toList());
}

// 음식 정보 수정
public void updateFood() {

// 음식 정보 수정
@Transactional
public void updateFood(UpdateFoodDto updateFoodDto) {
Food food = getFoodById(updateFoodDto.getFoodId());
food.updateFood(updateFoodDto.getName(), updateFoodDto.getBaseNutrition());
}

// 음식 삭제
public void deleteFood() {

@Transactional
public void deleteFood(Long foodId) {
foodRepository.deleteById(foodId);
}

// 즐겨찾기에 음식 저장
public Long saveFavoriteFood() {
return null;
@Transactional
public Long saveFavoriteFood(CreateFavoriteFoodDto createFavoriteFoodDto) {

User user = getUserById(createFavoriteFoodDto.getUserId());

FavoriteFood favoriteFood = FavoriteFood.createFavoriteFood(createFavoriteFoodDto.getName(), user, createFavoriteFoodDto.getBaseNutrition());
return favoriteFoodRepository.save(favoriteFood).getId();
}

// 즐겨찾기 음식 수정
public void updateFavoriteFood() {
//즐겨찾기 음식 리스트 반환
@Transactional(readOnly = true)
public List<ResponseFavoriteFoodDto> getFavoriteFoodList(Long userId){
List<FavoriteFood> foodList = favoriteFoodRepository.findAllByUserId(userId);
return foodList.stream()
.map(favoriteFood -> ResponseFavoriteFoodDto.of(favoriteFood.getId(), favoriteFood.getName(),
favoriteFood.getBaseNutrition(), favoriteFood.getCount())).collect(Collectors.toList());
}

// 즐겨찾기 음식 수정
@Transactional
public void updateFavoriteFood(UpdateFavoriteFoodDto updateFavoriteFoodDto) {
FavoriteFood food = getFavoriteFoodById(updateFavoriteFoodDto.getFavoriteFoodId());
food.updateFavoriteFood(updateFavoriteFoodDto.getName(), updateFavoriteFoodDto.getBaseNutrition());
}

// 즐겨찾기 해제
public void deleteFavoriteFood() {
@Transactional
public void deleteFavoriteFood(Long favoriteFoodId) {
favoriteFoodRepository.deleteById(favoriteFoodId);
}

@Transactional(readOnly = true)
// 유저의 특정 날짜에 먹은 음식들의 영양성분별 총합 조회 (섭취영양소/기준영양소 및 비율까지 계산해서 반환, dto 구체적 협의 필요)
public ResponseNutritionSumByDateDto getNutritionSumByDate(Long userId, LocalDate date) {
List<Food> foodList = foodRepository.findAllByUserIdAndDate(userId, date);
User targetUser = getUserById(userId);
int totalKcal = 0;
int totalCarbohydrate = 0;
int totalProtein = 0;
int totalFat = 0;

for (Food food : foodList) {
BaseNutrition targetFoodNutrition = food.getBaseNutrition();
totalKcal += targetFoodNutrition.getKcal();
totalCarbohydrate += targetFoodNutrition.getCarbohydrate();
totalProtein += targetFoodNutrition.getProtein();
totalFat += targetFoodNutrition.getFat();
}

double ratioKcal = Math.round((totalKcal*1.0)/(targetUser.getBaseNutrition().getKcal()*1.0))*10.0;
double ratioCarbohydrate = Math.round((totalCarbohydrate*1.0)/(targetUser.getBaseNutrition().getCarbohydrate()*1.0))*10.0;
double ratioProtein = Math.round((totalProtein*1.0)/(targetUser.getBaseNutrition().getProtein()*1.0))*10.0;
double ratioFat = Math.round((totalFat*1.0)/(targetUser.getBaseNutrition().getFat()*1.0))*10.0;

return ResponseNutritionSumByDateDto.of(totalKcal,totalCarbohydrate, totalProtein, totalFat, ratioKcal, ratioCarbohydrate, ratioProtein, ratioFat);
}

@Transactional
// 유저의 최근 7일간의 영양성분별 총합 조회 (섭취영양소/기준영양소 및 비율까지 계산해서 반환, dto 구체적 협의 필요)
public void getNutritionSumByWeek(Long userId) {

}

@Transactional
// 유저의 최근 1개월간의 영양성분별 총합 조회 (섭취영양소/기준영양소 및 비율까지 계산해서 반환, dto 구체적 협의 필요)
public void getNutritionSumByMonth(Long userId) {

}

@Transactional
// 유저의 최근 7일간의 Best 3 음식 조회 (dto 구체적 협의 필요)
public void getBestFoodByWeek(Long userId) {

}

@Transactional
// 유저의 최근 7일간의 Worst 3 음식 조회 (dto 구체적 협의 필요)
public void getWorstFoodByWeek(Long userId) {

}

private User getUserById(Long userId){
return userRepository.findById(userId)
.orElseThrow(() -> new UserException(ResponseCode.USER_NOT_FOUND));
}

private Food getFoodById(Long foodId){
return foodRepository.findById(foodId)
.orElseThrow(() -> new FoodException(ResponseCode.FOOD_NOT_FOUND));
}

private FavoriteFood getFavoriteFoodById(Long foodId){
return favoriteFoodRepository.findById(foodId)
.orElseThrow(() -> new FoodException(ResponseCode.FOOD_NOT_FOUND));
}


/**
* 메서드 구현 유의사항
* 1. 메서드명은 동사로 시작
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class ResponseUserDto {
private int age;
private BaseNutrition baseNutrition;


public static ResponseUserDto of(String userName, int userHeight, int userWeight, int userGender, int userAge, BaseNutrition userBaseNutrition) {
return new ResponseUserDto(userName, userHeight, userWeight, userGender, userAge, userBaseNutrition);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-db.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ spring.datasource.password=${DB_PASSWORD}

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.format_sql=true
Loading

0 comments on commit 60e12ed

Please sign in to comment.