package com.its.op.service.its.rse;

import com.its.op.dao.repository.its.rse.TbRseSectPassHsRepository;
import com.its.op.dto.its.rse.TbRseSectPassHsDto;
import com.its.op.entity.its.rse.TbRseSectPassHs;
import com.its.op.entity.its.rse.TbRseSectPassHsKey;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;

@Slf4j
@RequiredArgsConstructor
@Service
public class TbRseSectPassHsService {

    private final TbRseSectPassHsRepository repo;

    // 데이터 1건 조회, 없으면 exception
    private TbRseSectPassHs requireOne(TbRseSectPassHsKey id) throws NoSuchElementException {
        Optional<TbRseSectPassHs> info = this.repo.findById(id);
        if (info.isPresent()) {
            return info.get();
        }
        else {
            throw new NoSuchElementException("데이터가 존재하지 않습니다: " + id);
        }
//        return this.repo.findById(id)
//                .orElseThrow(() -> new NoSuchElementException("데이터가 존재하지 않습니다: " + id));
    }

    // 전체 데이터 조회
    @Transactional(readOnly = true)
    public List<TbRseSectPassHsDto> findAll() {
        List<TbRseSectPassHsDto> result = new ArrayList<>();
        List<TbRseSectPassHs> data = this.repo.findAll();
        for (TbRseSectPassHs entity : data) {
            result.add(entity.toDto());
        }
        return result;
    }

}
