package it.finki.tinki.service.impl; import it.finki.tinki.model.Users.User; import it.finki.tinki.model.Work.Internship; import it.finki.tinki.model.Work.Job; import it.finki.tinki.model.Work.Project; import it.finki.tinki.model.Skill; import it.finki.tinki.model.Users.Account; import it.finki.tinki.model.dto.response.work.InternshipResponseDTO; import it.finki.tinki.model.dto.response.work.JobResponseDTO; import it.finki.tinki.model.dto.response.work.ProjectResponseDTO; import it.finki.tinki.model.enumerator.AccountType; import it.finki.tinki.repository.*; import it.finki.tinki.service.AccountService; import it.finki.tinki.service.MatchmakerService; import it.finki.tinki.service.SkillService; import it.finki.tinki.service.WorkService; import org.springframework.stereotype.Service; import java.util.*; @Service public class WorkServiceImpl implements WorkService { JobRepository jobRepository; InternshipRepository internshipRepository; ProjectRepository projectRepository; MatchRepository matchRepository; MatchmakerService matchmakerService; SkillService skillService; AccountService accountService; UserRepository userRepository; public WorkServiceImpl(JobRepository jobRepository, InternshipRepository internshipRepository, ProjectRepository projectRepository, MatchRepository matchRepository, SkillService skillService, AccountService accountService, UserRepository userRepository, MatchmakerService matchmakerService) { this.jobRepository = jobRepository; this.internshipRepository = internshipRepository; this.projectRepository = projectRepository; this.matchRepository = matchRepository; this.skillService = skillService; this.accountService = accountService; this.userRepository = userRepository; this.matchmakerService = matchmakerService; } @Override public List getAllJobsByAccount(Long accId) { return this.jobRepository.findAllByAccount_Id(accId); } @Override public List getAllInternshipsByAccount(Long accId) { return this.internshipRepository.findAllByAccount_Id(accId); } @Override public List getAllProjectsByAccount(Long accId) { return this.projectRepository.findAllByAccount_Id(accId); } @Override public Job insertJob(String title, String description, Long adccId, int salary, List skillsRequired, AccountType type) { List skills = this.skillService.returnSkillsBasedOnId(skillsRequired); Account account = this.accountService.findByIdAndType(adccId, type); Job j = new Job(title, description, account, salary, skills); Job jb = this.jobRepository.save(j); List users = this.userRepository.findAll(); users.forEach(user -> { this.matchmakerService.setUpUserJobMatches(jb, user); }); return jb; } @Override public Internship insertInternship(String title, String description, Long adccId, int salary, List skillsTrained, int openSpots, AccountType type) { List skills = this.skillService.returnSkillsBasedOnId(skillsTrained); Account account = this.accountService.findByIdAndType(adccId, type); Internship j = new Internship(title, description, account, salary, skills, openSpots); Internship jb = this.internshipRepository.save(j); List users = this.userRepository.findAll(); users.forEach(user -> { this.matchmakerService.setUpUserInternshipMatches(jb, user); }); return jb; } @Override public Project insertProject(String title, String description, Long adccId, int salary, List skillsRequired, Date validUntil, AccountType type) { List skills = this.skillService.returnSkillsBasedOnId(skillsRequired); Account account = this.accountService.findByIdAndType(adccId, type); Project j = new Project(title, description, account, salary, skills, validUntil); Project jb = this.projectRepository.save(j); List users = this.userRepository.findAll(); users.forEach(user -> { this.matchmakerService.setUpUserProjectMatches(jb, user); }); return jb; } @Override public Job editJob(Long id, String title, String description, int salary) { Job j = this.jobRepository.findById(id).get(); j.setTitle(title); j.setDescription(description); j.setSalary(salary); return this.jobRepository.save(j); } @Override public Internship editInternship(Long id, String title, String description, int salary, int openSpots) { Internship j = this.internshipRepository.findById(id).get(); j.setTitle(title); j.setDescription(description); j.setSalary(salary); return this.internshipRepository.save(j); } @Override public Project editProject(Long id, String title, String description, int salary) { Project j = this.projectRepository.findById(id).get(); j.setTitle(title); j.setDescription(description); j.setSalary(salary); return this.projectRepository.save(j); } @Override public List fullTextJobSearch(String text) { List skills = this.skillService.returnBasedOnText(text); List jobs = this.jobRepository.findAllByTitleContainsOrDescriptionContains(text,text); List sjob = new ArrayList<>(); skills.forEach(skill -> { sjob.addAll(this.jobRepository.findAllBySkillsRequiredContaining(skill)); }); sjob.forEach(job -> { if(!jobs.contains(job)){ jobs.add(job); } }); List jobs2 = new ArrayList<>(); jobs.forEach(job -> { jobs2.add(new JobResponseDTO(job)); }); return jobs2; } @Override public List fullTextInternshipSearch(String text) { List skills = this.skillService.returnBasedOnText(text); List jobs = this.internshipRepository.findAllByTitleContainsOrDescriptionContains(text,text); List sjob = new ArrayList<>(); skills.forEach(skill -> { sjob.addAll(this.internshipRepository.findAllBySkillsTrainedContaining(skill)); }); sjob.forEach(job -> { if(!jobs.contains(job)){ jobs.add(job); } }); List jobs2 = new ArrayList<>(); jobs.forEach(job -> { jobs2.add(new InternshipResponseDTO(job)); }); return jobs2; } @Override public List fullTextProjectSearch(String text) { List skills = this.skillService.returnBasedOnText(text); List jobs = this.projectRepository.findAllByTitleContainsOrDescriptionContains(text,text); List sjob = new ArrayList<>(); skills.forEach(skill -> { sjob.addAll(this.projectRepository.findAllBySkillsRequiredContaining(skill)); }); sjob.forEach(job -> { if(!jobs.contains(job)){ jobs.add(job); } }); List jobs2 = new ArrayList<>(); jobs.forEach(job -> { jobs2.add(new ProjectResponseDTO(job)); }); return jobs2; } @Override public Job getJobById(Long id) { return this.jobRepository.findById(id).get(); } @Override public Internship getInternshipById(Long id) { return this.internshipRepository.findById(id).get(); } @Override public Project getProjectById(Long id) { return this.projectRepository.findById(id).get(); } }