[14b648e] | 1 | package it.finki.tinki.web.controller;
|
---|
| 2 |
|
---|
| 3 | import it.finki.tinki.model.Skill;
|
---|
| 4 | import it.finki.tinki.model.Users.Company;
|
---|
| 5 | import it.finki.tinki.model.Users.Team;
|
---|
| 6 | import it.finki.tinki.model.Users.User;
|
---|
[bd46dbb] | 7 | import it.finki.tinki.model.dto.register.account.CompanyRegisterDTO;
|
---|
| 8 | import it.finki.tinki.model.dto.register.account.TeamRegisterDTO;
|
---|
| 9 | import it.finki.tinki.model.dto.register.account.UserRegisterDTO;
|
---|
| 10 | import it.finki.tinki.model.dto.response.account.CompanyResponseDTO;
|
---|
| 11 | import it.finki.tinki.model.dto.response.account.TeamResponseDTO;
|
---|
| 12 | import it.finki.tinki.model.dto.response.account.UserResponseDTO;
|
---|
[14b648e] | 13 | import it.finki.tinki.model.enumerator.AccountType;
|
---|
| 14 | import it.finki.tinki.service.AccountService;
|
---|
| 15 | import it.finki.tinki.service.SkillService;
|
---|
[bd46dbb] | 16 | import org.springframework.web.bind.annotation.*;
|
---|
[14b648e] | 17 |
|
---|
| 18 | import java.util.List;
|
---|
| 19 | import java.util.Optional;
|
---|
| 20 |
|
---|
| 21 | @RestController
|
---|
[bd38a55] | 22 | @RequestMapping(path = "/api/edit/account")
|
---|
| 23 | public class AccountEditController {
|
---|
[14b648e] | 24 |
|
---|
| 25 | AccountService accountService;
|
---|
| 26 | SkillService skillService;
|
---|
| 27 |
|
---|
[bd38a55] | 28 | public AccountEditController(AccountService accountService, SkillService skillService) {
|
---|
[14b648e] | 29 | this.accountService = accountService;
|
---|
| 30 | this.skillService = skillService;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
[bd46dbb] | 33 | @PostMapping(path = "/user/{id}/{email}")
|
---|
[14b648e] | 34 | public UserResponseDTO editUser(@PathVariable(name = "id") Long id,
|
---|
| 35 | @PathVariable(name = "email") String email,
|
---|
| 36 | @RequestBody UserRegisterDTO body){
|
---|
| 37 |
|
---|
| 38 | Optional<?> a = this.accountService.findByIdAndEmail(id, email, AccountType.USER);
|
---|
| 39 |
|
---|
| 40 | if(a.isPresent()){
|
---|
| 41 | List<Skill> retained = this.skillService.returnSkillsBasedOnId(body.getRetainedSkills());
|
---|
| 42 | List<Skill> toLearn = this.skillService.returnSkillsBasedOnId(body.getSkillsToLearn());
|
---|
| 43 |
|
---|
[bd38a55] | 44 | User u = this.accountService.editUser(id, body.getEmail(), body.getName(), body.getSurname(), retained, toLearn);
|
---|
[14b648e] | 45 |
|
---|
| 46 | UserResponseDTO userResponseDTO = new UserResponseDTO();
|
---|
| 47 |
|
---|
[bd38a55] | 48 | userResponseDTO.setId(u.getId());
|
---|
[14b648e] | 49 | userResponseDTO.setEmail(u.getEmail());
|
---|
[bd38a55] | 50 | userResponseDTO.setType(AccountType.USER);
|
---|
[bd46dbb] | 51 | userResponseDTO.setError(null);
|
---|
[14b648e] | 52 | userResponseDTO.setName(u.getName());
|
---|
| 53 | userResponseDTO.setSurname(u.getSurname());
|
---|
| 54 | userResponseDTO.setRetained(u.getRetainedSkills());
|
---|
| 55 | userResponseDTO.setToLearn(u.getSkillsToLearn());
|
---|
| 56 |
|
---|
| 57 | return userResponseDTO;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | return null;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[bd46dbb] | 63 | @PostMapping(path = "/company/{id}/{email}")
|
---|
[14b648e] | 64 | public CompanyResponseDTO editCompany(@PathVariable(name = "id") Long id,
|
---|
| 65 | @PathVariable(name = "email") String email,
|
---|
| 66 | @RequestBody CompanyRegisterDTO body){
|
---|
| 67 |
|
---|
| 68 | Optional<?> a = this.accountService.findByIdAndEmail(id, email, AccountType.COMPANY);
|
---|
| 69 |
|
---|
| 70 | if(a.isPresent()){
|
---|
| 71 | Company c = this.accountService.editCompany(id, body.getEmail(), body.getName(), body.getCountry(), body.getCity(), body.getStreet());
|
---|
| 72 |
|
---|
| 73 | CompanyResponseDTO companyResponseDTO = new CompanyResponseDTO();
|
---|
| 74 |
|
---|
[bd38a55] | 75 | companyResponseDTO.setId(c.getId());
|
---|
[14b648e] | 76 | companyResponseDTO.setEmail(c.getEmail());
|
---|
[bd46dbb] | 77 | companyResponseDTO.setError(null);
|
---|
[bd38a55] | 78 | companyResponseDTO.setType(AccountType.COMPANY);
|
---|
[14b648e] | 79 | companyResponseDTO.setName(c.getName());
|
---|
| 80 | companyResponseDTO.setAddress(c.getAddress());
|
---|
| 81 |
|
---|
| 82 | return companyResponseDTO;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | return null;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[bd46dbb] | 88 | @PostMapping(path = "/team/{id}/{email}")
|
---|
[14b648e] | 89 | public TeamResponseDTO editTeam(@PathVariable(name = "id") Long id,
|
---|
| 90 | @PathVariable(name = "email") String email,
|
---|
| 91 | @RequestBody TeamRegisterDTO body){
|
---|
| 92 |
|
---|
| 93 | Optional<?> a = this.accountService.findByIdAndEmail(id, email, AccountType.TEAM);
|
---|
| 94 |
|
---|
| 95 | if(a.isPresent()){
|
---|
| 96 | Team t = this.accountService.editTeam(id, body.getEmail(), body.getName(), body.getMembers());
|
---|
| 97 |
|
---|
| 98 | TeamResponseDTO teamResponseDTO = new TeamResponseDTO();
|
---|
| 99 |
|
---|
[bd38a55] | 100 | teamResponseDTO.setId(t.getId());
|
---|
[14b648e] | 101 | teamResponseDTO.setEmail(t.getEmail());
|
---|
[bd46dbb] | 102 | teamResponseDTO.setError(null);
|
---|
[bd38a55] | 103 | teamResponseDTO.setType(AccountType.TEAM);
|
---|
[14b648e] | 104 | teamResponseDTO.setName(t.getName());
|
---|
| 105 | teamResponseDTO.setMembers(t.getMembers());
|
---|
| 106 |
|
---|
| 107 | return teamResponseDTO;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | return null;
|
---|
| 111 | }
|
---|
| 112 | }
|
---|