create but function to tobuys

dev-backend-tobuy
Amagasu 5 months ago
parent b1f8a53a31
commit d3c0e5eaf3
  1. 10
      backend/src/main/java/com/example/todoapp/controller/ToBuysController.java
  2. 18
      backend/src/main/java/com/example/todoapp/dto/ToBuysBuyDTO.java
  3. 42
      backend/src/main/java/com/example/todoapp/service/ToBuysService.java

@ -10,6 +10,7 @@ package com.example.todoapp.controller;
import com.example.todoapp.dto.DeleteToBuyRequest;
import com.example.todoapp.dto.ToBuyResponse;
import com.example.todoapp.dto.ToBuysDTO;
import com.example.todoapp.dto.ToBuysBuyDTO;
import com.example.todoapp.model.ToBuys;
import com.example.todoapp.model.User;
import com.example.todoapp.repository.UserRepository;
@ -137,4 +138,13 @@ public class ToBuysController {
return ResponseEntity.ok(response);
}
@PostMapping("/{id}")
public ResponseEntity<?> buyToBuys(
Authentication authentication,
@PathVariable("id") ToBuysBuyDTO toBuysbuyDTO) {
toBuysService.buyToBuys(authentication.getName(), toBuysbuyDTO);
return ResponseEntity.ok().build();
}
}

@ -0,0 +1,18 @@
package com.example.todoapp.dto;
import lombok.Data;
/**
* カテゴリDTOクラス
* このクラスはタスクのカテゴリ情報を表します
* カテゴリは名前所有ユーザーなどの情報を持ちます
*
*/
@Data
public class ToBuysBuyDTO {
private Long stuff_id;
private int amount;
private String shop;
private String stuff_name;
private String category;
}

@ -8,10 +8,15 @@
package com.example.todoapp.service;
import com.example.todoapp.util.MessageUtils;
import com.example.todoapp.dto.ToBuysDTO;
import com.example.todoapp.dto.ToBuysBuyDTO;
import com.example.todoapp.model.Stocks;
import com.example.todoapp.model.Stuffs;
import com.example.todoapp.model.ToBuys;
import com.example.todoapp.model.User;
import com.example.todoapp.repository.StocksRepository;
import com.example.todoapp.repository.StuffsRepository;
import com.example.todoapp.repository.ToBuysRepository;
import com.example.todoapp.repository.UserRepository;
@ -19,6 +24,7 @@ import com.example.todoapp.repository.UserRepository;
import jakarta.transaction.Transactional;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -44,6 +50,12 @@ public class ToBuysService {
@Autowired
private StuffsRepository stuffsRepository;
@Autowired
private StuffsRepository stocksRepository;
@Autowired
private MessageUtils messageUtils;
/**
@ -151,4 +163,34 @@ public class ToBuysService {
public int deleteToBuyByIds(Long userId, int tobuyId) {
return toBuysRepository.deleteByUserIdAndTobuyId(userId, tobuyId);
}
public Stocks buyToBuys(String username, ToBuysBuyDTO dto) {
// ユーザー情報を取得
User user = getUserByUsername(username);
// 新しい在庫を作成
Stocks stock = new Stocks();
stock.setStuffs(dto.getStuff_id());
stock.setUser_id(user);
stock.setAmount(dto.getAmount());
stock.setPrice(dto.getPrice());
stock.setLast_update(dto.getLast_update());
stock.setBuy_date(dto.getBuy_date());
stock.setExp_date(dto.getExp_date());
deleteToBuys(toBuyDTO);
// データベースに保存
return stockRepository.save(stock);
}
/**
* ユーザー名からユーザーエンティティを取得する
*
* @param username ユーザー名
* @return ユーザーエンティティ
* @throws UsernameNotFoundException ユーザーが見つからない場合
*/
private User getUserByUsername(String username) {
return userRepository.findByUsername(username)
.orElseThrow(() -> new UsernameNotFoundException(messageUtils.getMessage("error.auth.user.not.found.with.name", new Object[]{username})));
}
}
Loading…
Cancel
Save