From cb2ffbd4935ecad241b81dc29262843e6e5abb95 Mon Sep 17 00:00:00 2001 From: "zhang.pengcheng" Date: Wed, 4 Jun 2025 16:03:35 +0900 Subject: [PATCH] =?UTF-8?q?add-=E6=97=A2=E5=AD=98=E3=81=AE=E9=A3=9F?= =?UTF-8?q?=E6=9D=90=E3=81=AE=E5=A0=B4=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../todoapp/controller/ToBuysController.java | 22 ++++++++ .../com/example/todoapp/dto/ToBuyDTO.java | 18 +++++++ .../todoapp/repository/StuffsRepository.java | 12 +++++ .../todoapp/repository/ToBuysRepository.java | 9 ++++ .../todoapp/service/ToBuysService.java | 51 +++++++++++++++++++ 5 files changed, 112 insertions(+) create mode 100644 backend/src/main/java/com/example/todoapp/controller/ToBuysController.java create mode 100644 backend/src/main/java/com/example/todoapp/dto/ToBuyDTO.java create mode 100644 backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java create mode 100644 backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java create mode 100644 backend/src/main/java/com/example/todoapp/service/ToBuysService.java diff --git a/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java b/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java new file mode 100644 index 0000000..fbb0603 --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java @@ -0,0 +1,22 @@ +package com.example.todoapp.controller; + +import com.example.todoapp.dto.ToBuyDTO; +import com.example.todoapp.service.ToBuysService; +import jakarta.validation.Valid; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/api/tobuy/add") +public class ToBuysController { + + @Autowired + private ToBuysService toBuysService; + + @PostMapping + public ResponseEntity addToBuys(@Valid @RequestBody ToBuyDTO dto) { + toBuysService.addToBuys(dto); + return ResponseEntity.ok("Item added to 'To Buys' successfully"); + } +} \ No newline at end of file diff --git a/backend/src/main/java/com/example/todoapp/dto/ToBuyDTO.java b/backend/src/main/java/com/example/todoapp/dto/ToBuyDTO.java new file mode 100644 index 0000000..36295bf --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/dto/ToBuyDTO.java @@ -0,0 +1,18 @@ +package com.example.todoapp.dto; + +import lombok.Data; +/** + * カテゴリDTOクラス + * このクラスはタスクのカテゴリ情報を表します。 + * カテゴリは名前、色、所有ユーザーなどの情報を持ちます。 + * + */ + + @Data + public class ToBuyDTO { + private Long stuffsId; + private Long userId; + + private int amount; + private String store; + } \ No newline at end of file diff --git a/backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java b/backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java new file mode 100644 index 0000000..3c29f31 --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java @@ -0,0 +1,12 @@ +package com.example.todoapp.repository; + +import com.example.todoapp.model.Stuffs; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public interface StuffsRepository extends JpaRepository { + Optional findById(Long id); +} \ No newline at end of file diff --git a/backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java b/backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java new file mode 100644 index 0000000..ff6469f --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java @@ -0,0 +1,9 @@ +package com.example.todoapp.repository; + +import com.example.todoapp.model.ToBuys; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface ToBuysRepository extends JpaRepository { +} \ No newline at end of file diff --git a/backend/src/main/java/com/example/todoapp/service/ToBuysService.java b/backend/src/main/java/com/example/todoapp/service/ToBuysService.java new file mode 100644 index 0000000..2a44acb --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/service/ToBuysService.java @@ -0,0 +1,51 @@ +package com.example.todoapp.service; + +import com.example.todoapp.dto.ToBuyDTO; +import com.example.todoapp.model.Stuffs; +import com.example.todoapp.model.ToBuys; +import com.example.todoapp.model.User; +import com.example.todoapp.repository.StuffsRepository; +import com.example.todoapp.repository.ToBuysRepository; +import com.example.todoapp.repository.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.Optional; + +@Service +public class ToBuysService { + + @Autowired + private ToBuysRepository toBuysRepository; + + @Autowired + private UserRepository userRepository; + + @Autowired + private StuffsRepository stuffsRepository; + + public void addToBuys(ToBuyDTO toBuyDTO) { + + //get user + Optional user = userRepository.findById(toBuyDTO.getUserId()); + if (!user.isPresent()) { + throw new RuntimeException("no: " + toBuyDTO.getUserId()); + } + + //get stuffs + Optional stuffs = stuffsRepository.findById(toBuyDTO.getStuffsId()); + if (!stuffs.isPresent()) { + throw new RuntimeException("材料不存在"); + } + + ToBuys toBuys = new ToBuys(); + toBuys.setUser_id(user.get()); + toBuys.setStuffs(stuffs.get()); + toBuys.setAmount(toBuyDTO.getAmount()); + toBuys.setStore(toBuyDTO.getStore()); + + // 保存到数据库 + toBuysRepository.save(toBuys); + + } +} \ No newline at end of file