|
|
|
@ -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); |
|
|
|
|
} |
|
|
|
|
} |