Skip to content
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,6 +1,7 @@
package moja.refrigerator.controller.ingredient;

import moja.refrigerator.dto.ingredient.request.IngredientCreateRequest;
import moja.refrigerator.dto.ingredient.request.IngredientUpdateRequest;
import moja.refrigerator.dto.ingredient.request.RequestRegistIngredientBookmark;
import moja.refrigerator.dto.ingredient.response.ResponseRegistIngredientBookmark;
import moja.refrigerator.service.ingredient.IngredientService;
Expand Down Expand Up @@ -51,4 +52,17 @@ public List<IngredientResponse> getIngredient() {
return ingredientService.getIngredient();

}

// 재료 정보 수정
@PutMapping
public void updateIngredient(@RequestBody IngredientUpdateRequest request) {
ingredientService.updateIngredient(request);
}

// 재료 삭제
@DeleteMapping
public void deleteIngredient(@RequestParam long ingredientManagementPK) {
ingredientService.deleteIngredient(ingredientManagementPK);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public class IngredientCreateRequest {
private String registrationDate;
private int seasonDate;

// private int ingredientCategoryPk;
// private int ingredientStoragePk;
private int ingredientCategoryPk;
private int ingredientStoragePk;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package moja.refrigerator.dto.ingredient.request;

import lombok.Data;

@Data
public class IngredientUpdateRequest {

private long ingredientManagementPk;
private String ingredientName;
private String expirationDate;
private String registrationDate;
private int seasonDate;
private int ingredientCategoryPk;
private int ingredientStoragePk;

}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check!
lombok 쓰는데 클래스 생성자를 넣은 이유가 있을까유?

Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,11 @@ public class IngredientResponse {
private String expirationDate;
private String registrationDate;
private int seasonDate;
// 조회 시에는 pk 말고 이름으로 조회 되도록
private String ingredientCategory;
private String ingredientStorage;

// ModelMapper를 위한 기본 생성자 생성
// ModelMapper 를 위한 기본 생성자 생성
public IngredientResponse() {}

// 생성자
public IngredientResponse(long ingredientManagementPk, String ingredientName, String expirationDate, String registrationDate, int seasonDate) {
this.ingredientManagementPk = ingredientManagementPk;
this.ingredientName = ingredientName;
this.expirationDate = expirationDate;
this.registrationDate = registrationDate;
this.seasonDate = seasonDate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

@Repository
public interface IngredientManagementRepository extends JpaRepository<IngredientManagement, Long> {

}

Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package moja.refrigerator.service.ingredient;

import moja.refrigerator.dto.ingredient.request.IngredientCreateRequest;
import moja.refrigerator.dto.ingredient.request.IngredientUpdateRequest;
import moja.refrigerator.dto.ingredient.response.IngredientResponse;
import moja.refrigerator.dto.ingredient.request.RequestRegistIngredientBookmark;
import moja.refrigerator.dto.ingredient.response.ResponseRegistIngredientBookmark;
import java.util.List;
import moja.refrigerator.dto.ingredient.response.IngredientResponse;

public interface IngredientService {
void createIngredient(IngredientCreateRequest request);
// void createIngredient(IngredientCreateRequest request);

ResponseRegistIngredientBookmark createIngredientBookmark(RequestRegistIngredientBookmark requestBookmark);

void createIngredient(IngredientCreateRequest request); // 재료 등록 메서드

List<IngredientResponse> getIngredient(); // 재료 조회 메서드

void updateIngredient(IngredientUpdateRequest request);
void deleteIngredient(long ingredientManagementPK);
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package moja.refrigerator.service.ingredient;

//import moja.refrigerator.aggregate.ingredient.IngredientCategory;
import moja.refrigerator.aggregate.ingredient.IngredientCategory;
import moja.refrigerator.aggregate.ingredient.IngredientManagement;
import moja.refrigerator.aggregate.ingredient.IngredientStorage;
import moja.refrigerator.dto.ingredient.request.IngredientCreateRequest;
import moja.refrigerator.repository.ingredient.IngredientCategoryRepository;
import moja.refrigerator.dto.ingredient.request.IngredientUpdateRequest;
import moja.refrigerator.dto.ingredient.response.IngredientResponse;
import moja.refrigerator.repository.ingredient.IngredientManagementRepository;
import moja.refrigerator.repository.ingredient.IngredientStorageRepository;
import jakarta.persistence.EntityNotFoundException;
import moja.refrigerator.aggregate.ingredient.IngredientBookmark;
import moja.refrigerator.aggregate.ingredient.IngredientManagement;
//import moja.refrigerator.aggregate.ingredient.IngredientStorage;
import moja.refrigerator.aggregate.user.User;
import moja.refrigerator.dto.ingredient.request.IngredientCreateRequest;
//import moja.refrigerator.repository.ingredient.IngredientCategoryRepository;
import moja.refrigerator.dto.ingredient.request.RequestRegistIngredientBookmark;
import moja.refrigerator.dto.ingredient.response.ResponseRegistIngredientBookmark;
import moja.refrigerator.repository.ingredient.IngredientBookmarkRepository;
import moja.refrigerator.dto.ingredient.response.IngredientResponse;
import moja.refrigerator.repository.ingredient.IngredientManagementRepository;
//import moja.refrigerator.repository.ingredient.IngredientStorageRepository;
import moja.refrigerator.repository.user.UserRepository;

import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -28,22 +30,22 @@
public class IngredientServiceImpl implements IngredientService{

private IngredientManagementRepository ingredientManagementRepository;
// private IngredientStorageRepository ingredientStorageRepository;
// private IngredientCategoryRepository ingredientCategoryRepository;
private IngredientStorageRepository ingredientStorageRepository;
private IngredientCategoryRepository ingredientCategoryRepository;
private IngredientBookmarkRepository ingredientBookmarkRepository;
private UserRepository userRepository;
private ModelMapper mapper;

@Autowired
public IngredientServiceImpl(IngredientManagementRepository ingredientManagementRepository,
// IngredientStorageRepository ingredientStorageRepository,
// IngredientCategoryRepository ingredientCategoryRepository,
IngredientStorageRepository ingredientStorageRepository,
IngredientCategoryRepository ingredientCategoryRepository,
IngredientBookmarkRepository ingredientBookmarkRepository,
UserRepository userRepository,
ModelMapper mapper) {
this.ingredientManagementRepository = ingredientManagementRepository;
// this.ingredientStorageRepository = ingredientStorageRepository;
// this.ingredientCategoryRepository = ingredientCategoryRepository;
this.ingredientStorageRepository = ingredientStorageRepository;
this.ingredientCategoryRepository = ingredientCategoryRepository;
this.ingredientBookmarkRepository = ingredientBookmarkRepository;
this.userRepository = userRepository;
this.mapper = mapper;
Expand All @@ -54,14 +56,14 @@ public IngredientServiceImpl(IngredientManagementRepository ingredientManagement
public void createIngredient(IngredientCreateRequest request) {
IngredientManagement ingredient = mapper.map(request, IngredientManagement.class);

// IngredientCategory category = ingredientCategoryRepository.findById(request.getIngredientCategoryPk())
// .orElseThrow(() -> new IllegalArgumentException("카테고리를 찾을 수 없습니다."));
//
// IngredientStorage storage = ingredientStorageRepository.findById(request.getIngredientStoragePk())
// .orElseThrow(() -> new IllegalArgumentException("저장소를 찾을 수 없습니다."));
//
// ingredient.setIngredientCategory(category);
// ingredient.setIngredientStorage(storage);
IngredientCategory category = ingredientCategoryRepository.findById(request.getIngredientCategoryPk())
.orElseThrow(() -> new IllegalArgumentException("카테고리를 찾을 수 없습니다."));

IngredientStorage storage = ingredientStorageRepository.findById(request.getIngredientStoragePk())
.orElseThrow(() -> new IllegalArgumentException("해당 보관 방법을 찾을 수 없습니다."));

ingredient.setIngredientCategory(category);
ingredient.setIngredientStorage(storage);

// 재료를 JpaRepository의 save() 메소드로 DB에 저장 !
ingredientManagementRepository.save(ingredient);
Expand Down Expand Up @@ -98,4 +100,40 @@ public List<IngredientResponse> getIngredient() {
.collect(Collectors.toList());
}

@Override
@Transactional
public void updateIngredient(IngredientUpdateRequest request) {
// 1. 수정할 재료 조회
IngredientManagement ingredient = ingredientManagementRepository
.findById(request.getIngredientManagementPk())
.orElseThrow(() -> new IllegalArgumentException("수정할 재료를 찾을 수 없습니다."));

// 2. 새로운 카테고리와 저장소 조회
IngredientCategory newCategory = ingredientCategoryRepository.findById(request.getIngredientCategoryPk())
.orElseThrow(() -> new IllegalArgumentException("카테고리를 찾을 수 없습니다."));

IngredientStorage newStorage = ingredientStorageRepository.findById(request.getIngredientStoragePk())
.orElseThrow(() -> new IllegalArgumentException("해당 보관 방법을 찾을 수 없습니다."));

// 3. 기본 필드들 업데이트
ingredient.setIngredientName(request.getIngredientName());
ingredient.setExpirationDate(request.getExpirationDate());
ingredient.setRegistrationDate(request.getRegistrationDate());
ingredient.setSeasonDate(request.getSeasonDate());

// 4. 연관관계 설정
ingredient.setIngredientCategory(newCategory);
ingredient.setIngredientStorage(newStorage);
}

@Override
@Transactional
public void deleteIngredient(long ingredientManagementPK) {
IngredientManagement ingredient = ingredientManagementRepository
.findById(ingredientManagementPK)
.orElseThrow(() -> new IllegalArgumentException("삭제할 재료를 찾을 수 없습니다."));

ingredientManagementRepository.delete(ingredient);
}

}