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,12 +1,12 @@
package moja.refrigerator.controller.ingredient;

import moja.refrigerator.dto.ingredient.request.IngredientCreateRequest;
import moja.refrigerator.dto.ingredient.response.IngredientResponse;
import moja.refrigerator.service.ingredient.IngredientService;
import org.springframework.beans.factory.annotation.Autowired;
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;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ingredient")
Expand All @@ -18,8 +18,15 @@ public IngredientController(IngredientService ingredientService) {
this.ingredientService = ingredientService;
}

// 재료 등록
@PostMapping
public void createIngredient(@RequestBody IngredientCreateRequest request) {
ingredientService.createIngredient(request);
}

// 재료 조회
@GetMapping
public List<IngredientResponse> getIngredient() {
return ingredientService.getIngredient();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package moja.refrigerator.dto.ingredient.response;

import lombok.Data;

@Data
public class IngredientResponse {
private long ingredientManagementPk;
private String ingredientName;
private String expirationDate;
private String registrationDate;
private int seasonDate;

// 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
@@ -1,7 +1,11 @@
package moja.refrigerator.service.ingredient;

import moja.refrigerator.dto.ingredient.request.IngredientCreateRequest;
import moja.refrigerator.dto.ingredient.response.IngredientResponse;

import java.util.List;

public interface IngredientService {
void createIngredient(IngredientCreateRequest request);
void createIngredient(IngredientCreateRequest request); // 재료 등록 메서드
List<IngredientResponse> getIngredient(); // 재료 조회 메서드
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
//import moja.refrigerator.aggregate.ingredient.IngredientStorage;
import moja.refrigerator.dto.ingredient.request.IngredientCreateRequest;
//import moja.refrigerator.repository.ingredient.IngredientCategoryRepository;
import moja.refrigerator.dto.ingredient.response.IngredientResponse;
import moja.refrigerator.repository.ingredient.IngredientManagementRepository;
//import moja.refrigerator.repository.ingredient.IngredientStorageRepository;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class IngredientServiceImpl implements IngredientService{

Expand Down Expand Up @@ -45,6 +49,18 @@ public void createIngredient(IngredientCreateRequest request) {
// ingredient.setIngredientCategory(category);
// ingredient.setIngredientStorage(storage);

// 재료를 JpaRepository의 save() 메소드로 DB에 저장 !
ingredientManagementRepository.save(ingredient);
}

@Override
@Transactional(readOnly = true)
public List<IngredientResponse> getIngredient() {
List<IngredientManagement> ingredients = ingredientManagementRepository.findAll();

return ingredients.stream()
.map(ingredient -> mapper.map(ingredient, IngredientResponse.class))
.collect(Collectors.toList());
}

}