commit
555cf9ac0d
@ -0,0 +1,106 @@ |
||||
package com.example.todoapp.controller; |
||||
|
||||
import com.example.todoapp.dto.StockDTO; |
||||
import com.example.todoapp.model.Stocks; |
||||
import com.example.todoapp.service.StocksService; |
||||
import jakarta.validation.Valid; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.http.ResponseEntity; |
||||
import org.springframework.security.core.Authentication; |
||||
import org.springframework.web.bind.annotation.*; |
||||
|
||||
import java.util.List; |
||||
import java.util.stream.Collectors; |
||||
|
||||
/** |
||||
* 在庫管理のコントローラー |
||||
* <p> |
||||
* このコントローラーは在庫の取得、作成、更新、削除などの |
||||
* エンドポイントを提供します。すべてのエンドポイントは認証が必要です。 |
||||
* </p> |
||||
*/ |
||||
@RestController |
||||
@RequestMapping("/stocks") |
||||
public class StocksController { |
||||
|
||||
@Autowired |
||||
private StocksService stockService; |
||||
|
||||
/** |
||||
* ログインユーザーのすべての在庫を取得する |
||||
* |
||||
* @param authentication 認証情報 |
||||
* @return ユーザーの在庫リスト |
||||
*/ |
||||
@GetMapping |
||||
public ResponseEntity<List<StockDTO>> getAllStocks(Authentication authentication) { |
||||
List<Stocks> stocks = stockService.getALLStocksByUser(authentication.getName()); |
||||
// エンティティからDTOへの変換
|
||||
List<StockDTO> stockDTOs = stocks.stream() |
||||
.map(StockDTO::fromEntity) |
||||
.collect(Collectors.toList()); |
||||
return ResponseEntity.ok(stockDTOs); |
||||
} |
||||
|
||||
/** |
||||
* 指定されたIDの在庫を取得する |
||||
* |
||||
* @param authentication 認証情報 |
||||
* @param stockId 在庫ID |
||||
* @return 在庫情報 |
||||
*/ |
||||
@GetMapping("/{id}") |
||||
public ResponseEntity<StockDTO> getStockById( |
||||
Authentication authentication, |
||||
@PathVariable("id") Long stockId) { |
||||
Stocks stock = stockService.getStockById(authentication.getName(), stockId); |
||||
return ResponseEntity.ok(StockDTO.fromEntity(stock)); |
||||
} |
||||
|
||||
/** |
||||
* 新しい在庫を作成する |
||||
* |
||||
* @param authentication 認証情報 |
||||
* @param stock 作成する在庫の情報 |
||||
* @return 作成された在庫 |
||||
*/ |
||||
@PostMapping |
||||
public ResponseEntity<StockDTO> createStock( |
||||
Authentication authentication, |
||||
@Valid @RequestBody Stocks stock) { |
||||
Stocks createdStock = stockService.createStock(authentication.getName(), stock); |
||||
return ResponseEntity.ok(StockDTO.fromEntity(createdStock)); |
||||
} |
||||
|
||||
/** |
||||
* 指定されたIDの在庫を更新する |
||||
* |
||||
* @param authentication 認証情報 |
||||
* @param stockId 更新する在庫のID |
||||
* @param stockDetails 更新内容 |
||||
* @return 更新された在庫 |
||||
*/ |
||||
@PutMapping("/{id}") |
||||
public ResponseEntity<StockDTO> updateStock( |
||||
Authentication authentication, |
||||
@PathVariable("id") Long stockId, |
||||
@Valid @RequestBody Stocks stockDetails) { |
||||
Stocks updatedStock = stockService.updateStocks(authentication.getName(), stockId, stockDetails); |
||||
return ResponseEntity.ok(StockDTO.fromEntity(updatedStock)); |
||||
} |
||||
|
||||
/** |
||||
* 指定されたIDの在庫を削除する |
||||
* |
||||
* @param authentication 認証情報 |
||||
* @param taskId 削除する在庫のID |
||||
* @return 空のレスポンス |
||||
*/ |
||||
@DeleteMapping("/{id}") |
||||
public ResponseEntity<?> deleteStock( |
||||
Authentication authentication, |
||||
@PathVariable("id") Long stockId) { |
||||
stockService.deleteStock(authentication.getName(), stockId); |
||||
return ResponseEntity.ok().build(); |
||||
} |
||||
} |
@ -0,0 +1,46 @@ |
||||
package com.example.todoapp.dto; |
||||
|
||||
import com.example.todoapp.model.Stocks; |
||||
|
||||
import lombok.Data; |
||||
|
||||
import java.time.LocalDate; |
||||
|
||||
/** |
||||
* 在庫のデータ転送オブジェクト(DTO) |
||||
* <p> |
||||
* このクラスはクライアントとサーバー間で在庫情報をやり取りするために使用されます。 |
||||
* エンティティとは異なり、必要な情報のみを含み、関連エンティティへの参照ではなくIDのみを保持します。 |
||||
* </p> |
||||
*/ |
||||
|
||||
@Data |
||||
public class StockDTO { |
||||
private Long stock_id; |
||||
private Long stuff_id; |
||||
private Long user_id; |
||||
private int amount; |
||||
private int price; |
||||
private LocalDate buy_date; |
||||
private LocalDate last_update; |
||||
private LocalDate exp_date; |
||||
|
||||
/** |
||||
* 在庫エンティティからDTOを作成する |
||||
* |
||||
* @param stock 変換元の在庫エンティティ |
||||
* @return 変換されたStockDTOオブジェクト |
||||
*/ |
||||
public static StockDTO fromEntity(Stocks stock) { |
||||
StockDTO dto = new StockDTO(); |
||||
dto.setStock_id(stock.getStock_id()); |
||||
dto.setStuff_id(stock.getStuff_id()); |
||||
dto.setUser_id(stock.getUser_id().getId()); |
||||
dto.setAmount(stock.getAmount()); |
||||
dto.setPrice(stock.getPrice()); |
||||
dto.setBuy_date(stock.getBuy_date()); |
||||
dto.setLast_update(stock.getLast_update()); |
||||
dto.setExp_date(stock.getExp_date()); |
||||
return dto; |
||||
} |
||||
} |
@ -0,0 +1,112 @@ |
||||
package com.example.todoapp.service; |
||||
|
||||
import com.example.todoapp.model.Stocks; |
||||
import com.example.todoapp.util.MessageUtils; |
||||
import com.example.todoapp.model.User; |
||||
import com.example.todoapp.repository.StocksRepository; |
||||
import com.example.todoapp.repository.UserRepository; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* Stocksテーブルのサービスクラス |
||||
* <p> |
||||
* このクラスは在庫の追加、取得、更新、削除などのビジネスロジックを提供します。 |
||||
* </p> |
||||
*/ |
||||
@Service |
||||
public class StocksService { |
||||
@Autowired |
||||
private StocksRepository stocksRepository; |
||||
|
||||
@Autowired |
||||
private UserRepository userRepository; |
||||
|
||||
@Autowired |
||||
private MessageUtils messageUtils; |
||||
|
||||
/** |
||||
* 新たな在庫を追加する |
||||
* |
||||
* @param username ユーザー名 |
||||
* @param stock 追加する在庫情報 |
||||
* @return 保存された在庫エンティティ |
||||
*/ |
||||
public Stocks createStock(String username, Stocks stock) { |
||||
User user = getUserByUsername(username); |
||||
stock.setUser_id(user); |
||||
return stocksRepository.save(stock); |
||||
|
||||
} |
||||
|
||||
/** |
||||
* 指定されたユーザのすべての在庫を取得する |
||||
* @param username ユーザー名 |
||||
* @return ユーザーの在庫リスト(stock_id昇順) |
||||
*/ |
||||
public List<Stocks> getALLStocksByUser(String username) { |
||||
User user = getUserByUsername(username); |
||||
return stocksRepository.findStocksByUser_id(user.getId()); |
||||
} |
||||
|
||||
/** |
||||
* 指定されたユーザーの特定の在庫を取得する |
||||
* |
||||
* @param username ユーザー名 |
||||
* @param stockId 在庫ID |
||||
* @return 在庫エンティティ |
||||
* @throws RuntimeException 在庫が見つからない場合、または他のユーザーの在庫にアクセスしようとした場合 |
||||
*/ |
||||
public Stocks getStockById(String username, Long stockId) { |
||||
User user = getUserByUsername(username); |
||||
return stocksRepository.findById(stockId) |
||||
.filter(stock -> stock.getUser_id().getId().equals(user.getId())) // ユーザーの在庫かどうかを確認
|
||||
.orElseThrow(() -> new RuntimeException(messageUtils.getMessage("error.stock.not.found"))); |
||||
} |
||||
|
||||
/** |
||||
* 指定された在庫情報を編集する |
||||
* |
||||
* @param username ユーザー名 |
||||
* @param stockId 変数する在庫ID |
||||
* @param stockDetails 編集内容(新しい情報) |
||||
* @return 編集された在庫エンティティ |
||||
*/ |
||||
public Stocks updateStocks(String username, Long stockId, Stocks stockDetails) { |
||||
Stocks stock = getStockById(username, stockId); |
||||
stock.setAmount(stockDetails.getAmount()); |
||||
stock.setPrice(stockDetails.getPrice()); |
||||
stock.setLast_update(stockDetails.getLast_update()); |
||||
stock.setBuy_date(stockDetails.getBuy_date()); |
||||
stock.setExp_date(stockDetails.getExp_date()); |
||||
return stocksRepository.save(stock); |
||||
} |
||||
|
||||
/** |
||||
* 指定された在庫を削除する |
||||
* |
||||
* @param username ユーザー名 |
||||
* @param taskId 削除する在庫のID |
||||
*/ |
||||
public void deleteStock(String username, Long stockId) { |
||||
Stocks stock = getStockById(username, stockId); |
||||
stocksRepository.delete(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…
Reference in new issue