package com.pollex.pam.service; import java.util.List; import org.hibernate.boot.model.naming.IllegalIdentifierException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.pollex.pam.domain.Appointment; import com.pollex.pam.domain.InterviewRecord; import com.pollex.pam.enums.InterviewRecordStatusEnum; import com.pollex.pam.repository.InterviewRecordRepository; import com.pollex.pam.security.SecurityUtils; import com.pollex.pam.service.dto.InterviewRecordDTO; import com.pollex.pam.service.mapper.InterviewRecordMapper; import com.pollex.pam.web.rest.errors.InterviewRecordNotFoundException; @Service @Transactional public class InterviewRecordService { @Autowired InterviewRecordRepository interviewRecordRepository; @Autowired InterviewRecordMapper interviewRecordMapper; @Autowired AppointmentService appointmentService; public InterviewRecord create(InterviewRecordDTO dto) { if(dto.getId()!=null) { throw new IllegalArgumentException(); } InterviewRecord record = interviewRecordMapper.toInterviewRecord(dto); checkAuth(record); record.setStatus(InterviewRecordStatusEnum.AVAILABLE); interviewRecordRepository.save(record); return record; } public InterviewRecord update(InterviewRecordDTO dto) { if(dto.getId()==null) { throw new IllegalArgumentException(); } InterviewRecord record = findById(dto.getId()); checkAuth(record); interviewRecordMapper.copyToInterviewRecord(dto, record); interviewRecordRepository.save(record); return record; } public void checkAuth(InterviewRecord record) { Appointment appointment = appointmentService.findById(record.getAppointmentId()); if(!appointment.getAgentNo().equals(SecurityUtils.getAgentNo())) { throw new IllegalAccessError("The account can not edit the appointment"); } } public void delete(Long interviewRecordId) { InterviewRecord record = findById(interviewRecordId); record.setStatus(InterviewRecordStatusEnum.DELETED); interviewRecordRepository.save(record); } public InterviewRecord findById(Long id) { return interviewRecordRepository.findById(id) .orElseThrow(InterviewRecordNotFoundException::new); } public List findByAppointmentId(Long appointmentId) { List records = interviewRecordRepository.findByAppointmentId(appointmentId); return interviewRecordMapper.toInterviewRecordDTO(records); } public List findByAppointmentIdAndStatus(Long appointmentId, InterviewRecordStatusEnum status) { List records = interviewRecordRepository.findByAppointmentIdAndStatus(appointmentId, status); return interviewRecordMapper.toInterviewRecordDTO(records); } }