保誠-保戶業務員媒合平台
wayne
2022-02-17 a3716f72066d25d745f4d5103ff23a553c3e102b
pamapi/src/main/java/com/pollex/pam/service/AppointmentMemoService.java
比對新檔案
@@ -0,0 +1,60 @@
package com.pollex.pam.service;
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.AppointmentMemo;
import com.pollex.pam.repository.AppointmentMemoRepository;
import com.pollex.pam.repository.AppointmentRepository;
import com.pollex.pam.security.SecurityUtils;
import com.pollex.pam.service.dto.AppointmentMemoCreateDTO;
import com.pollex.pam.service.dto.AppointmentMemoUpdateDTO;
import com.pollex.pam.service.mapper.AppointmentMemoMapper;
import com.pollex.pam.web.rest.errors.AppointmentMemoNotFoundException;
import com.pollex.pam.web.rest.errors.AppointmentNotFoundException;
@Service
@Transactional
public class AppointmentMemoService {
   @Autowired
   AppointmentMemoRepository appointmentMemoRepository;
   @Autowired
   AppointmentMemoMapper appointmentMemoMapper;
   @Autowired
   AppointmentRepository appointmentRepository;
   public AppointmentMemo create(AppointmentMemoCreateDTO memoDTO) {
      AppointmentMemo memo = appointmentMemoMapper.toAppointmentMemo(memoDTO);
      return appointmentMemoRepository.save(memo);
   }
   public void checkPermission(Long appointmentId) {
      Appointment appointment = appointmentRepository.findById(appointmentId)
            .orElseThrow(AppointmentNotFoundException::new);
      if(!appointment.getAgentNo().equals(SecurityUtils.getAgentNo())) {
         throw new IllegalAccessError("not have permission");
      }
   }
   public AppointmentMemo update(AppointmentMemoUpdateDTO memoDTO) {
      AppointmentMemo memo = appointmentMemoRepository
            .findById(memoDTO.getId())
            .orElseThrow(AppointmentMemoNotFoundException::new);
      checkPermission(memo.getAppointmentId());
      appointmentMemoMapper.copyToAppointmentMemo(memoDTO, memo);
      return appointmentMemoRepository.save(memo);
   }
   public void delete(Long memoId) {
      AppointmentMemo memo = appointmentMemoRepository
            .findById(memoId)
            .orElseThrow(AppointmentMemoNotFoundException::new);
      checkPermission(memo.getAppointmentId());
      appointmentMemoRepository.delete(memo);
   }
}