diff --git a/rentplace/src/main/java/kattsyn/dev/rentplace/controllers/PropertyController.java b/rentplace/src/main/java/kattsyn/dev/rentplace/controllers/PropertyController.java index 4281fd7..f2e9ebc 100644 --- a/rentplace/src/main/java/kattsyn/dev/rentplace/controllers/PropertyController.java +++ b/rentplace/src/main/java/kattsyn/dev/rentplace/controllers/PropertyController.java @@ -76,6 +76,21 @@ public ResponseEntity> getProperties() { return ResponseEntity.ok(properties); } + @Operation( + summary = "Получение объявлений пользователя", + description = "Позволяет получить все объявления пользователя по его токену. Только для авторизованных пользователей." + ) + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "Успешно", content = @Content(mediaType = "application/json", schema = @Schema(implementation = PropertyDTO[].class))), + @ApiResponse(responseCode = "500", description = "Непредвиденная ошибка со стороны сервера", content = @Content) + }) + @SecurityRequirement(name = "JWT") + @GetMapping("/my") + public ResponseEntity> getUserProperties(Authentication authentication) { + List properties = propertyService.findAllByOwnerEmail(authentication.getName()); + return ResponseEntity.ok(properties); + } + @Operation( summary = "Получить объявление", description = "Получить объявление по id" diff --git a/rentplace/src/main/java/kattsyn/dev/rentplace/controllers/ReservationController.java b/rentplace/src/main/java/kattsyn/dev/rentplace/controllers/ReservationController.java index dac6937..1a4c599 100644 --- a/rentplace/src/main/java/kattsyn/dev/rentplace/controllers/ReservationController.java +++ b/rentplace/src/main/java/kattsyn/dev/rentplace/controllers/ReservationController.java @@ -44,6 +44,21 @@ public ResponseEntity> getReservations() { return ResponseEntity.ok(reservationDTOS); } + @Operation( + summary = "Получение всех бронирований пользователя", + description = "Позволяет получить все бронирования пользователя" + ) + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "Успешно", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ReservationDTO[].class))), + @ApiResponse(responseCode = "500", description = "Непредвиденная ошибка со стороны сервера", content = @Content) + }) + @GetMapping("/my") + @SecurityRequirement(name = "JWT") + public ResponseEntity> getUserReservations(Authentication authentication) { + List reservationDTOS = reservationService.findAllReservationsByRenterEmail(authentication.getName()); + return ResponseEntity.ok(reservationDTOS); + } + @Operation( summary = "Получение бронирования", description = "Получение бронирования по id") @@ -78,7 +93,6 @@ public ResponseEntity getReservation(@PathVariable }) @PreAuthorize("hasAuthority('ROLE_ADMIN') or hasAuthority('ROLE_USER')" ) @SecurityRequirement(name = "JWT") - @SecurityRequirement(name = "JWT") @PostMapping(path = "/", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public ResponseEntity createReservation(@Valid @ModelAttribute ReservationCreateEditDTO reservationCreateEditDTO, Authentication authentication) { diff --git a/rentplace/src/main/java/kattsyn/dev/rentplace/repositories/PropertyRepository.java b/rentplace/src/main/java/kattsyn/dev/rentplace/repositories/PropertyRepository.java index b1e02fc..12c04d5 100644 --- a/rentplace/src/main/java/kattsyn/dev/rentplace/repositories/PropertyRepository.java +++ b/rentplace/src/main/java/kattsyn/dev/rentplace/repositories/PropertyRepository.java @@ -18,4 +18,6 @@ public interface PropertyRepository extends JpaRepository { """) List findAllWithRelations(); + List findAllByOwnerEmail(String email); + } diff --git a/rentplace/src/main/java/kattsyn/dev/rentplace/repositories/ReservationRepository.java b/rentplace/src/main/java/kattsyn/dev/rentplace/repositories/ReservationRepository.java index c9f27ec..433c2f6 100644 --- a/rentplace/src/main/java/kattsyn/dev/rentplace/repositories/ReservationRepository.java +++ b/rentplace/src/main/java/kattsyn/dev/rentplace/repositories/ReservationRepository.java @@ -3,5 +3,10 @@ import kattsyn.dev.rentplace.entities.Reservation; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; + public interface ReservationRepository extends JpaRepository { + + List findAllByRenterEmail(String email); + } diff --git a/rentplace/src/main/java/kattsyn/dev/rentplace/services/PropertyService.java b/rentplace/src/main/java/kattsyn/dev/rentplace/services/PropertyService.java index a7d9cb9..545be3f 100644 --- a/rentplace/src/main/java/kattsyn/dev/rentplace/services/PropertyService.java +++ b/rentplace/src/main/java/kattsyn/dev/rentplace/services/PropertyService.java @@ -16,6 +16,8 @@ public interface PropertyService { List findAll(); + List findAllByOwnerEmail(String email); + PropertyDTO findById(long id); Property getPropertyById(long id); diff --git a/rentplace/src/main/java/kattsyn/dev/rentplace/services/ReservationService.java b/rentplace/src/main/java/kattsyn/dev/rentplace/services/ReservationService.java index ecca812..f46da58 100644 --- a/rentplace/src/main/java/kattsyn/dev/rentplace/services/ReservationService.java +++ b/rentplace/src/main/java/kattsyn/dev/rentplace/services/ReservationService.java @@ -26,4 +26,6 @@ public interface ReservationService { List findAllReservations(); + List findAllReservationsByRenterEmail(String email); + } diff --git a/rentplace/src/main/java/kattsyn/dev/rentplace/services/impl/PropertyServiceImpl.java b/rentplace/src/main/java/kattsyn/dev/rentplace/services/impl/PropertyServiceImpl.java index 44d75db..82daa9e 100644 --- a/rentplace/src/main/java/kattsyn/dev/rentplace/services/impl/PropertyServiceImpl.java +++ b/rentplace/src/main/java/kattsyn/dev/rentplace/services/impl/PropertyServiceImpl.java @@ -64,6 +64,11 @@ public List findAll() { return propertyMapper.fromProperties(propertyRepository.findAllWithRelations()); } + @Override + public List findAllByOwnerEmail(String email) { + return propertyMapper.fromProperties(propertyRepository.findAllByOwnerEmail(email)); + } + @Override @Transactional public Property getPropertyById(long id) { diff --git a/rentplace/src/main/java/kattsyn/dev/rentplace/services/impl/ReservationServiceImpl.java b/rentplace/src/main/java/kattsyn/dev/rentplace/services/impl/ReservationServiceImpl.java index e4e0aa7..7ac43a6 100644 --- a/rentplace/src/main/java/kattsyn/dev/rentplace/services/impl/ReservationServiceImpl.java +++ b/rentplace/src/main/java/kattsyn/dev/rentplace/services/impl/ReservationServiceImpl.java @@ -59,6 +59,11 @@ public List findAllReservations() { return reservationMapper.fromReservations(reservationRepository.findAll()); } + @Override + public List findAllReservationsByRenterEmail(String email) { + return reservationMapper.fromReservations(reservationRepository.findAllByRenterEmail(email)); + } + @Override public Reservation getReservationById(long reservationId) { return reservationRepository.findById(reservationId).orElseThrow(