source: src/main/java/it/finki/tinki/web/controller/AccountEditController.java@ 8f1f460

Last change on this file since 8f1f460 was 5f9d25a, checked in by Vzdra <vladko.zdravkovski@…>, 3 years ago

refactoring controllers

  • Property mode set to 100644
File size: 3.2 KB
RevLine 
[14b648e]1package it.finki.tinki.web.controller;
2
3import it.finki.tinki.model.Skill;
4import it.finki.tinki.model.Users.Company;
5import it.finki.tinki.model.Users.Team;
6import it.finki.tinki.model.Users.User;
[bd46dbb]7import it.finki.tinki.model.dto.register.account.CompanyRegisterDTO;
8import it.finki.tinki.model.dto.register.account.TeamRegisterDTO;
9import it.finki.tinki.model.dto.register.account.UserRegisterDTO;
10import it.finki.tinki.model.dto.response.account.CompanyResponseDTO;
11import it.finki.tinki.model.dto.response.account.TeamResponseDTO;
12import it.finki.tinki.model.dto.response.account.UserResponseDTO;
[14b648e]13import it.finki.tinki.model.enumerator.AccountType;
14import it.finki.tinki.service.AccountService;
[5f9d25a]15import it.finki.tinki.service.BuilderService;
[14b648e]16import it.finki.tinki.service.SkillService;
[bd46dbb]17import org.springframework.web.bind.annotation.*;
[14b648e]18
19import java.util.List;
20import java.util.Optional;
21
22@RestController
[17abe5e]23@CrossOrigin(origins = "http://localhost:3000")
[bd38a55]24@RequestMapping(path = "/api/edit/account")
25public class AccountEditController {
[14b648e]26
27 AccountService accountService;
28 SkillService skillService;
[5f9d25a]29 BuilderService builderService;
[14b648e]30
[5f9d25a]31 public AccountEditController(AccountService accountService, SkillService skillService, BuilderService builderService) {
[14b648e]32 this.accountService = accountService;
33 this.skillService = skillService;
[5f9d25a]34 this.builderService = builderService;
[14b648e]35 }
36
[5f9d25a]37 @PostMapping(path = "/user/{id}")
[14b648e]38 public UserResponseDTO editUser(@PathVariable(name = "id") Long id,
39 @RequestBody UserRegisterDTO body){
40
[5f9d25a]41 Optional<?> a = this.accountService.findByIdAndEmail(id, body.getEmail(), AccountType.USER);
[14b648e]42
43 if(a.isPresent()){
44 List<Skill> retained = this.skillService.returnSkillsBasedOnId(body.getRetainedSkills());
45 List<Skill> toLearn = this.skillService.returnSkillsBasedOnId(body.getSkillsToLearn());
[bd38a55]46 User u = this.accountService.editUser(id, body.getEmail(), body.getName(), body.getSurname(), retained, toLearn);
[5f9d25a]47 return this.builderService.buildUserResponseDTO(u);
[14b648e]48 }
49
[5f9d25a]50 return new UserResponseDTO();
[14b648e]51 }
52
[5f9d25a]53 @PostMapping(path = "/company/{id}")
[14b648e]54 public CompanyResponseDTO editCompany(@PathVariable(name = "id") Long id,
55 @RequestBody CompanyRegisterDTO body){
56
[5f9d25a]57 Optional<?> a = this.accountService.findByIdAndEmail(id, body.getEmail(), AccountType.COMPANY);
[14b648e]58
59 if(a.isPresent()){
60 Company c = this.accountService.editCompany(id, body.getEmail(), body.getName(), body.getCountry(), body.getCity(), body.getStreet());
[5f9d25a]61 return this.builderService.buildCompanyResponseDTO(c);
[14b648e]62 }
63
[5f9d25a]64 return new CompanyResponseDTO();
[14b648e]65 }
66
[5f9d25a]67 @PostMapping(path = "/team/{id}")
[14b648e]68 public TeamResponseDTO editTeam(@PathVariable(name = "id") Long id,
69 @RequestBody TeamRegisterDTO body){
70
[5f9d25a]71 Optional<?> a = this.accountService.findByIdAndEmail(id, body.getEmail(), AccountType.TEAM);
[14b648e]72
73 if(a.isPresent()){
74 Team t = this.accountService.editTeam(id, body.getEmail(), body.getName(), body.getMembers());
[5f9d25a]75 return this.builderService.buildTeamResponseDTO(t);
[14b648e]76 }
77
[5f9d25a]78 return new TeamResponseDTO();
[14b648e]79 }
80}
Note: See TracBrowser for help on using the repository browser.