tobuyの返す修正しました

feature-backend-tobuy-stock^2
zhang.pengcheng 5 months ago
parent 33428779d5
commit d7c2eadb09
  1. 27
      backend/src/main/java/com/example/todoapp/controller/ToBuysController.java
  2. 3
      backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java
  3. 33
      backend/src/main/java/com/example/todoapp/service/ToBuysService.java

@ -57,19 +57,36 @@ public class ToBuysController {
*/
@PostMapping("/add")
public ResponseEntity<String> addToBuys(
public ResponseEntity<Map<String, Object>> addToBuys(
@Valid @RequestBody ToBuysDTO dto,
Authentication authentication) {
toBuysService.addToBuys(dto, authentication);
return ResponseEntity.ok("Item added to 'To Buys' successfully");
ToBuys createdToBuy = toBuysService.addToBuys(dto, authentication);
// 构建响应体
Map<String, Object> response = new HashMap<>();
response.put("result", true);
response.put("tobuyId", createdToBuy.getTobuyId());
response.put("stuffId", createdToBuy.getStuff().getStuffId());
response.put("message", "追加に成功しました");
return ResponseEntity.ok(response);
}
@PutMapping("/update")
public ResponseEntity<String> updateToBuys(
public ResponseEntity<Map<String, Object>> updateToBuys(
@Valid @RequestBody ToBuysDTO dto,
Authentication authentication) {
toBuysService.updateToBuys(dto, authentication);
return ResponseEntity.ok("Item updated to 'To Buys' successfully");
ToBuys updatedToBuy = toBuysService.updateToBuys(dto, authentication);
Map<String, Object> response = new HashMap<>();
response.put("tobuyId", updatedToBuy.getTobuyId());
response.put("stuffId", updatedToBuy.getStuff().getStuffId());
return ResponseEntity.ok(response);
}

@ -10,6 +10,7 @@ package com.example.todoapp.repository;
import com.example.todoapp.model.Stuffs;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@ -26,4 +27,6 @@ public interface StuffsRepository extends JpaRepository<Stuffs, Long> {
// 材料情報を主キーで取得するメソッド(必要に応じて追加)
Stuffs findByStuffId(Long stuffId);
Optional<Stuffs> findByStuffName(String stuffName);
}

@ -62,7 +62,7 @@ public class ToBuysService {
*
* @param toBuyDTO 追加する購入アイテムのデータDTO
*/
public void addToBuys(ToBuysDTO toBuyDTO, Authentication authentication) {
public ToBuys addToBuys(ToBuysDTO toBuyDTO, Authentication authentication) {
// ユーザー情報を取得
String username = authentication.getName();
@ -92,8 +92,8 @@ public class ToBuysService {
toBuys.setStore(toBuyDTO.getShop());
// データベースに保存
toBuysRepository.save(toBuys);
return toBuysRepository.save(toBuys);
}
/**
@ -101,7 +101,7 @@ public class ToBuysService {
*
* @param toBuyDTO 変更する購入アイテムのデータDTO
*/
public void updateToBuys(ToBuysDTO toBuyDTO, Authentication authentication) {
public ToBuys updateToBuys(ToBuysDTO toBuyDTO, Authentication authentication) {
// ユーザー情報を取得
String username = authentication.getName();
@ -110,11 +110,20 @@ public class ToBuysService {
Stuffs stuffs;
if (toBuyDTO.getStuffId() == null) {
Optional<Stuffs> existingStuffs = stuffsRepository.findByStuffName(toBuyDTO.getStuffName());
// 新しい材料を作成
stuffs = new Stuffs();
stuffs.setStuffName(toBuyDTO.getStuffName());
stuffs.setCategory(toBuyDTO.getCategory());
stuffs = stuffsRepository.save(stuffs);
if (existingStuffs.isPresent()) {
// 如果存在,更新已有材料的属性
stuffs = existingStuffs.get();
stuffs.setCategory(toBuyDTO.getCategory()); // 可选:更新分类
} else {
// 否则新建材料
stuffs = new Stuffs();
stuffs.setStuffName(toBuyDTO.getStuffName());
stuffs.setCategory(toBuyDTO.getCategory());
}
stuffsRepository.save(stuffs);
} else {
// 材料情報を取得
Optional<Stuffs> optionalStuffs = stuffsRepository.findById(toBuyDTO.getStuffId());
@ -124,22 +133,18 @@ public class ToBuysService {
stuffs = optionalStuffs.get();
//update
// stuffs.setStuffName(toBuyDTO.getStuffName());
// stuffs.setCategory(toBuyDTO.getCategory());
stuffs = stuffsRepository.save(stuffs);
}
ToBuys toBuys = new ToBuys();
toBuys.setTobuyId(toBuyDTO.getTobuyId());
toBuys.setUser(user);
toBuys.setStuff(stuffs);
toBuys.setAmount(toBuyDTO.getAmount());
toBuys.setStore(toBuyDTO.getShop());
// データベースに保存
toBuysRepository.save(toBuys);
return toBuysRepository.save(toBuys);
}
/**

Loading…
Cancel
Save