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

refactoring controllers

File:
1 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}
Note: See TracChangeset for help on using the changeset viewer.