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