Ignore:
Timestamp:
01/11/21 18:57:03 (4 years ago)
Author:
Vzdra <vladko.zdravkovski@…>
Branches:
master
Children:
8f1f460
Parents:
fc8421e
Message:

refactoring controllers

Location:
src/main/java/it/finki/tinki/web/controller
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/it/finki/tinki/web/controller/AccountEditController.java

    rfc8421e r5f9d25a  
    1313import it.finki.tinki.model.enumerator.AccountType;
    1414import it.finki.tinki.service.AccountService;
     15import it.finki.tinki.service.BuilderService;
    1516import it.finki.tinki.service.SkillService;
    1617import org.springframework.web.bind.annotation.*;
     
    2627    AccountService accountService;
    2728    SkillService skillService;
     29    BuilderService builderService;
    2830
    29     public AccountEditController(AccountService accountService, SkillService skillService) {
     31    public AccountEditController(AccountService accountService, SkillService skillService, BuilderService builderService) {
    3032        this.accountService = accountService;
    3133        this.skillService = skillService;
     34        this.builderService = builderService;
    3235    }
    3336
    34     @PostMapping(path = "/user/{id}/{email}")
     37    @PostMapping(path = "/user/{id}")
    3538    public UserResponseDTO editUser(@PathVariable(name = "id") Long id,
    36                                     @PathVariable(name = "email") String email,
    3739                                    @RequestBody UserRegisterDTO body){
    3840
    39         Optional<?> a = this.accountService.findByIdAndEmail(id, email, AccountType.USER);
     41        Optional<?> a = this.accountService.findByIdAndEmail(id, body.getEmail(), AccountType.USER);
    4042
    4143        if(a.isPresent()){
    4244            List<Skill> retained = this.skillService.returnSkillsBasedOnId(body.getRetainedSkills());
    4345            List<Skill> toLearn = this.skillService.returnSkillsBasedOnId(body.getSkillsToLearn());
    44 
    4546            User u = this.accountService.editUser(id, body.getEmail(), body.getName(), body.getSurname(), retained, toLearn);
    46 
    47             UserResponseDTO userResponseDTO = new UserResponseDTO();
    48 
    49             userResponseDTO.setId(u.getId());
    50             userResponseDTO.setEmail(u.getEmail());
    51             userResponseDTO.setType(AccountType.USER);
    52             userResponseDTO.setError(null);
    53             userResponseDTO.setName(u.getName());
    54             userResponseDTO.setSurname(u.getSurname());
    55             userResponseDTO.setRetained(u.getRetainedSkills());
    56             userResponseDTO.setToLearn(u.getSkillsToLearn());
    57 
    58             return userResponseDTO;
     47            return this.builderService.buildUserResponseDTO(u);
    5948        }
    6049
    61         return null;
     50        return new UserResponseDTO();
    6251    }
    6352
    64     @PostMapping(path = "/company/{id}/{email}")
     53    @PostMapping(path = "/company/{id}")
    6554    public CompanyResponseDTO editCompany(@PathVariable(name = "id") Long id,
    66                                           @PathVariable(name = "email") String email,
    6755                                          @RequestBody CompanyRegisterDTO body){
    6856
    69         Optional<?> a = this.accountService.findByIdAndEmail(id, email, AccountType.COMPANY);
     57        Optional<?> a = this.accountService.findByIdAndEmail(id, body.getEmail(), AccountType.COMPANY);
    7058
    7159        if(a.isPresent()){
    7260            Company c = this.accountService.editCompany(id, body.getEmail(), body.getName(), body.getCountry(), body.getCity(), body.getStreet());
    73 
    74             CompanyResponseDTO companyResponseDTO = new CompanyResponseDTO();
    75 
    76             companyResponseDTO.setId(c.getId());
    77             companyResponseDTO.setEmail(c.getEmail());
    78             companyResponseDTO.setError(null);
    79             companyResponseDTO.setType(AccountType.COMPANY);
    80             companyResponseDTO.setName(c.getName());
    81             companyResponseDTO.setAddress(c.getAddress());
    82 
    83             return companyResponseDTO;
     61            return this.builderService.buildCompanyResponseDTO(c);
    8462        }
    8563
    86         return null;
     64        return new CompanyResponseDTO();
    8765    }
    8866
    89     @PostMapping(path = "/team/{id}/{email}")
     67    @PostMapping(path = "/team/{id}")
    9068    public TeamResponseDTO editTeam(@PathVariable(name = "id") Long id,
    91                                     @PathVariable(name = "email") String email,
    9269                                    @RequestBody TeamRegisterDTO body){
    9370
    94         Optional<?> a = this.accountService.findByIdAndEmail(id, email, AccountType.TEAM);
     71        Optional<?> a = this.accountService.findByIdAndEmail(id, body.getEmail(), AccountType.TEAM);
    9572
    9673        if(a.isPresent()){
    9774            Team t = this.accountService.editTeam(id, body.getEmail(), body.getName(), body.getMembers());
    98 
    99             TeamResponseDTO teamResponseDTO = new TeamResponseDTO();
    100 
    101             teamResponseDTO.setId(t.getId());
    102             teamResponseDTO.setEmail(t.getEmail());
    103             teamResponseDTO.setError(null);
    104             teamResponseDTO.setType(AccountType.TEAM);
    105             teamResponseDTO.setName(t.getName());
    106             teamResponseDTO.setMembers(t.getMembers());
    107 
    108             return teamResponseDTO;
     75            return this.builderService.buildTeamResponseDTO(t);
    10976        }
    11077
    111         return null;
     78        return new TeamResponseDTO();
    11279    }
    11380}
  • src/main/java/it/finki/tinki/web/controller/AccountRegisterController.java

    rfc8421e r5f9d25a  
    3838        List<Skill> retained = this.skillService.returnSkillsBasedOnId(body.getRetainedSkills());
    3939        List<Skill> toLearn = this.skillService.returnSkillsBasedOnId(body.getSkillsToLearn());
    40 
    4140        Account k = this.accountService.registerUser(body.getEmail(), body.getPassword(), body.getName(), body.getSurname(), retained, toLearn);
    42 
    4341        Map<String, String> response = new HashMap<>();
    4442
     
    5654
    5755        Account k = this.accountService.registerTeam(body.getEmail(), body.getPassword(), body.getName(), body.getMembers());
    58 
    5956        Map<String, String> response = new HashMap<>();
    6057
     
    7370        Account k = this.accountService.registerCompany(body.getEmail(),
    7471                body.getPassword(), body.getName(), body.getCountry(), body.getCity(), body.getStreet());
    75 
    7672        Map<String, String> response = new HashMap<>();
    7773
  • src/main/java/it/finki/tinki/web/controller/LoginController.java

    rfc8421e r5f9d25a  
    1818import it.finki.tinki.model.enumerator.AccountType;
    1919import it.finki.tinki.service.AccountService;
     20import it.finki.tinki.service.BuilderService;
    2021import it.finki.tinki.service.MatchmakerService;
    2122import it.finki.tinki.service.WorkService;
     
    3132
    3233    AccountService accountService;
    33     MatchmakerService matchmakerService;
    3434    WorkService workService;
     35    BuilderService builderService;
    3536
    36     public LoginController(AccountService accountService, MatchmakerService matchmakerService, WorkService workService) {
     37    public LoginController(AccountService accountService, WorkService workService, BuilderService builderService) {
    3738        this.accountService = accountService;
    38         this.matchmakerService = matchmakerService;
    3939        this.workService = workService;
     40        this.builderService = builderService;
    4041    }
    4142
     
    4344    public LoginResponseDTO loginProcess(@RequestBody AccountLoginDTO body) throws ResponseStatusException {
    4445
    45         System.out.println(body);
    4646        Account a1 = accountService.findUser(body.getEmail(), body.getPassword(), body.getType());
    4747
    4848        if(a1!=null){
    4949            if(a1.getClass().equals(User.class)){
    50                 return processUser(a1);
     50                return this.builderService.buildUserResponseDTO(a1);
    5151            }else if(a1.getClass().equals(Team.class)){
    52                 return processTeam(a1);
     52                return this.builderService.buildTeamResponseDTO(a1);
    5353            }else{
    54                 return processCompany(a1);
     54                return this.builderService.buildCompanyResponseDTO(a1);
    5555            }
    5656        }
    5757
    5858        return new LoginResponseDTO();
    59     }
    60 
    61     private UserResponseDTO processUser(Account a1){
    62         UserResponseDTO uDto = new UserResponseDTO();
    63 
    64         uDto.setError(null);
    65 
    66         uDto.setId(a1.getId());
    67         uDto.setEmail(a1.getEmail());
    68         uDto.setName(a1.getName());
    69         uDto.setType(AccountType.USER);
    70         uDto.setSurname(((User) a1).getSurname());
    71 
    72         uDto.setRetained(((User) a1).getRetainedSkills());
    73         uDto.setToLearn(((User) a1).getSkillsToLearn());
    74 
    75         List<Job> matchedJobs = this.matchmakerService.getMatchingJobsForUser((User) a1);
    76         List<Project> matchedProjects = this.matchmakerService.getMatchingProjectsForUser((User) a1);
    77         List<Internship> matchedInternships = this.matchmakerService.getMatchingInternshipsForUser((User) a1);
    78 
    79         matchedJobs.forEach(job -> {
    80             JobResponseDTO dto = new JobResponseDTO(job);
    81             uDto.getJobs().add(dto);
    82         });
    83 
    84         matchedProjects.forEach(project -> {
    85             ProjectResponseDTO dto = new ProjectResponseDTO(project);
    86             uDto.getProjects().add(dto);
    87         });
    88 
    89         matchedInternships.forEach(internship -> {
    90             InternshipResponseDTO dto = new InternshipResponseDTO(internship);
    91             uDto.getInternships().add(dto);
    92         });
    93 
    94 
    95         return uDto;
    96     }
    97 
    98     private TeamResponseDTO processTeam(Account a1){
    99         TeamResponseDTO tDto = new TeamResponseDTO();
    100 
    101         tDto.setError(null);
    102 
    103         tDto.setId(a1.getId());
    104         tDto.setEmail(a1.getEmail());
    105         tDto.setName(a1.getName());
    106         tDto.setType(AccountType.TEAM);
    107         tDto.setMembers(((Team) a1).getMembers());
    108 
    109         List<Job> jobs = this.workService.getAllJobsByAccount(a1.getId());
    110         List<Project> projects = this.workService.getAllProjectsByAccount(a1.getId());
    111 
    112         jobs.forEach(job -> {
    113             JobResponseDTO dto = new JobResponseDTO(job);
    114             tDto.getJobs().add(dto);
    115         });
    116 
    117         projects.forEach(project -> {
    118             ProjectResponseDTO dto = new ProjectResponseDTO(project);
    119             tDto.getProjects().add(dto);
    120         });
    121 
    122         return tDto;
    123     }
    124 
    125     private CompanyResponseDTO processCompany(Account a1){
    126         CompanyResponseDTO cDto = new CompanyResponseDTO();
    127 
    128         cDto.setError(null);
    129 
    130         cDto.setId(a1.getId());
    131         cDto.setEmail(a1.getEmail());
    132         cDto.setName(a1.getName());
    133         cDto.setType(AccountType.COMPANY);
    134         cDto.setAddress(((Company) a1).getAddress());
    135 
    136         List<Job> jobs = this.workService.getAllJobsByAccount(a1.getId());
    137         List<Internship> internships = this.workService.getAllInternshipsByAccount(a1.getId());
    138 
    139         jobs.forEach(job -> {
    140             JobResponseDTO dto = new JobResponseDTO(job);
    141             cDto.getJobs().add(dto);
    142         });
    143 
    144         internships.forEach(internship -> {
    145             InternshipResponseDTO dto = new InternshipResponseDTO(internship);
    146             cDto.getInternships().add(dto);
    147         });
    148 
    149         return cDto;
    15059    }
    15160
     
    16473        return this.workService.fullTextProjectSearch(text);
    16574    }
    166 
    16775}
  • src/main/java/it/finki/tinki/web/controller/WorkEditController.java

    rfc8421e r5f9d25a  
    2525    }
    2626
    27     @PostMapping("/job/{userId}/{id}")
    28     public JobResponseDTO editJob(@PathVariable Long userId,
    29                                   @PathVariable Long id,
     27    @PostMapping("/job/{id}")
     28    public JobResponseDTO editJob(@PathVariable Long id,
    3029                                  @RequestBody JobRegisterDTO body){
    3130
    32         if(userId.equals(this.workService.getJobById(id).getAccount().getId())) {
     31        if(body.getAccountId().equals(this.workService.getJobById(id).getAccount().getId())) {
    3332            Job k = this.workService.editJob(id, body.getTitle(), body.getDescription(), body.getSalary());
    34 
    3533            return new JobResponseDTO(k);
    3634        }
    37 
    3835        return null;
    3936    }
    4037
    41     @PostMapping("/internship/{userId}/{id}")
    42     public InternshipResponseDTO editInternship(@PathVariable Long userId,
    43                                                 @PathVariable Long id,
     38    @PostMapping("/internship/{id}")
     39    public InternshipResponseDTO editInternship(@PathVariable Long id,
    4440                                                @RequestBody InternshipRegisterDTO body){
    4541
    46         if(userId.equals(this.workService.getJobById(id).getAccount().getId())){
     42        if(body.getAccountId().equals(this.workService.getJobById(id).getAccount().getId())){
    4743            Internship k = this.workService.editInternship(id, body.getTitle(), body.getDescription(), body.getSalary(), body.getOpenSpots());
    48 
    4944            return new InternshipResponseDTO(k);
    5045        }
    51 
    5246        return null;
    5347    }
    5448
    55     @PostMapping("/project/{userId}/{id}")
    56     public ProjectResponseDTO editProject(@PathVariable Long userId,
    57                                           @PathVariable Long id,
     49    @PostMapping("/project/{id}")
     50    public ProjectResponseDTO editProject(@PathVariable Long id,
    5851                                          @RequestBody ProjectRegisterDTO body){
    5952
    60         if(userId.equals(this.workService.getJobById(id).getAccount().getId())) {
     53        if(body.getAccountId().equals(this.workService.getJobById(id).getAccount().getId())) {
    6154            Project k = this.workService.editProject(id, body.getTitle(), body.getDescription(), body.getSalary());
    62 
    6355            return new ProjectResponseDTO(k);
    6456        }
    65 
    6657        return null;
    6758    }
  • src/main/java/it/finki/tinki/web/controller/WorkRegisterController.java

    rfc8421e r5f9d25a  
    2626    @PostMapping("/job")
    2727    public JobResponseDTO registerJob(@RequestBody JobRegisterDTO body){
    28 
    2928        Job j = this.workService.insertJob(body.getTitle(),
    3029                body.getDescription(), body.getAccountId(), body.getSalary(), body.getSkillsRequired(), body.getType());
    31 
    3230        return new JobResponseDTO(j);
    3331    }
     
    3533    @PostMapping("/internship")
    3634    public InternshipResponseDTO registerInternship(@RequestBody InternshipRegisterDTO body){
    37 
    3835        Internship j = this.workService.insertInternship(body.getTitle(),
    3936                body.getDescription(), body.getAccountId(), body.getSalary(), body.getSkillsTrained(), body.getOpenSpots(), body.getType());
    40 
    4137        return new InternshipResponseDTO(j);
    4238    }
     
    4440    @PostMapping("/project")
    4541    public ProjectResponseDTO registerProject(@RequestBody ProjectRegisterDTO body){
    46 
    4742        Project j = this.workService.insertProject(body.getTitle(),
    4843                body.getDescription(), body.getAccountId(), body.getSalary(), body.getSkillsRequired(), body.getValidUntil(), body.getType());
    49 
    5044        return new ProjectResponseDTO(j);
    5145    }
Note: See TracChangeset for help on using the changeset viewer.