[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
|
---|
[17abe5e] | 22 | @CrossOrigin(origins = "http://localhost:3000")
|
---|
[bd38a55] | 23 | @RequestMapping(path = "/api/edit/account")
|
---|
| 24 | public class AccountEditController {
|
---|
[14b648e] | 25 |
|
---|
| 26 | AccountService accountService;
|
---|
| 27 | SkillService skillService;
|
---|
| 28 |
|
---|
[bd38a55] | 29 | public AccountEditController(AccountService accountService, SkillService skillService) {
|
---|
[14b648e] | 30 | this.accountService = accountService;
|
---|
| 31 | this.skillService = skillService;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
[bd46dbb] | 34 | @PostMapping(path = "/user/{id}/{email}")
|
---|
[14b648e] | 35 | public UserResponseDTO editUser(@PathVariable(name = "id") Long id,
|
---|
| 36 | @PathVariable(name = "email") String email,
|
---|
| 37 | @RequestBody UserRegisterDTO body){
|
---|
| 38 |
|
---|
| 39 | Optional<?> a = this.accountService.findByIdAndEmail(id, email, AccountType.USER);
|
---|
| 40 |
|
---|
| 41 | if(a.isPresent()){
|
---|
| 42 | List<Skill> retained = this.skillService.returnSkillsBasedOnId(body.getRetainedSkills());
|
---|
| 43 | List<Skill> toLearn = this.skillService.returnSkillsBasedOnId(body.getSkillsToLearn());
|
---|
| 44 |
|
---|
[bd38a55] | 45 | User u = this.accountService.editUser(id, body.getEmail(), body.getName(), body.getSurname(), retained, toLearn);
|
---|
[14b648e] | 46 |
|
---|
| 47 | UserResponseDTO userResponseDTO = new UserResponseDTO();
|
---|
| 48 |
|
---|
[bd38a55] | 49 | userResponseDTO.setId(u.getId());
|
---|
[14b648e] | 50 | userResponseDTO.setEmail(u.getEmail());
|
---|
[bd38a55] | 51 | userResponseDTO.setType(AccountType.USER);
|
---|
[bd46dbb] | 52 | userResponseDTO.setError(null);
|
---|
[14b648e] | 53 | userResponseDTO.setName(u.getName());
|
---|
| 54 | userResponseDTO.setSurname(u.getSurname());
|
---|
| 55 | userResponseDTO.setRetained(u.getRetainedSkills());
|
---|
| 56 | userResponseDTO.setToLearn(u.getSkillsToLearn());
|
---|
| 57 |
|
---|
| 58 | return userResponseDTO;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | return null;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[bd46dbb] | 64 | @PostMapping(path = "/company/{id}/{email}")
|
---|
[14b648e] | 65 | public CompanyResponseDTO editCompany(@PathVariable(name = "id") Long id,
|
---|
| 66 | @PathVariable(name = "email") String email,
|
---|
| 67 | @RequestBody CompanyRegisterDTO body){
|
---|
| 68 |
|
---|
| 69 | Optional<?> a = this.accountService.findByIdAndEmail(id, email, AccountType.COMPANY);
|
---|
| 70 |
|
---|
| 71 | if(a.isPresent()){
|
---|
| 72 | Company c = this.accountService.editCompany(id, body.getEmail(), body.getName(), body.getCountry(), body.getCity(), body.getStreet());
|
---|
| 73 |
|
---|
| 74 | CompanyResponseDTO companyResponseDTO = new CompanyResponseDTO();
|
---|
| 75 |
|
---|
[bd38a55] | 76 | companyResponseDTO.setId(c.getId());
|
---|
[14b648e] | 77 | companyResponseDTO.setEmail(c.getEmail());
|
---|
[bd46dbb] | 78 | companyResponseDTO.setError(null);
|
---|
[bd38a55] | 79 | companyResponseDTO.setType(AccountType.COMPANY);
|
---|
[14b648e] | 80 | companyResponseDTO.setName(c.getName());
|
---|
| 81 | companyResponseDTO.setAddress(c.getAddress());
|
---|
| 82 |
|
---|
| 83 | return companyResponseDTO;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | return null;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[bd46dbb] | 89 | @PostMapping(path = "/team/{id}/{email}")
|
---|
[14b648e] | 90 | public TeamResponseDTO editTeam(@PathVariable(name = "id") Long id,
|
---|
| 91 | @PathVariable(name = "email") String email,
|
---|
| 92 | @RequestBody TeamRegisterDTO body){
|
---|
| 93 |
|
---|
| 94 | Optional<?> a = this.accountService.findByIdAndEmail(id, email, AccountType.TEAM);
|
---|
| 95 |
|
---|
| 96 | if(a.isPresent()){
|
---|
| 97 | Team t = this.accountService.editTeam(id, body.getEmail(), body.getName(), body.getMembers());
|
---|
| 98 |
|
---|
| 99 | TeamResponseDTO teamResponseDTO = new TeamResponseDTO();
|
---|
| 100 |
|
---|
[bd38a55] | 101 | teamResponseDTO.setId(t.getId());
|
---|
[14b648e] | 102 | teamResponseDTO.setEmail(t.getEmail());
|
---|
[bd46dbb] | 103 | teamResponseDTO.setError(null);
|
---|
[bd38a55] | 104 | teamResponseDTO.setType(AccountType.TEAM);
|
---|
[14b648e] | 105 | teamResponseDTO.setName(t.getName());
|
---|
| 106 | teamResponseDTO.setMembers(t.getMembers());
|
---|
| 107 |
|
---|
| 108 | return teamResponseDTO;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | return null;
|
---|
| 112 | }
|
---|
| 113 | }
|
---|