package space.nstart.pcp.pcp_stations_service.service

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import space.nstart.pcp.pcp_stations_service.configuration.CustomErrorException
import space.nstart.pcp.pcp_stations_service.entity.StationEntity
import space.nstart.pcp.pcp_stations_service.repository.StationRepository
import space.nstart.pcp.pcp_types_lib.dto.ballistics.StationDTO
import java.util.UUID

@Service
class StationService {


    @Autowired
    private lateinit var stationRepository: StationRepository



    fun all() = stationRepository.findAll().map { it.toDTO() }

    fun add(station : StationDTO): StationDTO = stationRepository.save(StationEntity(station)).toDTO()

    fun byId(id : UUID): StationDTO =
        stationRepository.findByStationId(id)
            .map { it.toDTO() }
            .orElseThrow { CustomErrorException("Станция $id не найдена") }

    fun delete(id : UUID): Int = stationRepository.deleteByStationId(id)
}
