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

main
Last change on this file was 08f82ec, checked in by 223021 <daniel.ilievski.2@…>, 2 weeks ago

Did more refactoring

  • Property mode set to 100644
File size: 6.7 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 + "/applications").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 JobSeeker jobSeeker = jobSeekerRepository.findById(applicationDTO.getJobSeekerId())
61 .orElseThrow(() -> new IllegalArgumentException("User not found."));
62 JobAdvertisement jobAdvertisement = jobAdvertisementRepository.findById(applicationDTO.getJobAdId())
63 .orElseThrow(() -> new IllegalArgumentException("Job advertisement not found."));
64
65 if (applicationDTO.getResumeFile().isEmpty()) {
66 throw new RuntimeException("Failed to store empty file.");
67 }
68
69 List<String> answers = new ArrayList<>();
70 answers.add(applicationDTO.getAnswerOne());
71 answers.add(applicationDTO.getAnswerTwo());
72 answers.add(applicationDTO.getAnswerThree());
73
74 Application application = new Application(jobSeeker, jobAdvertisement,
75 answers,applicationDTO.getMessageToRecruiter());
76 application = applicationRepository.save(application);
77
78 Path filePath = this.fileStorageLocation.resolve(String.valueOf(application.getId())).resolve("resume");
79 Path targetLocation = filePath.resolve(applicationDTO.getResumeFile().getOriginalFilename());
80
81 try {
82 Files.createDirectories(filePath);
83 Files.copy(applicationDTO.getResumeFile().getInputStream(), targetLocation);
84 } catch (IOException e) {
85 throw new RuntimeException(e);
86 }
87
88 String relativePath = Paths.get("uploads","applications",String.valueOf(application.getId()),
89 "resume", applicationDTO.getResumeFile().getOriginalFilename()).toString();
90 application.setResumeFilePath(relativePath);
91 application = applicationRepository.save(application);
92
93 return Application.mapToApplicationDetailsDTO(application);
94 }
95
96 @Override
97 public List<ApplicationDetailsDTO> findAllByJobAdvertisementId(Long jobId) {
98 List<Application> applications = applicationRepository.findAllByJobAdvertisementId(jobId);
99 return applications.stream().map(Application::mapToApplicationDetailsDTO).toList();
100 }
101
102 @Override
103 public List<ApplicationDetailsDTO> findAllByJobSeekerId(Long jobSeekerId) {
104 List<Application> applications = applicationRepository.findAllByJobSeekerId(jobSeekerId);
105 return applications.stream().map(Application::mapToApplicationDetailsDTO).toList();
106 }
107
108 @Override
109 public Resource loadResumeAsResource(Long applicationId) {
110 Application application = applicationRepository.findById(applicationId).
111 orElseThrow(() -> new IllegalArgumentException("Application not found"));
112
113 String relativeFilePath = application.getResumeFilePath();
114 Path filePath = fileStorageLocation.getParent().getParent().resolve(relativeFilePath).normalize();
115
116 try {
117 Resource resource = new UrlResource(filePath.toUri());
118 if (resource.exists()) {
119 return resource;
120 } else {
121 throw new RuntimeException("File not found: " + relativeFilePath);
122 }
123 } catch (IOException ex) {
124 throw new RuntimeException("File path is invalid: " + relativeFilePath, ex);
125 }
126 }
127
128 @Override
129 public ApplicationStatusDTO updateApplicationStatus(Long id, String status) {
130 Application application = applicationRepository.findById(id).orElse(null);
131 System.out.println(status);
132 application.setStatus(ApplicationStatus.valueOf(status));
133 applicationRepository.save(application);
134 return new ApplicationStatusDTO(id, status);
135 }
136}
Note: See TracBrowser for help on using the repository browser.