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
RevLine 
[14b648e]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;
[bd46dbb]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;
[14b648e]13import it.finki.tinki.model.enumerator.AccountType;
14import it.finki.tinki.service.AccountService;
15import it.finki.tinki.service.SkillService;
[bd46dbb]16import org.springframework.web.bind.annotation.*;
[14b648e]17
18import java.util.List;
19import java.util.Optional;
20
21@RestController
[17abe5e]22@CrossOrigin(origins = "http://localhost:3000")
[bd38a55]23@RequestMapping(path = "/api/edit/account")
24public 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}
Note: See TracBrowser for help on using the repository browser.