parent
e40755598f
commit
cb2ffbd493
@ -0,0 +1,22 @@ |
|||||||
|
package com.example.todoapp.controller; |
||||||
|
|
||||||
|
import com.example.todoapp.dto.ToBuyDTO; |
||||||
|
import com.example.todoapp.service.ToBuysService; |
||||||
|
import jakarta.validation.Valid; |
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.http.ResponseEntity; |
||||||
|
import org.springframework.web.bind.annotation.*; |
||||||
|
|
||||||
|
@RestController |
||||||
|
@RequestMapping("/api/tobuy/add") |
||||||
|
public class ToBuysController { |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private ToBuysService toBuysService; |
||||||
|
|
||||||
|
@PostMapping |
||||||
|
public ResponseEntity<String> addToBuys(@Valid @RequestBody ToBuyDTO dto) { |
||||||
|
toBuysService.addToBuys(dto); |
||||||
|
return ResponseEntity.ok("Item added to 'To Buys' successfully"); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,18 @@ |
|||||||
|
package com.example.todoapp.dto; |
||||||
|
|
||||||
|
import lombok.Data; |
||||||
|
/** |
||||||
|
* カテゴリDTOクラス |
||||||
|
* このクラスはタスクのカテゴリ情報を表します。 |
||||||
|
* カテゴリは名前、色、所有ユーザーなどの情報を持ちます。 |
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
@Data |
||||||
|
public class ToBuyDTO { |
||||||
|
private Long stuffsId; |
||||||
|
private Long userId; |
||||||
|
|
||||||
|
private int amount; |
||||||
|
private String store; |
||||||
|
} |
@ -0,0 +1,12 @@ |
|||||||
|
package com.example.todoapp.repository; |
||||||
|
|
||||||
|
import com.example.todoapp.model.Stuffs; |
||||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||||
|
import org.springframework.stereotype.Repository; |
||||||
|
|
||||||
|
import java.util.Optional; |
||||||
|
|
||||||
|
@Repository |
||||||
|
public interface StuffsRepository extends JpaRepository<Stuffs, Long> { |
||||||
|
Optional<Stuffs> findById(Long id); |
||||||
|
} |
@ -0,0 +1,9 @@ |
|||||||
|
package com.example.todoapp.repository; |
||||||
|
|
||||||
|
import com.example.todoapp.model.ToBuys; |
||||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||||
|
import org.springframework.stereotype.Repository; |
||||||
|
|
||||||
|
@Repository |
||||||
|
public interface ToBuysRepository extends JpaRepository<ToBuys, Integer> { |
||||||
|
} |
@ -0,0 +1,51 @@ |
|||||||
|
package com.example.todoapp.service; |
||||||
|
|
||||||
|
import com.example.todoapp.dto.ToBuyDTO; |
||||||
|
import com.example.todoapp.model.Stuffs; |
||||||
|
import com.example.todoapp.model.ToBuys; |
||||||
|
import com.example.todoapp.model.User; |
||||||
|
import com.example.todoapp.repository.StuffsRepository; |
||||||
|
import com.example.todoapp.repository.ToBuysRepository; |
||||||
|
import com.example.todoapp.repository.UserRepository; |
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.stereotype.Service; |
||||||
|
|
||||||
|
import java.util.Optional; |
||||||
|
|
||||||
|
@Service |
||||||
|
public class ToBuysService { |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private ToBuysRepository toBuysRepository; |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private UserRepository userRepository; |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private StuffsRepository stuffsRepository; |
||||||
|
|
||||||
|
public void addToBuys(ToBuyDTO toBuyDTO) { |
||||||
|
|
||||||
|
//get user
|
||||||
|
Optional<User> user = userRepository.findById(toBuyDTO.getUserId()); |
||||||
|
if (!user.isPresent()) { |
||||||
|
throw new RuntimeException("no: " + toBuyDTO.getUserId()); |
||||||
|
} |
||||||
|
|
||||||
|
//get stuffs
|
||||||
|
Optional<Stuffs> stuffs = stuffsRepository.findById(toBuyDTO.getStuffsId()); |
||||||
|
if (!stuffs.isPresent()) { |
||||||
|
throw new RuntimeException("材料不存在"); |
||||||
|
} |
||||||
|
|
||||||
|
ToBuys toBuys = new ToBuys(); |
||||||
|
toBuys.setUser_id(user.get()); |
||||||
|
toBuys.setStuffs(stuffs.get()); |
||||||
|
toBuys.setAmount(toBuyDTO.getAmount()); |
||||||
|
toBuys.setStore(toBuyDTO.getStore()); |
||||||
|
|
||||||
|
// 保存到数据库
|
||||||
|
toBuysRepository.save(toBuys); |
||||||
|
|
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue