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

Last change on this file since fc8421e was 17abe5e, checked in by i-ina <76742075+i-ina@…>, 3 years ago

react components for details and bugfix

  • Property mode set to 100644
File size: 4.4 KB
Line 
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;
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;
13import it.finki.tinki.model.enumerator.AccountType;
14import it.finki.tinki.service.AccountService;
15import it.finki.tinki.service.SkillService;
16import org.springframework.web.bind.annotation.*;
17
18import java.util.List;
19import java.util.Optional;
20
21@RestController
22@CrossOrigin(origins = "http://localhost:3000")
23@RequestMapping(path = "/api/edit/account")
24public class AccountEditController {
25
26 AccountService accountService;
27 SkillService skillService;
28
29 public AccountEditController(AccountService accountService, SkillService skillService) {
30 this.accountService = accountService;
31 this.skillService = skillService;
32 }
33
34 @PostMapping(path = "/user/{id}/{email}")
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
45 User u = this.accountService.editUser(id, body.getEmail(), body.getName(), body.getSurname(), retained, toLearn);
46
47 UserResponseDTO userResponseDTO = new UserResponseDTO();
48
49 userResponseDTO.setId(u.getId());
50 userResponseDTO.setEmail(u.getEmail());
51 userResponseDTO.setType(AccountType.USER);
52 userResponseDTO.setError(null);
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
64 @PostMapping(path = "/company/{id}/{email}")
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
76 companyResponseDTO.setId(c.getId());
77 companyResponseDTO.setEmail(c.getEmail());
78 companyResponseDTO.setError(null);
79 companyResponseDTO.setType(AccountType.COMPANY);
80 companyResponseDTO.setName(c.getName());
81 companyResponseDTO.setAddress(c.getAddress());
82
83 return companyResponseDTO;
84 }
85
86 return null;
87 }
88
89 @PostMapping(path = "/team/{id}/{email}")
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
101 teamResponseDTO.setId(t.getId());
102 teamResponseDTO.setEmail(t.getEmail());
103 teamResponseDTO.setError(null);
104 teamResponseDTO.setType(AccountType.TEAM);
105 teamResponseDTO.setName(t.getName());
106 teamResponseDTO.setMembers(t.getMembers());
107
108 return teamResponseDTO;
109 }
110
111 return null;
112 }
113}
Note: See TracBrowser for help on using the repository browser.