source: src/main/java/it/finki/tinki/web/controller/EditController.java@ 14b648e

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

refactor and edit team/company/user

  • Property mode set to 100644
File size: 3.8 KB
Line 
1package it.finki.tinki.web.controller;
2
3import it.finki.tinki.model.Skill;
4import it.finki.tinki.model.Users.Account;
5import it.finki.tinki.model.Users.Company;
6import it.finki.tinki.model.Users.Team;
7import it.finki.tinki.model.Users.User;
8import it.finki.tinki.model.dto.*;
9import it.finki.tinki.model.enumerator.AccountType;
10import it.finki.tinki.service.AccountService;
11import it.finki.tinki.service.SkillService;
12import org.springframework.web.bind.annotation.PathVariable;
13import org.springframework.web.bind.annotation.RequestBody;
14import org.springframework.web.bind.annotation.RequestMapping;
15import org.springframework.web.bind.annotation.RestController;
16
17import java.util.List;
18import java.util.Optional;
19
20@RestController
21@RequestMapping(path = "/api/edit")
22public 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}
Note: See TracBrowser for help on using the repository browser.