chavdardim1990

PhoneToAdmin

Feb 24th, 2022
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 KB | None | 0 0
  1. ---------------------------------------------------------------------------------------------------------------------------
  2. ------------------------------------------------ USERCONTROLLER -----------------------------------------------------------
  3. ---------------------------------------------------------------------------------------------------------------------------
  4.  
  5.  
  6.  
  7. @PutMapping("/{userId}")
  8. public UserOutDTO update(@RequestHeader HttpHeaders headers,@PathVariable int userId,
  9. @Valid @RequestBody UserDTO userDTO) {
  10. try {
  11. User loggedInUser = authenticationHelper.tryGetUser(headers);
  12. User userToBeUpdated = mapper.toDTOUpdate(userDTO, userId);
  13. Optional<String> phone = Optional.ofNullable(userDTO.getPhone());
  14. int userRole = userDTO.getUserType();
  15. service.update(loggedInUser, userId, userToBeUpdated, phone, userRole);
  16.  
  17. return mapper.fromDTO(userToBeUpdated);
  18. } catch (EntityNotFoundException e) {
  19. throw new ResponseStatusException(
  20. HttpStatus.NOT_FOUND,
  21. e.getMessage()
  22. );
  23. } catch (DuplicateEntityException e) {
  24. throw new ResponseStatusException(
  25. HttpStatus.CONFLICT,
  26. e.getMessage()
  27. );
  28. } catch (UnauthorizedOperationException e){
  29. throw new ResponseStatusException(
  30. HttpStatus.UNAUTHORIZED,
  31. e.getMessage()
  32. );
  33. }
  34.  
  35. }
  36.  
  37.  
  38. ---------------------------------------------------------------------------------------------------------------------------
  39. ------------------------------------------------ UserService --------------------------------------------------------------
  40. ---------------------------------------------------------------------------------------------------------------------------
  41.  
  42. @Override
  43. public void update(User userToBeAuthorized, int userId, User userToBeUpdated,
  44. Optional<String> phoneFromUser, int userRole) {
  45.  
  46. boolean sameUser = userToBeAuthorized.getUserId() == userId;
  47. boolean loggedUserIsAdmin = userToBeAuthorized.getUserType().getUserRole().equalsIgnoreCase("admin");
  48. boolean userToUpdateIsAdmin = userToBeUpdated.getUserType().getUserRole().equalsIgnoreCase("admin");
  49. boolean sameRole = userRole <= userToBeAuthorized.getUserType().getUserTypeId();
  50.  
  51. boolean duplicateExists = true;
  52. boolean sameUserInDB = false;
  53.  
  54. try {
  55. User existingUser = repository.getByEmail(userToBeUpdated.getEmail());
  56. if (existingUser.getUserId() == userToBeUpdated.getUserId()) {
  57. duplicateExists = false;
  58. sameUserInDB = true;
  59. }
  60. } catch (EntityNotFoundException e) {
  61. duplicateExists = false;
  62. }
  63. if (duplicateExists) {
  64. throw new DuplicateEntityException("User", "email", userToBeUpdated.getEmail());
  65. }
  66.  
  67. if (!sameUserInDB){
  68. throw new UnauthorizedOperationException(UPDATE_USER_ERROR_MESSAGE);
  69. }
  70.  
  71. /*Update USER || ADMIN*/
  72. if (sameUser && sameRole){
  73. repository.update(userToBeUpdated);
  74. }
  75.  
  76. /*Promote user*/
  77. if(loggedUserIsAdmin && !sameUser && userRole == 3){
  78. repository.update(userToBeUpdated);
  79. }
  80.  
  81. Phone existingPhone = phoneRepository.getByUserId(userId);
  82.  
  83. /*Admin can add or change its phone*/
  84. if (phoneFromUser.isPresent() && sameUser && loggedUserIsAdmin){
  85. Phone phoneToBe = new Phone();
  86. phoneToBe.setPhone(phoneFromUser.get());
  87. phoneToBe.setUser(userId);
  88. if (existingPhone != null){
  89. phoneRepository.update(phoneToBe);
  90. if (phoneFromUser.get().equals(existingPhone.getPhone())){
  91. throw new DuplicateEntityException("Phone", "phoneNumber", phoneFromUser.get());
  92. }
  93. }
  94. else{
  95. phoneRepository.create(phoneToBe);
  96. }
  97. }
  98. }
  99.  
  100. ---------------------------------------------------------------------------------------------------------------------------
  101. ------------------------------------------------ UserRepository -----------------------------------------------------------
  102. ---------------------------------------------------------------------------------------------------------------------------
  103.  
  104. @Override
  105. public void update(User user) {
  106. try(Session session = sessionFactory.openSession()){
  107. session.beginTransaction();
  108. session.update(user);
  109. session.getTransaction().commit();
  110. }
  111. }
  112.  
  113. ---------------------------------------------------------------------------------------------------------------------------
  114. ------------------------------------------------ PhoneRepository ----------------------------------------------------------
  115. ---------------------------------------------------------------------------------------------------------------------------
  116.  
  117.  
  118.  
  119. @Override
  120. public Phone getByUserId(int userId) {
  121. try(Session session = sessionFactory.openSession()){
  122. Query<Phone> query = session.createQuery
  123. ("from Phones where userId = :userId", Phone.class);
  124. query.setParameter("userId", userId);
  125. Phone phone = null;
  126. if (!query.list().isEmpty()){
  127. phone = query.list().get(0);
  128. }
  129. return phone;
  130. }
  131. }
  132.  
  133. @Override
  134. public void create(Phone phoneToBe) {
  135. try(Session session = sessionFactory.openSession()){
  136. session.beginTransaction();
  137. session.save(phoneToBe);
  138. session.getTransaction().commit();
  139. }
  140. }
  141.  
  142. @Override
  143. public void update(Phone phoneToBe) {
  144. try(Session session = sessionFactory.openSession()){
  145. session.beginTransaction();
  146. session.update(phoneToBe);
  147. session.getTransaction().commit();
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment