PhoneController.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package it.bgates.remotebe.controller;
  2. import it.bgates.remotebe.controller.beans.FilterBean;
  3. import it.bgates.remotebe.entities.Phone;
  4. import it.bgates.remotebe.exception.InsufficientCreditException;
  5. import it.bgates.remotebe.exception.NotFoundException;
  6. import it.bgates.remotebe.exception.PermissionDeniedException;
  7. import it.bgates.remotebe.exception.UserNotFoundException;
  8. import it.bgates.remotebe.service.PhoneService;
  9. import lombok.RequiredArgsConstructor;
  10. import org.springframework.http.HttpStatus;
  11. import org.springframework.http.ProblemDetail;
  12. import org.springframework.http.ResponseEntity;
  13. import org.springframework.web.bind.annotation.*;
  14. import java.security.Principal;
  15. import java.util.List;
  16. import static org.springframework.http.HttpStatus.*;
  17. import static org.springframework.http.ProblemDetail.forStatus;
  18. @RestController
  19. @RequestMapping("/phones")
  20. @RequiredArgsConstructor
  21. public class PhoneController {
  22. private final PhoneService phoneService;
  23. /**
  24. * Retrieves a list of phones associated with a specific device ID.
  25. *
  26. * @param id the unique identifier of the device whose associated phones are to be retrieved
  27. * @param principal the security principal of the currently authenticated user
  28. * @return a ResponseEntity containing a list of phones associated with the specified device ID;
  29. * returns a 412 status if the user is not found or a 404 status if the device or its phones are not found
  30. */
  31. @GetMapping("/{id}")
  32. public ResponseEntity<List<Phone>> getDevicePhones(@PathVariable Integer id, Principal principal) {
  33. try {
  34. List<Phone> phones = phoneService.getPhonesByDeviceId(id, principal);
  35. return ResponseEntity.ok(phones);
  36. } catch (UserNotFoundException e) {
  37. return ResponseEntity.status(PRECONDITION_FAILED).build();
  38. } catch(NotFoundException e) {
  39. return ResponseEntity.status(NOT_FOUND).build();
  40. }
  41. }
  42. /**
  43. * Adds a new phone entity associated with the authenticated user.
  44. *
  45. * @param phone the phone entity to be added
  46. * the phone entity should contain the device (at least the "id" field) to
  47. * which the phone will be associated
  48. * @param principal the security principal of the currently authenticated user
  49. * @return a ResponseEntity containing the added phone entity if successful;
  50. * returns a 500 status with error details if an InsufficientCreditException occurs,
  51. * a 412 status if the user is not found, or a 500 status for other exceptions
  52. **/
  53. @PostMapping("")
  54. public ResponseEntity<Phone> addPhone(@RequestBody Phone phone, Principal principal) {
  55. try {
  56. return ResponseEntity.ok(phoneService.addPhone(phone, principal));
  57. } catch (InsufficientCreditException ex) {
  58. ProblemDetail pd = forStatus(500);
  59. pd.setDetail(ex.getMessage());
  60. return ResponseEntity.of(pd).build();
  61. } catch (UserNotFoundException e) {
  62. return ResponseEntity.status(PRECONDITION_FAILED).build();
  63. } catch (Exception e) {
  64. ProblemDetail pd = forStatus(HttpStatus.INTERNAL_SERVER_ERROR);
  65. pd.setDetail(e.getMessage());
  66. return ResponseEntity.of(pd).build();
  67. }
  68. }
  69. /**
  70. * Deletes the phone entities associated with a specified id.
  71. * The service checks if the current user is authorized to access the device
  72. * that has the phone. Throws exception if not, else mark the phone number
  73. * for deletion and synchronization.
  74. * On next send-configuration, the phone is removed from the device using
  75. * the SMS delete command, and the phone is remove ONLY IF it has
  76. * from and to dates set (temporary number).
  77. *
  78. * @param id the unique identifier of the phone to be deleted
  79. * @param principal the security principal of the currently authenticated user
  80. * @return a ResponseEntity containing a boolean value indicating the success of the deletion;
  81. * returns a 412 status if the user is not found,
  82. * a 404 status if the device or phones are not found,
  83. * or a 500 status with error details for a permission denial
  84. */
  85. @DeleteMapping("/{id}")
  86. public ResponseEntity<Boolean> deleteDevicePhones(@PathVariable Integer id, Principal principal) {
  87. try {
  88. boolean res = phoneService.deletePhones(id, principal);
  89. return ResponseEntity.ok(res);
  90. } catch (UserNotFoundException e) {
  91. return ResponseEntity.status(PRECONDITION_FAILED).build();
  92. } catch(NotFoundException e) {
  93. return ResponseEntity.status(NOT_FOUND).build();
  94. } catch (PermissionDeniedException e) {
  95. ProblemDetail pd = forStatus(HttpStatus.INTERNAL_SERVER_ERROR);
  96. pd.setDetail(e.getMessage());
  97. return ResponseEntity.of(pd).build();
  98. }
  99. }
  100. /**
  101. * Filters a list of phones based on the specified device ID and optional filter criteria.
  102. *
  103. * @param deviceId the unique identifier of the device to filter phones for
  104. * @param filterBean the filter criteria for phones; can be null if no filtering is required
  105. * @param principal the security principal of the currently authenticated user
  106. * @return a ResponseEntity containing the list of filtered phones if successful
  107. */
  108. @PostMapping("/filter/{deviceId}")
  109. public ResponseEntity<List<Phone>> filter(
  110. @PathVariable() Integer deviceId,
  111. @RequestBody(required = false) FilterBean filterBean,
  112. Principal principal
  113. ) {
  114. try {
  115. String filter = (filterBean != null) ? filterBean.getFilter() : null;
  116. List<Phone> phones = phoneService.filter(deviceId, filter, principal);
  117. return ResponseEntity.ok(phones);
  118. } catch (UserNotFoundException e) {
  119. return ResponseEntity.status(PRECONDITION_FAILED).build();
  120. }
  121. }
  122. /**
  123. * Deactivates a phone associated with the specified ID.
  124. * The service ensures that the authenticated user has the necessary permissions
  125. * to deactivate the phone. If successful, the phone will no longer be active.
  126. *
  127. * @param id the unique identifier of the phone to be deactivated
  128. * @param principal the security principal of the currently authenticated user
  129. * @return a ResponseEntity containing a boolean value indicating whether the deactivation was successful;
  130. * returns a 412 status if the user is not found, or a 404 status if the phone is not found
  131. */
  132. @GetMapping("/deactivate/{id}")
  133. public ResponseEntity<Boolean> deactivatePhone(@PathVariable Integer id, Principal principal) {
  134. try {
  135. Boolean res = phoneService.deactivatePhone(id, principal);
  136. return ResponseEntity.ok(res);
  137. } catch (UserNotFoundException e) {
  138. return ResponseEntity.status(PRECONDITION_FAILED).build();
  139. } catch (NotFoundException e) {
  140. return ResponseEntity.status(NOT_FOUND).build();
  141. } catch (PermissionDeniedException e) {
  142. return ResponseEntity.status(UNAUTHORIZED).build();
  143. }
  144. }
  145. /**
  146. * Activates a phone associated with the specified ID.
  147. *
  148. * @param id the unique identifier of the phone to be activated.
  149. * @param principal the authenticated user making the request.
  150. * @return a ResponseEntity containing a Boolean indicating the success of the activation,
  151. * or an appropriate HTTP status if an error occurs.
  152. */
  153. @GetMapping("/activate/{id}")
  154. public ResponseEntity<Boolean> activatePhone(@PathVariable Integer id, Principal principal) {
  155. try {
  156. Boolean res = phoneService.activatePhone(id, principal);
  157. return ResponseEntity.ok(res);
  158. } catch (UserNotFoundException e) {
  159. return ResponseEntity.status(PRECONDITION_FAILED).build();
  160. } catch (NotFoundException e) {
  161. return ResponseEntity.status(NOT_FOUND).build();
  162. } catch (PermissionDeniedException e) {
  163. return ResponseEntity.status(UNAUTHORIZED).build();
  164. }
  165. }
  166. }