Advertisement
Guest User

UserSkillController

a guest
Jan 23rd, 2020
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.68 KB | None | 0 0
  1. package de.ite.onepager.mindmap.controller;
  2.  
  3. import de.ite.onepager.mindmap.model.User;
  4. import de.ite.onepager.mindmap.model.UserSkill;
  5. import de.ite.onepager.mindmap.model.requests.UserSkillRequest;
  6. import de.ite.onepager.mindmap.service.SkillService;
  7. import de.ite.onepager.mindmap.service.UserService;
  8. import de.ite.onepager.mindmap.service.UserSkillService;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.http.HttpStatus;
  11. import org.springframework.http.ResponseEntity;
  12. import org.springframework.web.bind.annotation.*;
  13. import org.springframework.web.server.ResponseStatusException;
  14.  
  15. import javax.validation.constraints.NotNull;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import java.util.NoSuchElementException;
  19. import java.util.Optional;
  20.  
  21. @RestController
  22. @RequestMapping("/api/v1/userskills")
  23. @CrossOrigin(origins = "*")
  24. public class UserSkillController {
  25.  
  26.     @Autowired
  27.     UserSkillService userSkillService;
  28.  
  29.     @Autowired
  30.     SkillService skillService;
  31.  
  32.     @Autowired
  33.     UserService userService;
  34.  
  35.     @GetMapping("") // used in  getUsersKnowledge(user_id, skill_id)  in  ConsumeWebService.js
  36.     public ResponseEntity<UserSkillRequest> getUserSkillByUserIdAndSkillId(@RequestParam("user_id") String userIdParam, @RequestParam("skill_id") String skillIdParam) {
  37.  
  38.         Long userId;
  39.         Long skillId;
  40.  
  41.         // check if user_id and skill_id are numbers
  42.         try {
  43.             userId = Long.parseLong(userIdParam);
  44.             skillId = Long.parseLong(skillIdParam);
  45.         } catch (NumberFormatException e) {
  46.             throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
  47.         }
  48.  
  49.         // check if user exists in DB
  50.         try {
  51.             userService.getUserById(userId).get();
  52.         } catch (NoSuchElementException e) {
  53.             throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
  54.         }
  55.  
  56.         // check if skill exists in DB
  57.         try {
  58.             skillService.getById(skillId).get();
  59.         } catch (NoSuchElementException e) {
  60.             throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
  61.         }
  62.  
  63.         Optional<UserSkillRequest> result = userSkillService.getUserSkillByUserIdAndSkillId(userId, skillId);
  64.  
  65.         if (!result.isPresent()) {
  66.             throw new ResponseStatusException(HttpStatus.NOT_FOUND);
  67.         }
  68.  
  69.         return new ResponseEntity<>(result.get(), HttpStatus.OK);
  70.     }
  71.  
  72.     @GetMapping("/user") // used in getUser()  in ConsumeWebService.js
  73.     public ResponseEntity<List<UserSkillRequest>> getUserByUserId(@RequestParam("user_id") String userIdParam) {
  74.  
  75.         Long userId;
  76.  
  77.         try {
  78.             userId = Long.parseLong(userIdParam);
  79.         } catch (NumberFormatException e) {
  80.             throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
  81.         }
  82.  
  83.         List<UserSkillRequest> result = userSkillService.getUserSkills(userId);
  84.  
  85.         if (result.isEmpty()) {
  86.             throw new ResponseStatusException(HttpStatus.NOT_FOUND);
  87.         }
  88.  
  89.         return new ResponseEntity<>(result, HttpStatus.OK);
  90.     }
  91.  
  92.     @GetMapping("/email/user")
  93.     public ResponseEntity<List<UserSkillRequest>> getUserSkillsByEmail(@NotNull @RequestParam("email") String email) {
  94.  
  95.         List<UserSkillRequest> result = new ArrayList<>();
  96.  
  97.         try {
  98.             result = userSkillService.getUserSkillsByEmail(email);
  99.         } catch (IllegalArgumentException e) {
  100.             throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
  101.         } catch (NoSuchElementException e) {
  102.             throw new ResponseStatusException(HttpStatus.NOT_FOUND);
  103.         }
  104.  
  105.         if (result.isEmpty()) {
  106.             throw new ResponseStatusException(HttpStatus.NOT_FOUND);
  107.         }
  108.  
  109.         return new ResponseEntity<>(result, HttpStatus.OK);
  110.     }
  111.  
  112.     // if user skill we attempt to add already exists (by unique key user_id + skill_id), we just update it"
  113.     @PostMapping("/add") //used in addUserKnowledge() in ConsumeWebService.js
  114.     public ResponseEntity<UserSkill> addUserSkill(@NotNull @RequestBody UserSkillRequest request) {
  115.  
  116.         UserSkill result;
  117.  
  118.         try {
  119.             result = userSkillService.addUserSkill(request);
  120.         } catch (IllegalArgumentException e) {
  121.             throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
  122.         } catch (NoSuchElementException e) {
  123.             throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
  124.         }
  125.  
  126.         return new ResponseEntity<>(result, HttpStatus.OK);
  127.     }
  128.  
  129.     //throws exception if UserSkill does not exist!
  130.     @PostMapping("/update")   //used in updateUserKnowledge() in ConsumeWebService.js
  131.     public ResponseEntity<UserSkill> updateUserSkill(@NotNull @RequestBody UserSkillRequest updateRequest) {
  132.  
  133.         UserSkill result;
  134.  
  135.         try {
  136.             result = userSkillService.updateUserSkill(updateRequest);
  137.         } catch (IllegalArgumentException e) {
  138.             throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
  139.         } catch (NoSuchElementException e) {
  140.             throw new ResponseStatusException(HttpStatus.NOT_FOUND);
  141.         }
  142.  
  143.         return new ResponseEntity<>(result, HttpStatus.OK);
  144.     }
  145.  
  146.     @GetMapping("/find") // found in getUserIdByEmail () in ConsumeWebService.js
  147.     public ResponseEntity<User> getUserIdByEmail(@NotNull @RequestParam("email") String email) {
  148.  
  149.         Optional<User> user = userService.getUserByEmail(email);
  150.  
  151.         if (!user.isPresent()) {
  152.             throw new ResponseStatusException(HttpStatus.NOT_FOUND);
  153.         }
  154.  
  155.         return new ResponseEntity<>(user.get(), HttpStatus.OK);
  156.     }
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement