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