取得を実装しました

backend-tobuy-adddish
zhang.pengcheng 5 months ago
parent 44cd5183cd
commit e57d01de15
  1. 44
      backend/src/main/java/com/example/todoapp/controller/ToBuysController.java
  2. 12
      backend/src/main/java/com/example/todoapp/dto/ToBuyResponse.java
  3. 2
      backend/src/main/java/com/example/todoapp/dto/ToBuysDTO.java
  4. 13
      backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java
  5. 20
      backend/src/main/java/com/example/todoapp/service/ToBuysService.java

@ -7,9 +7,17 @@
package com.example.todoapp.controller;
import com.example.todoapp.dto.ToBuyDTO;
import com.example.todoapp.dto.ToBuyResponse;
import com.example.todoapp.dto.ToBuysDTO;
import com.example.todoapp.model.ToBuys;
import com.example.todoapp.service.ToBuysService;
import jakarta.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@ -22,7 +30,7 @@ import org.springframework.web.bind.annotation.*;
* </p>
*/
@RestController
@RequestMapping("/tobuy/add")
@RequestMapping("/tobuy")
public class ToBuysController {
@Autowired
@ -35,9 +43,37 @@ public class ToBuysController {
* @return 成功時のレスポンスメッセージ
*/
@PostMapping
public ResponseEntity<String> addToBuys(@Valid @RequestBody ToBuyDTO dto) {
@PostMapping("/add")
public ResponseEntity<String> addToBuys(@Valid @RequestBody ToBuysDTO dto) {
toBuysService.addToBuys(dto);
return ResponseEntity.ok("Item added to 'To Buys' successfully");
}
/**
* 指定されたユーザーIDに基づいてすべての買うものリストを取得する
*
* @param userId ユーザーID
* @return ユーザーに紐づく買うものリスト
*/
@GetMapping("/get")
public ResponseEntity<?> getAllToBuysByUserId(@RequestParam Long user_id) {
List<ToBuys> toBuysList = toBuysService.getToBuysByUserId(user_id);
// DTO形式に変換して返す
List<ToBuyResponse> responseList = toBuysList.stream()
.map(toBuy -> {
ToBuyResponse resp = new ToBuyResponse();
resp.setTobuy_id(toBuy.getTobuy_id());
resp.setStuff_id(toBuy.getStuffs().getStuff_id());
resp.setAmount(toBuy.getAmount());
resp.setShop(toBuy.getStore());
return resp;
})
.collect(Collectors.toList());
Map<String, Object> responseBody = new HashMap<>();
responseBody.put("tobuy_array", responseList);
return ResponseEntity.ok(responseList);
}
}

@ -0,0 +1,12 @@
// src/main/java/com/example/todoapp/dto/ToBuyResponse.java
package com.example.todoapp.dto;
import lombok.Data;
@Data
public class ToBuyResponse {
private int tobuy_id;
private Long stuff_id;
private int amount;
private String shop;
}

@ -9,7 +9,7 @@ import lombok.Data;
*/
@Data
public class ToBuyDTO {
public class ToBuysDTO {
private Long stuff_id;
private Long user_id;

@ -8,7 +8,11 @@
package com.example.todoapp.repository;
import com.example.todoapp.model.ToBuys;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
/**
@ -20,4 +24,13 @@ import org.springframework.stereotype.Repository;
*/
@Repository
public interface ToBuysRepository extends JpaRepository<ToBuys, Integer> {
/**
* 指定されたユーザーIDに基づいて買うものリストを取得する
*
* @param userId ユーザーID
* @return 買うものリスト
*/
@Query("SELECT t FROM ToBuys t WHERE t.user_id.id = ?1")
List<ToBuys> findByUserId(Long user_id);
}

@ -8,7 +8,7 @@
package com.example.todoapp.service;
import com.example.todoapp.dto.ToBuyDTO;
import com.example.todoapp.dto.ToBuysDTO;
import com.example.todoapp.model.Stuffs;
import com.example.todoapp.model.ToBuys;
import com.example.todoapp.model.User;
@ -18,6 +18,7 @@ import com.example.todoapp.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
/**
@ -46,11 +47,11 @@ public class ToBuysService {
*
* @param toBuyDTO 追加する購入アイテムのデータDTO
*/
public void addToBuys(ToBuyDTO toBuyDTO) {
public void addToBuys(ToBuysDTO toBuyDTO) {
// ユーザー情報を取得
User user = userRepository.findById(toBuyDTO.getUser_id())
.orElseThrow(() -> new RuntimeException("用户不存在: " + toBuyDTO.getUser_id()));
.orElseThrow(() -> new RuntimeException("ユーザーがありませ: " + toBuyDTO.getUser_id()));
Stuffs stuffs;
if (toBuyDTO.getStuff_id() == null) {
@ -63,13 +64,12 @@ public class ToBuysService {
// 材料情報を取得
Optional<Stuffs> optionalStuffs = stuffsRepository.findById(toBuyDTO.getStuff_id());
if (!optionalStuffs.isPresent()) {
throw new RuntimeException("材料不存在");
throw new RuntimeException("材料がありません");
}
stuffs = optionalStuffs.get();
}
ToBuys toBuys = new ToBuys();
System.out.println("AAAAuser_id: " + toBuyDTO.getUser_id());
toBuys.setUser_id(user);
toBuys.setStuffs(stuffs);
toBuys.setAmount(toBuyDTO.getAmount());
@ -79,4 +79,14 @@ public class ToBuysService {
toBuysRepository.save(toBuys);
}
/**
* 指定されたユーザーIDに基づいてすべての買うものリストを取得する
*
* @param userId ユーザーID
* @return ユーザーに紐づく買うものリスト
*/
public List<ToBuys> getToBuysByUserId(Long userId) {
return toBuysRepository.findByUserId(userId);
}
}
Loading…
Cancel
Save