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/account")
|
---|
22 | public class AccountEditController {
|
---|
23 |
|
---|
24 | AccountService accountService;
|
---|
25 | SkillService skillService;
|
---|
26 |
|
---|
27 | public AccountEditController(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 = this.accountService.editUser(id, body.getEmail(), body.getName(), body.getSurname(), retained, toLearn);
|
---|
44 |
|
---|
45 | UserResponseDTO userResponseDTO = new UserResponseDTO();
|
---|
46 |
|
---|
47 | userResponseDTO.setId(u.getId());
|
---|
48 | userResponseDTO.setEmail(u.getEmail());
|
---|
49 | userResponseDTO.setType(AccountType.USER);
|
---|
50 | userResponseDTO.setName(u.getName());
|
---|
51 | userResponseDTO.setSurname(u.getSurname());
|
---|
52 | userResponseDTO.setRetained(u.getRetainedSkills());
|
---|
53 | userResponseDTO.setToLearn(u.getSkillsToLearn());
|
---|
54 |
|
---|
55 | return userResponseDTO;
|
---|
56 | }
|
---|
57 |
|
---|
58 | return null;
|
---|
59 | }
|
---|
60 |
|
---|
61 | @RequestMapping(path = "/company/{id}/{email}")
|
---|
62 | public CompanyResponseDTO editCompany(@PathVariable(name = "id") Long id,
|
---|
63 | @PathVariable(name = "email") String email,
|
---|
64 | @RequestBody CompanyRegisterDTO body){
|
---|
65 |
|
---|
66 | Optional<?> a = this.accountService.findByIdAndEmail(id, email, AccountType.COMPANY);
|
---|
67 |
|
---|
68 | if(a.isPresent()){
|
---|
69 | Company c = this.accountService.editCompany(id, body.getEmail(), body.getName(), body.getCountry(), body.getCity(), body.getStreet());
|
---|
70 |
|
---|
71 | CompanyResponseDTO companyResponseDTO = new CompanyResponseDTO();
|
---|
72 |
|
---|
73 | companyResponseDTO.setId(c.getId());
|
---|
74 | companyResponseDTO.setEmail(c.getEmail());
|
---|
75 | companyResponseDTO.setType(AccountType.COMPANY);
|
---|
76 | companyResponseDTO.setName(c.getName());
|
---|
77 | companyResponseDTO.setAddress(c.getAddress());
|
---|
78 |
|
---|
79 | return companyResponseDTO;
|
---|
80 | }
|
---|
81 |
|
---|
82 | return null;
|
---|
83 | }
|
---|
84 |
|
---|
85 | @RequestMapping(path = "/team/{id}/{email}")
|
---|
86 | public TeamResponseDTO editTeam(@PathVariable(name = "id") Long id,
|
---|
87 | @PathVariable(name = "email") String email,
|
---|
88 | @RequestBody TeamRegisterDTO body){
|
---|
89 |
|
---|
90 | Optional<?> a = this.accountService.findByIdAndEmail(id, email, AccountType.TEAM);
|
---|
91 |
|
---|
92 | if(a.isPresent()){
|
---|
93 | Team t = this.accountService.editTeam(id, body.getEmail(), body.getName(), body.getMembers());
|
---|
94 |
|
---|
95 | TeamResponseDTO teamResponseDTO = new TeamResponseDTO();
|
---|
96 |
|
---|
97 | teamResponseDTO.setId(t.getId());
|
---|
98 | teamResponseDTO.setEmail(t.getEmail());
|
---|
99 | teamResponseDTO.setType(AccountType.TEAM);
|
---|
100 | teamResponseDTO.setName(t.getName());
|
---|
101 | teamResponseDTO.setMembers(t.getMembers());
|
---|
102 |
|
---|
103 | return teamResponseDTO;
|
---|
104 | }
|
---|
105 |
|
---|
106 | return null;
|
---|
107 | }
|
---|
108 | }
|
---|