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

Last change on this file since bd46dbb was bd46dbb, checked in by Vzdra <vladko.zdravkovski@…>, 3 years ago

refactoring and response dto

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