Files
smartproc-be/src/main/java/com/iconplus/smartproc/service/drp/PostCreateTahunDrpService.java
dirgantarasiahaan 06058d279e upload dokumen
2023-05-30 11:12:25 +07:00

46 lines
1.5 KiB
Java

package com.iconplus.smartproc.service.drp;
import com.iconplus.smartproc.exception.BusinessException;
import com.iconplus.smartproc.helper.service.BaseService;
import com.iconplus.smartproc.model.entity.Drp;
import com.iconplus.smartproc.model.request.DrpRequest;
import com.iconplus.smartproc.model.response.DrpResponse;
import com.iconplus.smartproc.repository.DrpRepository;
import com.iconplus.smartproc.util.Constants;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
public class PostCreateTahunDrpService implements BaseService<DrpRequest, DrpResponse> {
private DrpRepository drpRepository;
public PostCreateTahunDrpService(DrpRepository drpRepository) {
this.drpRepository = drpRepository;
}
@Override
public DrpResponse execute(DrpRequest input) {
validateTahunDrp(input);
Drp drp = Drp.builder()
.tahun(input.getTahun())
.isDelete(false)
.build();
var result = drpRepository.save(drp);
return DrpResponse.builder()
.id(result.getId())
.build();
}
private void validateTahunDrp(DrpRequest input) {
Optional<Drp> drpOptional = drpRepository.findByTahunAndIsDeleteFalse(input.getTahun());
if (drpOptional.isPresent()) {
throw new BusinessException(Constants.ERR_CODE_10010,
Constants.ERR_TTL_10010,
Constants.ERR_MSG_10010);
}
}
}