購入処理のAPI実装

feature-backend-tobuy-buy
Masaharu.Kato 5 months ago
parent 22ac67172e
commit 765fec0ebc
  1. 33
      backend/src/main/java/com/example/todoapp/controller/ToBuysController.java
  2. 14
      backend/src/main/java/com/example/todoapp/dto/BuyRequestDTO.java
  3. 16
      backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java
  4. 38
      backend/src/main/java/com/example/todoapp/service/ToBuysService.java

@ -7,6 +7,7 @@
package com.example.todoapp.controller; package com.example.todoapp.controller;
import com.example.todoapp.dto.BuyRequestDTO;
import com.example.todoapp.dto.DeleteToBuyRequest; import com.example.todoapp.dto.DeleteToBuyRequest;
import com.example.todoapp.dto.ToBuyResponse; import com.example.todoapp.dto.ToBuyResponse;
import com.example.todoapp.dto.ToBuysDTO; import com.example.todoapp.dto.ToBuysDTO;
@ -47,9 +48,6 @@ public class ToBuysController {
@Autowired @Autowired
private UserRepository userRepository; private UserRepository userRepository;
@Autowired
private StuffsRepository stuffsRepository;
/** /**
* 新しい購入アイテムを追加する * 新しい購入アイテムを追加する
* *
@ -140,19 +138,11 @@ public class ToBuysController {
public ResponseEntity<Map<String, Boolean>> deleteToBuy( public ResponseEntity<Map<String, Boolean>> deleteToBuy(
@RequestBody DeleteToBuyRequest request, @RequestBody DeleteToBuyRequest request,
Authentication authentication) { Authentication authentication) {
// 認証されたユーザー名を取得
String username = authentication.getName();
// ユーザー情報を取得 int deletedCount = toBuysService.deleteToBuyById(request.getTobuyId());
User user = userRepository.findByUsername(username)
.orElseThrow(() -> new UsernameNotFoundException("User not found"));
int deletedCount = toBuysService.deleteToBuyByIds(user.getId(), request.getTobuyId());
Map<String, Boolean> response = new HashMap<>(); Map<String, Boolean> response = new HashMap<>();
if (deletedCount > 0) { if (deletedCount > 0) {
response.put("result", true); response.put("result", true);
} else { } else {
@ -162,12 +152,23 @@ public class ToBuysController {
return ResponseEntity.ok(response); return ResponseEntity.ok(response);
} }
@PostMapping("/{id}") /**
public ResponseEntity<?> buyToBuys( * 買うものリストの在庫登録購入処理
* 買うものリストから削除し追加情報を付加して在庫リストに追加
*
* @param request 買うものの情報を含むリクエストボディ
* @return
*/
@PostMapping("/buy")
public ResponseEntity<Map<String, Boolean>> buyToBuys(
Authentication authentication, Authentication authentication,
@PathVariable("id") ToBuysDTO toBuysDTO) { @RequestBody BuyRequestDTO toBuysDTO) {
toBuysService.buyToBuys(authentication.getName(), toBuysDTO); toBuysService.buyToBuys(authentication.getName(), toBuysDTO);
return ResponseEntity.ok().build();
Map<String, Boolean> response = new HashMap<>();
response.put("result", true);
return ResponseEntity.ok(response);
} }
} }

@ -0,0 +1,14 @@
package com.example.todoapp.dto;
import java.time.LocalDate;
import lombok.Data;
@Data
public class BuyRequestDTO {
private Long tobuyId;
private int price;
private LocalDate expDate;
private LocalDate lastUpdate;
private LocalDate buyDate;
}

@ -27,6 +27,15 @@ import org.springframework.stereotype.Repository;
@Repository @Repository
public interface ToBuysRepository extends JpaRepository<ToBuys, Integer> { public interface ToBuysRepository extends JpaRepository<ToBuys, Integer> {
/**
* 指定されたIDに基づいて買うものレコードを取得する
*
* @param tobuyId 買うものID
* @return 買うものレコード
*/
@Query("SELECT t FROM ToBuys t WHERE t.tobuyId = ?1")
ToBuys findById(Long tobuyId);
/** /**
* 指定されたユーザーIDに基づいて買うものリストを取得する * 指定されたユーザーIDに基づいて買うものリストを取得する
* *
@ -37,13 +46,12 @@ public interface ToBuysRepository extends JpaRepository<ToBuys, Integer> {
List<ToBuys> findByUser(Long userId); List<ToBuys> findByUser(Long userId);
/** /**
* 指定されたユーザーIDに基づいて買うものリストを取得する * 指定された買うものIDに基づいて買うものリストを削除
* *
* @param userId ユーザーID
* @param tobuyId 買うものID * @param tobuyId 買うものID
* @return * @return
*/ */
@Modifying @Modifying
@Query("DELETE FROM ToBuys t WHERE t.user.id = :userId AND t.tobuyId = :tobuyId") @Query("DELETE FROM ToBuys t WHERE t.tobuyId = :tobuyId")
int deleteByUserIdAndTobuyId(@Param("userId") Long userId, @Param("tobuyId") Long tobuyId); int deleteByTobuyId(@Param("tobuyId") Long tobuyId);
} }

@ -9,7 +9,7 @@
package com.example.todoapp.service; package com.example.todoapp.service;
import com.example.todoapp.util.MessageUtils; import com.example.todoapp.util.MessageUtils;
import com.example.todoapp.dto.BuyRequestDTO;
import com.example.todoapp.dto.ToBuysDTO; import com.example.todoapp.dto.ToBuysDTO;
import com.example.todoapp.model.Stocks; import com.example.todoapp.model.Stocks;
import com.example.todoapp.model.Stuffs; import com.example.todoapp.model.Stuffs;
@ -148,9 +148,9 @@ public class ToBuysService {
} }
/** /**
* 指定されたユーザーIDに基づいてすべての買うものリストを取得する * 指定されたユーザーに基づいてすべての買うものリストを取得する
* *
* @param userId ユーザーID * @param user ユーザー
* @return ユーザーに紐づく買うものリスト * @return ユーザーに紐づく買うものリスト
*/ */
public List<ToBuys> getToBuysByUser(User user) { public List<ToBuys> getToBuysByUser(User user) {
@ -158,31 +158,43 @@ public class ToBuysService {
} }
/** /**
* 指定されたユーザーIDと購入リストIDに基づいて買うものを削除する * 指定された購入リストIDに基づいて買うものを削除する
* *
* @param userId ユーザーID
* @param tobuyId 購入リストID * @param tobuyId 購入リストID
*/ */
@Transactional @Transactional
public int deleteToBuyByIds(Long userId, Long tobuyId) { public int deleteToBuyById(Long tobuyId) {
return toBuysRepository.deleteByUserIdAndTobuyId(userId, tobuyId); return toBuysRepository.deleteByTobuyId(tobuyId);
} }
public Stocks buyToBuys(String username, ToBuysDTO dto) { /**
* 指定されたユーザーIDと購入データに基づいて買うものを購入する
*
* @param username ユーザーID
* @param dto 購入データ
*/
@Transactional
public Stocks buyToBuys(String username, BuyRequestDTO dto) {
// ユーザー情報を取得 // ユーザー情報を取得
User user = getUserByUsername(username); User user = getUserByUsername(username);
// Tobuy情報の取得
ToBuys tobuy = toBuysRepository.findById(dto.getTobuyId());
// 新しい在庫を作成 // 新しい在庫を作成
Stocks stock = new Stocks(); Stocks stock = new Stocks();
Stuffs stuff = new Stuffs(); stock.setStuff(tobuy.getStuff());
stuff.setStuffId(dto.getStuffId());
stock.setStuff(stuff);
stock.setUser(user); stock.setUser(user);
stock.setAmount(dto.getAmount()); stock.setAmount(tobuy.getAmount());
stock.setPrice(dto.getPrice()); stock.setPrice(dto.getPrice());
stock.setLastUpdate(dto.getLastUpdate()); stock.setLastUpdate(dto.getLastUpdate());
stock.setBuyDate(dto.getBuyDate()); stock.setBuyDate(dto.getBuyDate());
stock.setExpDate(dto.getExpDate()); stock.setExpDate(dto.getExpDate());
deleteToBuyByIds(dto.getUserId(), dto.getTobuyId());
// 買うものリストから削除
System.out.println("tobuy.getTobuyId()=" + tobuy.getTobuyId());
deleteToBuyById(tobuy.getTobuyId());
// データベースに保存 // データベースに保存
return stocksRepository.save(stock); return stocksRepository.save(stock);
} }

Loading…
Cancel
Save