TbFcltSubjService.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package com.its.op.service.its.facility;
  2. import com.its.op.dao.repository.its.facility.TbFcltSubjRepository;
  3. import com.its.op.dto.its.common.NewIdLongDto;
  4. import com.its.op.dto.its.facility.TbFcltSubjDto;
  5. import com.its.op.entity.its.facility.TbFcltSubj;
  6. import lombok.RequiredArgsConstructor;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.springframework.stereotype.Service;
  9. import org.springframework.transaction.annotation.Transactional;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.NoSuchElementException;
  13. import java.util.Optional;
  14. @Slf4j
  15. @RequiredArgsConstructor
  16. @Service
  17. public class TbFcltSubjService {
  18. private final TbFcltSubjRepository repo;
  19. // 데이터 1건 조회, 없으면 exception
  20. private TbFcltSubj requireOne(Long id) throws NoSuchElementException {
  21. Optional<TbFcltSubj> info = this.repo.findById(id);
  22. if (info.isPresent()) {
  23. return info.get();
  24. }
  25. else {
  26. throw new NoSuchElementException("데이터가 존재하지 않습니다: " + id);
  27. }
  28. }
  29. // 전체 데이터 조회
  30. @Transactional(readOnly = true)
  31. public List<TbFcltSubjDto> findAll() {
  32. List<TbFcltSubjDto> result = new ArrayList<>();
  33. List<TbFcltSubj> data = this.repo.findAll();
  34. for (TbFcltSubj entity : data) {
  35. result.add(entity.toDto());
  36. }
  37. return result;
  38. }
  39. // 데이터 1건 조회(기존 데이터가 반드시 존재해야 함)
  40. @Transactional(readOnly = true)
  41. public TbFcltSubjDto findById(Long id) {
  42. TbFcltSubj entity = requireOne(id);
  43. return entity.toDto();
  44. }
  45. // 데이터 변경 또는 생성-개별(데이터가 존재하면 업데이트 없으면 신규로 생성)
  46. @Transactional
  47. public TbFcltSubjDto mergeInfo(TbFcltSubjDto.TbFcltSubjUpdReq req) {
  48. TbFcltSubj obj = req.toEntity();
  49. this.repo.save(obj);
  50. return obj.toDto();
  51. }
  52. // 정보 삭제-개별, 데이터 존재하지 않으면 Exception
  53. @Transactional
  54. public TbFcltSubjDto deleteById(Long id) {
  55. TbFcltSubj entity = requireOne(id);
  56. this.repo.deleteById(id);
  57. return entity.toDto();
  58. }
  59. @Transactional(readOnly = true)
  60. public NewIdLongDto getNewNmbr() {
  61. Long newId = this.repo.getNewNmbr();
  62. return NewIdLongDto.builder().newId(newId).build();
  63. }
  64. @Transactional(readOnly = true)
  65. public NewIdLongDto getNewSubjId(String fcltType) {
  66. Long newId = this.repo.getNewSubjId(fcltType);
  67. return NewIdLongDto.builder().newId(newId).build();
  68. }
  69. @Transactional(readOnly = true)
  70. public List<TbFcltSubjDto> findAllByFcltType(String fcltType) {
  71. List<TbFcltSubjDto> result = new ArrayList<>();
  72. List<TbFcltSubj> data = this.repo.findAllByFcltType(fcltType);
  73. for (TbFcltSubj entity : data) {
  74. result.add(entity.toDto());
  75. }
  76. return result;
  77. }
  78. }