From 9d24a254f27929286cadba3b0c9532a14ca7daa3 Mon Sep 17 00:00:00 2001 From: "zhang.pengcheng" Date: Tue, 3 Jun 2025 17:27:08 +0900 Subject: [PATCH] =?UTF-8?q?=E9=A3=9F=E6=9D=90=E3=82=92=E5=AE=9F=E8=A3=85?= =?UTF-8?q?=E3=81=97=E3=81=BE=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../todoapp/controller/StuffsController.java | 45 ++++++++++++++++ .../example/todoapp/dto/StuffDetailDTO.java | 19 +++++++ .../example/todoapp/dto/StuffRequestDTO.java | 34 ++++++++++++ .../todoapp/repository/StuffsRepository.java | 21 ++++++++ .../todoapp/service/StuffsService.java | 53 +++++++++++++++++++ 5 files changed, 172 insertions(+) create mode 100644 backend/src/main/java/com/example/todoapp/controller/StuffsController.java create mode 100644 backend/src/main/java/com/example/todoapp/dto/StuffDetailDTO.java create mode 100644 backend/src/main/java/com/example/todoapp/dto/StuffRequestDTO.java create mode 100644 backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java create mode 100644 backend/src/main/java/com/example/todoapp/service/StuffsService.java diff --git a/backend/src/main/java/com/example/todoapp/controller/StuffsController.java b/backend/src/main/java/com/example/todoapp/controller/StuffsController.java new file mode 100644 index 0000000..4a71368 --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/controller/StuffsController.java @@ -0,0 +1,45 @@ +package com.example.todoapp.controller; + +import com.example.todoapp.dto.StuffRequestDTO; +import com.example.todoapp.model.Stuffs; +import com.example.todoapp.service.StuffsService; + +import java.util.Collections; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +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; + + +@RestController +@RequestMapping("/stuffs") +public class StuffsController { + + @Autowired + private StuffsService stuffsService; + + @PostMapping("/add") + public ResponseEntity createStuffs(@RequestBody StuffRequestDTO requestDTO) { + try { + Stuffs newStuffs = stuffsService.createStuffs(requestDTO); + return ResponseEntity.ok(newStuffs); + } catch (IllegalArgumentException e) { + return ResponseEntity.badRequest().body(Collections.singletonMap("error", e.getMessage())); + } + } + + @PostMapping("/get") + public ResponseEntity getStuffs(@RequestBody StuffRequestDTO requestDTO) { + try { + Long stuffId = requestDTO.getStuffs().getStuff_id(); + Stuffs stuff = stuffsService.getStuff(stuffId); + return ResponseEntity.ok(stuff); + } catch (IllegalArgumentException e) { + return ResponseEntity.badRequest().body(Collections.singletonMap("error", e.getMessage())); + } + } + +} \ No newline at end of file diff --git a/backend/src/main/java/com/example/todoapp/dto/StuffDetailDTO.java b/backend/src/main/java/com/example/todoapp/dto/StuffDetailDTO.java new file mode 100644 index 0000000..5e784e7 --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/dto/StuffDetailDTO.java @@ -0,0 +1,19 @@ +package com.example.todoapp.dto; + +import com.example.todoapp.model.Stuffs; +import lombok.Data; + +@Data +public class StuffDetailDTO { + private Long stuff_id; + private String stuff_name; + private String summary; + private String category; + + public StuffDetailDTO(Stuffs stuff) { + this.stuff_id = stuff.getStuff_id(); + this.stuff_name = stuff.getStuff_name(); + this.summary = stuff.getSummary(); + this.category = stuff.getCategory(); + } +} \ No newline at end of file diff --git a/backend/src/main/java/com/example/todoapp/dto/StuffRequestDTO.java b/backend/src/main/java/com/example/todoapp/dto/StuffRequestDTO.java new file mode 100644 index 0000000..be95b13 --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/dto/StuffRequestDTO.java @@ -0,0 +1,34 @@ +package com.example.todoapp.dto; + +import com.example.todoapp.dto.StuffRequestDTO.StuffDTO; +import com.example.todoapp.dto.StuffRequestDTO.StuffIdentifier; + +import lombok.Data; + +/** + * タスクエンティティのリポジトリインターフェース + *

+ * このインターフェースはタスクデータへのアクセスと操作を提供します。 + * Spring Data JPAによって自動的に実装されます。 + *

+ */ +@Data +public class StuffRequestDTO { + + + @Data + public static class StuffDTO { + private Long stuff_id; + private String stuff_name; + private String summary; + private String category; + } + + @Data + public static class StuffIdentifier { + private Long stuff_id; + } + + private StuffDTO stuff; + private StuffIdentifier stuffs; +} \ 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..390acbf --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java @@ -0,0 +1,21 @@ +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; + +/** + * タスクエンティティのリポジトリインターフェース + *

+ * このインターフェースはタスクデータへのアクセスと操作を提供します。 + * Spring Data JPAによって自動的に実装されます。 + *

+ */ +@Repository +public interface StuffsRepository extends JpaRepository { + + Optional findById(Long stuff_id); +} \ No newline at end of file diff --git a/backend/src/main/java/com/example/todoapp/service/StuffsService.java b/backend/src/main/java/com/example/todoapp/service/StuffsService.java new file mode 100644 index 0000000..78dd91b --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/service/StuffsService.java @@ -0,0 +1,53 @@ +package com.example.todoapp.service; + +import com.example.todoapp.dto.StuffRequestDTO; +import com.example.todoapp.model.Stuffs; +import com.example.todoapp.repository.StuffsRepository; + +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** + * 認証機能のサービスクラス + *

+ * このクラスはユーザー登録とログイン処理のビジネスロジックを提供します。 + * 認証に成功した場合、JWTトークンを発行します。 + *

+ */ +@Service +public class StuffsService { + + @Autowired + private StuffsRepository stuffsRepository; + + @Transactional + public Stuffs createStuffs(StuffRequestDTO requestDTO) { + + Stuffs stuffs = new Stuffs(); + stuffs.setStuff_name(requestDTO.getStuff().getStuff_name()); + stuffs.setSummary(requestDTO.getStuff().getSummary()); + stuffs.setCategory(requestDTO.getStuff().getCategory()); + + return stuffsRepository.save(stuffs); + } + + @Transactional + public void deteleStuffs(Long stuffId) { + stuffsRepository.deleteById(stuffId); + + } + + // 查询食材方法(移除用户验证) + @Transactional(readOnly = true) + public Stuffs getStuff(Long stuffId) { + Optional stuffOpt = stuffsRepository.findById(stuffId); + if (stuffOpt.isPresent()) { + return stuffOpt.get(); + } else { + throw new RuntimeException("no: " + stuffId); + } + } +} \ No newline at end of file