source: jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/service/impl/ApplicationServiceImpl.java@ b248810

main
Last change on this file since b248810 was 28b3398, checked in by 223021 <daniel.ilievski.2@…>, 3 weeks ago

Implemented job application functionality, added job advertisement filtering and replaced text areas with editors

  • Property mode set to 100644
File size: 5.9 KB
Line 
1package mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.service.impl;
2
3import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.applications.DTO.ApplicationDetailsDTO;
4import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.applications.DTO.ApplicationStatusDTO;
5import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.enumerations.ApplicationStatus;
6import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.JobSeeker;
7import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.repositories.JobSeekerRepository;
8import org.springframework.core.io.Resource;
9import org.springframework.core.io.UrlResource;
10import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.applications.Application;
11import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.applications.DTO.ApplicationDTO;
12import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.job_advertisements.JobAdvertisement;
13import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.User;
14import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.repositories.ApplicationRepository;
15import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.repositories.JobAdvertisementRepository;
16import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.repositories.UserRepository;
17import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.service.intef.ApplicationService;
18import org.springframework.beans.factory.annotation.Autowired;
19import org.springframework.beans.factory.annotation.Value;
20import org.springframework.stereotype.Service;
21
22import java.io.IOException;
23import java.nio.file.Files;
24import java.nio.file.Path;
25import java.nio.file.Paths;
26import java.nio.file.StandardCopyOption;
27import java.util.ArrayList;
28import java.util.List;
29
30@Service
31//@RequiredArgsConstructor
32public 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}
Note: See TracBrowser for help on using the repository browser.