parent
e40755598f
commit
9d24a254f2
@ -0,0 +1,45 @@ |
|||||||
|
package com.example.todoapp.controller; |
||||||
|
|
||||||
|
import com.example.todoapp.dto.StuffRequestDTO; |
||||||
|
import com.example.todoapp.model.Stuffs; |
||||||
|
import com.example.todoapp.service.StuffsService; |
||||||
|
|
||||||
|
import java.util.Collections; |
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.http.ResponseEntity; |
||||||
|
import org.springframework.web.bind.annotation.PostMapping; |
||||||
|
import org.springframework.web.bind.annotation.RequestBody; |
||||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||||
|
import org.springframework.web.bind.annotation.RestController; |
||||||
|
|
||||||
|
|
||||||
|
@RestController |
||||||
|
@RequestMapping("/stuffs") |
||||||
|
public class StuffsController { |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private StuffsService stuffsService; |
||||||
|
|
||||||
|
@PostMapping("/add") |
||||||
|
public ResponseEntity<?> createStuffs(@RequestBody StuffRequestDTO requestDTO) { |
||||||
|
try { |
||||||
|
Stuffs newStuffs = stuffsService.createStuffs(requestDTO); |
||||||
|
return ResponseEntity.ok(newStuffs); |
||||||
|
} catch (IllegalArgumentException e) { |
||||||
|
return ResponseEntity.badRequest().body(Collections.singletonMap("error", e.getMessage())); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@PostMapping("/get") |
||||||
|
public ResponseEntity<?> getStuffs(@RequestBody StuffRequestDTO requestDTO) { |
||||||
|
try { |
||||||
|
Long stuffId = requestDTO.getStuffs().getStuff_id(); |
||||||
|
Stuffs stuff = stuffsService.getStuff(stuffId); |
||||||
|
return ResponseEntity.ok(stuff); |
||||||
|
} catch (IllegalArgumentException e) { |
||||||
|
return ResponseEntity.badRequest().body(Collections.singletonMap("error", e.getMessage())); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,19 @@ |
|||||||
|
package com.example.todoapp.dto; |
||||||
|
|
||||||
|
import com.example.todoapp.model.Stuffs; |
||||||
|
import lombok.Data; |
||||||
|
|
||||||
|
@Data |
||||||
|
public class StuffDetailDTO { |
||||||
|
private Long stuff_id; |
||||||
|
private String stuff_name; |
||||||
|
private String summary; |
||||||
|
private String category; |
||||||
|
|
||||||
|
public StuffDetailDTO(Stuffs stuff) { |
||||||
|
this.stuff_id = stuff.getStuff_id(); |
||||||
|
this.stuff_name = stuff.getStuff_name(); |
||||||
|
this.summary = stuff.getSummary(); |
||||||
|
this.category = stuff.getCategory(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,34 @@ |
|||||||
|
package com.example.todoapp.dto; |
||||||
|
|
||||||
|
import com.example.todoapp.dto.StuffRequestDTO.StuffDTO; |
||||||
|
import com.example.todoapp.dto.StuffRequestDTO.StuffIdentifier; |
||||||
|
|
||||||
|
import lombok.Data; |
||||||
|
|
||||||
|
/** |
||||||
|
* タスクエンティティのリポジトリインターフェース |
||||||
|
* <p> |
||||||
|
* このインターフェースはタスクデータへのアクセスと操作を提供します。 |
||||||
|
* Spring Data JPAによって自動的に実装されます。 |
||||||
|
* </p> |
||||||
|
*/ |
||||||
|
@Data |
||||||
|
public class StuffRequestDTO { |
||||||
|
|
||||||
|
|
||||||
|
@Data |
||||||
|
public static class StuffDTO { |
||||||
|
private Long stuff_id; |
||||||
|
private String stuff_name; |
||||||
|
private String summary; |
||||||
|
private String category; |
||||||
|
} |
||||||
|
|
||||||
|
@Data |
||||||
|
public static class StuffIdentifier { |
||||||
|
private Long stuff_id; |
||||||
|
} |
||||||
|
|
||||||
|
private StuffDTO stuff; |
||||||
|
private StuffIdentifier stuffs; |
||||||
|
} |
@ -0,0 +1,21 @@ |
|||||||
|
package com.example.todoapp.repository; |
||||||
|
|
||||||
|
import com.example.todoapp.model.Stuffs; |
||||||
|
|
||||||
|
import java.util.Optional; |
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||||
|
import org.springframework.stereotype.Repository; |
||||||
|
|
||||||
|
/** |
||||||
|
* タスクエンティティのリポジトリインターフェース |
||||||
|
* <p> |
||||||
|
* このインターフェースはタスクデータへのアクセスと操作を提供します。 |
||||||
|
* Spring Data JPAによって自動的に実装されます。 |
||||||
|
* </p> |
||||||
|
*/ |
||||||
|
@Repository |
||||||
|
public interface StuffsRepository extends JpaRepository<Stuffs, Long> { |
||||||
|
|
||||||
|
Optional<Stuffs> findById(Long stuff_id); |
||||||
|
} |
@ -0,0 +1,53 @@ |
|||||||
|
package com.example.todoapp.service; |
||||||
|
|
||||||
|
import com.example.todoapp.dto.StuffRequestDTO; |
||||||
|
import com.example.todoapp.model.Stuffs; |
||||||
|
import com.example.todoapp.repository.StuffsRepository; |
||||||
|
|
||||||
|
import java.util.Optional; |
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.stereotype.Service; |
||||||
|
import org.springframework.transaction.annotation.Transactional; |
||||||
|
|
||||||
|
/** |
||||||
|
* 認証機能のサービスクラス |
||||||
|
* <p> |
||||||
|
* このクラスはユーザー登録とログイン処理のビジネスロジックを提供します。 |
||||||
|
* 認証に成功した場合、JWTトークンを発行します。 |
||||||
|
* </p> |
||||||
|
*/ |
||||||
|
@Service |
||||||
|
public class StuffsService { |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private StuffsRepository stuffsRepository; |
||||||
|
|
||||||
|
@Transactional |
||||||
|
public Stuffs createStuffs(StuffRequestDTO requestDTO) { |
||||||
|
|
||||||
|
Stuffs stuffs = new Stuffs(); |
||||||
|
stuffs.setStuff_name(requestDTO.getStuff().getStuff_name()); |
||||||
|
stuffs.setSummary(requestDTO.getStuff().getSummary()); |
||||||
|
stuffs.setCategory(requestDTO.getStuff().getCategory()); |
||||||
|
|
||||||
|
return stuffsRepository.save(stuffs); |
||||||
|
} |
||||||
|
|
||||||
|
@Transactional |
||||||
|
public void deteleStuffs(Long stuffId) { |
||||||
|
stuffsRepository.deleteById(stuffId); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
// 查询食材方法(移除用户验证)
|
||||||
|
@Transactional(readOnly = true) |
||||||
|
public Stuffs getStuff(Long stuffId) { |
||||||
|
Optional<Stuffs> stuffOpt = stuffsRepository.findById(stuffId); |
||||||
|
if (stuffOpt.isPresent()) { |
||||||
|
return stuffOpt.get(); |
||||||
|
} else { |
||||||
|
throw new RuntimeException("no: " + stuffId); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue