[28b3398] | 1 | package mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.service.impl;
|
---|
| 2 |
|
---|
| 3 | import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.applications.DTO.ApplicationDetailsDTO;
|
---|
| 4 | import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.applications.DTO.ApplicationStatusDTO;
|
---|
| 5 | import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.enumerations.ApplicationStatus;
|
---|
| 6 | import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.JobSeeker;
|
---|
| 7 | import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.repositories.JobSeekerRepository;
|
---|
| 8 | import org.springframework.core.io.Resource;
|
---|
| 9 | import org.springframework.core.io.UrlResource;
|
---|
| 10 | import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.applications.Application;
|
---|
| 11 | import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.applications.DTO.ApplicationDTO;
|
---|
| 12 | import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.job_advertisements.JobAdvertisement;
|
---|
| 13 | import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.User;
|
---|
| 14 | import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.repositories.ApplicationRepository;
|
---|
| 15 | import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.repositories.JobAdvertisementRepository;
|
---|
| 16 | import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.repositories.UserRepository;
|
---|
| 17 | import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.service.intef.ApplicationService;
|
---|
| 18 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
| 19 | import org.springframework.beans.factory.annotation.Value;
|
---|
| 20 | import org.springframework.stereotype.Service;
|
---|
| 21 |
|
---|
| 22 | import java.io.IOException;
|
---|
| 23 | import java.nio.file.Files;
|
---|
| 24 | import java.nio.file.Path;
|
---|
| 25 | import java.nio.file.Paths;
|
---|
| 26 | import java.nio.file.StandardCopyOption;
|
---|
| 27 | import java.util.ArrayList;
|
---|
| 28 | import java.util.List;
|
---|
| 29 |
|
---|
| 30 | @Service
|
---|
| 31 | //@RequiredArgsConstructor
|
---|
| 32 | public class ApplicationServiceImpl implements ApplicationService {
|
---|
| 33 | private final Path fileStorageLocation;
|
---|
| 34 |
|
---|
| 35 | private final UserRepository userRepository;
|
---|
| 36 | private final ApplicationRepository applicationRepository;
|
---|
| 37 | private final JobAdvertisementRepository jobAdvertisementRepository;
|
---|
| 38 | private final JobSeekerRepository jobSeekerRepository;
|
---|
| 39 |
|
---|
| 40 | @Autowired
|
---|
| 41 | public ApplicationServiceImpl(@Value("${file.upload-dir}") String uploadDir, UserRepository userRepository, ApplicationRepository applicationRepository, JobAdvertisementRepository jobAdvertisementRepository,
|
---|
| 42 | JobSeekerRepository jobSeekerRepository) {
|
---|
| 43 | this.fileStorageLocation = Paths.get(uploadDir).toAbsolutePath().normalize();
|
---|
| 44 |
|
---|
| 45 | try {
|
---|
| 46 | Files.createDirectories(this.fileStorageLocation);
|
---|
| 47 | } catch (IOException ex) {
|
---|
| 48 | throw new RuntimeException("Could not create the directory where the uploaded files will be stored.", ex);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | this.userRepository = userRepository;
|
---|
| 52 | this.applicationRepository = applicationRepository;
|
---|
| 53 | this.jobAdvertisementRepository = jobAdvertisementRepository;
|
---|
| 54 | this.jobSeekerRepository = jobSeekerRepository;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | @Override
|
---|
| 58 | public ApplicationDetailsDTO submitApplication(ApplicationDTO applicationDTO) {
|
---|
| 59 |
|
---|
| 60 | if (applicationDTO.getResumeFile().isEmpty()) {
|
---|
| 61 | throw new RuntimeException("Failed to store empty file.");
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | Path targetLocation = this.fileStorageLocation.resolve(applicationDTO.getResumeFile().getOriginalFilename());
|
---|
| 65 | try {
|
---|
| 66 | Files.copy(applicationDTO.getResumeFile().getInputStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING);
|
---|
| 67 | } catch (IOException e) {
|
---|
| 68 | throw new RuntimeException(e);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | JobSeeker jobSeeker = jobSeekerRepository.findById(applicationDTO.getJobSeekerId()).orElseThrow(() -> new IllegalArgumentException("User not found"));
|
---|
| 72 | JobAdvertisement jobAdvertisement = jobAdvertisementRepository.findById(applicationDTO.getJobAdId()).orElseThrow(() -> new IllegalArgumentException("Job Ad not found"));
|
---|
| 73 |
|
---|
| 74 | List<String> answers = new ArrayList<>();
|
---|
| 75 | answers.add(applicationDTO.getAnswerOne());
|
---|
| 76 | answers.add(applicationDTO.getAnswerTwo());
|
---|
| 77 | answers.add(applicationDTO.getAnswerThree());
|
---|
| 78 |
|
---|
| 79 | Application application = new Application(jobSeeker, jobAdvertisement, applicationDTO.getResumeFile().getOriginalFilename(), answers, applicationDTO.getMessageToRecruiter());
|
---|
| 80 | applicationRepository.save(application);
|
---|
| 81 | return Application.mapToApplicationDetailsDTO(application);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | @Override
|
---|
| 85 | public List<ApplicationDetailsDTO> findAllByJobAdvertisementId(Long jobId) {
|
---|
| 86 | List<Application> applications = applicationRepository.findAllByJobAdvertisementId(jobId);
|
---|
| 87 | return applications.stream().map(Application::mapToApplicationDetailsDTO).toList();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | @Override
|
---|
| 91 | public List<ApplicationDetailsDTO> findAllByJobSeekerId(Long jobSeekerId) {
|
---|
| 92 | List<Application> applications = applicationRepository.findAllByJobSeekerId(jobSeekerId);
|
---|
| 93 | return applications.stream().map(Application::mapToApplicationDetailsDTO).toList();
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | @Override
|
---|
| 97 | public Resource loadResumeAsResource(String fileName) {
|
---|
| 98 | try {
|
---|
| 99 | Path filePath = fileStorageLocation.resolve(fileName).normalize();
|
---|
| 100 | Resource resource = new UrlResource(filePath.toUri());
|
---|
| 101 | if (resource.exists()) {
|
---|
| 102 | return resource;
|
---|
| 103 | } else {
|
---|
| 104 | throw new RuntimeException("File not found " + fileName);
|
---|
| 105 | }
|
---|
| 106 | } catch (IOException ex) {
|
---|
| 107 | throw new RuntimeException("File not found " + fileName, ex);
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | @Override
|
---|
| 112 | public ApplicationStatusDTO updateApplicationStatus(Long id, String status) {
|
---|
| 113 | Application application = applicationRepository.findById(id).orElse(null);
|
---|
| 114 | System.out.println(status);
|
---|
| 115 | application.setStatus(ApplicationStatus.valueOf(status));
|
---|
| 116 | applicationRepository.save(application);
|
---|
| 117 | return new ApplicationStatusDTO(id, status);
|
---|
| 118 | }
|
---|
| 119 | }
|
---|