|
|
@@ -3,56 +3,88 @@ package com.its.op.common.service;
|
|
|
import com.its.op.common.dto.TbAtrdDto;
|
|
|
import com.its.op.common.entity.TbAtrd;
|
|
|
import com.its.op.common.repository.TbAtrdRepository;
|
|
|
-import com.its.op.common.vo.TbAtrdQueryVo;
|
|
|
-import com.its.op.common.vo.TbAtrdUpdateVo;
|
|
|
-import com.its.op.common.vo.TbAtrdVo;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
-import org.springframework.beans.BeanUtils;
|
|
|
-import org.springframework.data.domain.Page;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
import java.util.NoSuchElementException;
|
|
|
+import java.util.Optional;
|
|
|
|
|
|
+@Slf4j
|
|
|
@RequiredArgsConstructor
|
|
|
@Service
|
|
|
public class TbAtrdService {
|
|
|
|
|
|
- private final TbAtrdRepository tbAtrdRepository;
|
|
|
+ private final TbAtrdRepository repo;
|
|
|
|
|
|
- public String save(TbAtrdVo vo) {
|
|
|
- TbAtrd bean = new TbAtrd();
|
|
|
- BeanUtils.copyProperties(vo, bean);
|
|
|
- bean = tbAtrdRepository.save(bean);
|
|
|
- return bean.getAtrdId();
|
|
|
+ // 데이터 1건 조회, 없으면 exception
|
|
|
+ private TbAtrd requireOne(String id) {
|
|
|
+ return repo.findById(id)
|
|
|
+ .orElseThrow(() -> new NoSuchElementException("데이터가 존재하지 않습니다: " + id));
|
|
|
}
|
|
|
|
|
|
- public void delete(String id) {
|
|
|
- tbAtrdRepository.deleteById(id);
|
|
|
+ // 전체 데이터 조회
|
|
|
+ public List<TbAtrdDto> findAll() {
|
|
|
+ List<TbAtrdDto> result = new ArrayList<>();
|
|
|
+ List<TbAtrd> data = this.repo.findAll();
|
|
|
+ for (TbAtrd entity : data) {
|
|
|
+ result.add(entity.toDto());
|
|
|
+ }
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
- public void update(String id, TbAtrdUpdateVo vo) {
|
|
|
- TbAtrd bean = requireOne(id);
|
|
|
- BeanUtils.copyProperties(vo, bean);
|
|
|
- tbAtrdRepository.save(bean);
|
|
|
+ // 데이터 1건 조회(기존 데이터가 반드시 존재해야 함)
|
|
|
+ public TbAtrdDto findById(String id) {
|
|
|
+ TbAtrd entity = requireOne(id);
|
|
|
+ return entity.toDto();
|
|
|
}
|
|
|
|
|
|
- public TbAtrdDto getById(String id) {
|
|
|
- TbAtrd original = requireOne(id);
|
|
|
- return toDto(original);
|
|
|
+ // 데이터 변경
|
|
|
+ public TbAtrdDto updateById(String id, TbAtrdDto.TbAtrdUpdReq req) {
|
|
|
+ TbAtrd entity = requireOne(id);
|
|
|
+ entity.updateInfo(req);
|
|
|
+ this.repo.save(entity);
|
|
|
+ return entity.toDto();
|
|
|
}
|
|
|
|
|
|
- public Page<TbAtrdDto> query(TbAtrdQueryVo vo) {
|
|
|
- throw new UnsupportedOperationException();
|
|
|
+ // 데이터 변경 또는 생성-목록(데이터가 존재하면 업데이트 없으면 신규로 생성)
|
|
|
+ public List<TbAtrdDto> mergeInfoList(List<TbAtrdDto.TbAtrdUpdReq> reqList) {
|
|
|
+ List<TbAtrdDto> result = new ArrayList<>();
|
|
|
+ for (TbAtrdDto.TbAtrdUpdReq req : reqList) {
|
|
|
+ TbAtrd obj = req.toEntity();
|
|
|
+ this.repo.save(obj);
|
|
|
+ result.add(obj.toDto());
|
|
|
+ }
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
- private TbAtrdDto toDto(TbAtrd original) {
|
|
|
- TbAtrdDto bean = new TbAtrdDto();
|
|
|
- BeanUtils.copyProperties(original, bean);
|
|
|
- return bean;
|
|
|
+ // 데이터 변경 또는 생성-개별(데이터가 존재하면 업데이트 없으면 신규로 생성)
|
|
|
+ public TbAtrdDto mergeInfo(TbAtrdDto.TbAtrdUpdReq req) {
|
|
|
+ TbAtrd obj = req.toEntity();
|
|
|
+ this.repo.save(obj);
|
|
|
+ return obj.toDto();
|
|
|
}
|
|
|
|
|
|
- private TbAtrd requireOne(String id) {
|
|
|
- return tbAtrdRepository.findById(id)
|
|
|
- .orElseThrow(() -> new NoSuchElementException("Resource not found: " + id));
|
|
|
+ // 정보 삭제-개별, 데이터 존재하지 않으면 Exception
|
|
|
+ public TbAtrdDto deleteById(String id) {
|
|
|
+ TbAtrd entity = requireOne(id);
|
|
|
+ this.repo.deleteById(id);
|
|
|
+ return entity.toDto();
|
|
|
}
|
|
|
+
|
|
|
+ // 정보 삭제-목록, 존재하는 데이터 만 삭제
|
|
|
+ public List<TbAtrdDto> deleteByIds(List<String> ids) {
|
|
|
+ List<TbAtrdDto> result = new ArrayList<>();
|
|
|
+ for (String id : ids) {
|
|
|
+ Optional<TbAtrd> obj = this.repo.findById(id);
|
|
|
+ if (obj.isPresent()) {
|
|
|
+ this.repo.deleteById(id);
|
|
|
+ result.add(obj.get().toDto());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
}
|