Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.ite.onepager.mindmap.controller;
- import de.ite.onepager.mindmap.model.User;
- import de.ite.onepager.mindmap.model.UserSkill;
- import de.ite.onepager.mindmap.model.requests.UserSkillRequest;
- import de.ite.onepager.mindmap.service.SkillService;
- import de.ite.onepager.mindmap.service.UserService;
- import de.ite.onepager.mindmap.service.UserSkillService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.ResponseEntity;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.server.ResponseStatusException;
- import javax.validation.constraints.NotNull;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.NoSuchElementException;
- import java.util.Optional;
- @RestController
- @RequestMapping("/api/v1/userskills")
- @CrossOrigin(origins = "*")
- public class UserSkillController {
- @Autowired
- UserSkillService userSkillService;
- @Autowired
- SkillService skillService;
- @Autowired
- UserService userService;
- @GetMapping("") // used in getUsersKnowledge(user_id, skill_id) in ConsumeWebService.js
- public ResponseEntity<UserSkillRequest> getUserSkillByUserIdAndSkillId(@RequestParam("user_id") String userIdParam, @RequestParam("skill_id") String skillIdParam) {
- Long userId;
- Long skillId;
- // check if user_id and skill_id are numbers
- try {
- userId = Long.parseLong(userIdParam);
- skillId = Long.parseLong(skillIdParam);
- } catch (NumberFormatException e) {
- throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
- }
- // check if user exists in DB
- try {
- userService.getUserById(userId).get();
- } catch (NoSuchElementException e) {
- throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
- }
- // check if skill exists in DB
- try {
- skillService.getById(skillId).get();
- } catch (NoSuchElementException e) {
- throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
- }
- Optional<UserSkillRequest> result = userSkillService.getUserSkillByUserIdAndSkillId(userId, skillId);
- if (!result.isPresent()) {
- throw new ResponseStatusException(HttpStatus.NOT_FOUND);
- }
- return new ResponseEntity<>(result.get(), HttpStatus.OK);
- }
- @GetMapping("/user") // used in getUser() in ConsumeWebService.js
- public ResponseEntity<List<UserSkillRequest>> getUserByUserId(@RequestParam("user_id") String userIdParam) {
- Long userId;
- try {
- userId = Long.parseLong(userIdParam);
- } catch (NumberFormatException e) {
- throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
- }
- List<UserSkillRequest> result = userSkillService.getUserSkills(userId);
- if (result.isEmpty()) {
- throw new ResponseStatusException(HttpStatus.NOT_FOUND);
- }
- return new ResponseEntity<>(result, HttpStatus.OK);
- }
- @GetMapping("/email/user")
- public ResponseEntity<List<UserSkillRequest>> getUserSkillsByEmail(@NotNull @RequestParam("email") String email) {
- List<UserSkillRequest> result = new ArrayList<>();
- try {
- result = userSkillService.getUserSkillsByEmail(email);
- } catch (IllegalArgumentException e) {
- throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
- } catch (NoSuchElementException e) {
- throw new ResponseStatusException(HttpStatus.NOT_FOUND);
- }
- if (result.isEmpty()) {
- throw new ResponseStatusException(HttpStatus.NOT_FOUND);
- }
- return new ResponseEntity<>(result, HttpStatus.OK);
- }
- // if user skill we attempt to add already exists (by unique key user_id + skill_id), we just update it"
- @PostMapping("/add") //used in addUserKnowledge() in ConsumeWebService.js
- public ResponseEntity<UserSkill> addUserSkill(@NotNull @RequestBody UserSkillRequest request) {
- UserSkill result;
- try {
- result = userSkillService.addUserSkill(request);
- } catch (IllegalArgumentException e) {
- throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
- } catch (NoSuchElementException e) {
- throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
- }
- return new ResponseEntity<>(result, HttpStatus.OK);
- }
- //throws exception if UserSkill does not exist!
- @PostMapping("/update") //used in updateUserKnowledge() in ConsumeWebService.js
- public ResponseEntity<UserSkill> updateUserSkill(@NotNull @RequestBody UserSkillRequest updateRequest) {
- UserSkill result;
- try {
- result = userSkillService.updateUserSkill(updateRequest);
- } catch (IllegalArgumentException e) {
- throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
- } catch (NoSuchElementException e) {
- throw new ResponseStatusException(HttpStatus.NOT_FOUND);
- }
- return new ResponseEntity<>(result, HttpStatus.OK);
- }
- @GetMapping("/find") // found in getUserIdByEmail () in ConsumeWebService.js
- public ResponseEntity<User> getUserIdByEmail(@NotNull @RequestParam("email") String email) {
- Optional<User> user = userService.getUserByEmail(email);
- if (!user.isPresent()) {
- throw new ResponseStatusException(HttpStatus.NOT_FOUND);
- }
- return new ResponseEntity<>(user.get(), HttpStatus.OK);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement