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

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

bugfixes and refactoring

  • Property mode set to 100644
File size: 3.5 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.BuilderService;
16import it.finki.tinki.service.SkillService;
17import org.springframework.web.bind.annotation.*;
18
19import java.util.List;
20import java.util.Optional;
21
22@RestController
23@CrossOrigin(origins = "http://localhost:3000")
24@RequestMapping(path = "/api/edit/account")
25public class AccountEditController {
26
27 AccountService accountService;
28 SkillService skillService;
29 BuilderService builderService;
30
31 public AccountEditController(AccountService accountService, SkillService skillService, BuilderService builderService) {
32 this.accountService = accountService;
33 this.skillService = skillService;
34 this.builderService = builderService;
35 }
36
37 @PostMapping(path = "/user/{id}/{email}")
38 public UserResponseDTO editUser(@PathVariable(name = "id") Long id,
39 @PathVariable(name = "email") String email,
40 @RequestBody UserRegisterDTO body){
41
42 Optional<?> a = this.accountService.findByIdAndEmail(id, email, AccountType.USER);
43
44 if(a.isPresent()){
45 List<Skill> retained = this.skillService.returnSkillsBasedOnId(body.getRetainedSkills());
46 List<Skill> toLearn = this.skillService.returnSkillsBasedOnId(body.getSkillsToLearn());
47 User u = this.accountService.editUser(id, body.getEmail(), body.getName(), body.getSurname(), retained, toLearn);
48 return this.builderService.buildUserResponseDTO(u);
49 }
50
51 return new UserResponseDTO();
52 }
53
54 @PostMapping(path = "/company/{id}/{email}")
55 public CompanyResponseDTO editCompany(@PathVariable(name = "id") Long id,
56 @PathVariable(name = "email") String email,
57 @RequestBody CompanyRegisterDTO body){
58
59 Optional<?> a = this.accountService.findByIdAndEmail(id, email, AccountType.COMPANY);
60
61 if(a.isPresent()){
62 Company c = this.accountService.editCompany(id, body.getEmail(), body.getName(), body.getCountry(), body.getCity(), body.getStreet());
63 return this.builderService.buildCompanyResponseDTO(c);
64 }
65
66 return new CompanyResponseDTO();
67 }
68
69 @PostMapping(path = "/team/{id}/{email}")
70 public TeamResponseDTO editTeam(@PathVariable(name = "id") Long id,
71 @PathVariable(name = "email") String email,
72 @RequestBody TeamRegisterDTO body){
73
74 Optional<?> a = this.accountService.findByIdAndEmail(id, email, AccountType.TEAM);
75
76 if(a.isPresent()){
77 Team t = this.accountService.editTeam(id, body.getEmail(), body.getName(), body.getMembers());
78 return this.builderService.buildTeamResponseDTO(t);
79 }
80
81 return new TeamResponseDTO();
82 }
83}
Note: See TracBrowser for help on using the repository browser.