Skip to content

Commit 7a9db16

Browse files
committed
(TP-110) feat: add and impl FavouritesController
1 parent d17b16e commit 7a9db16

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package kattsyn.dev.rentplace.controllers;
2+
3+
import io.swagger.v3.oas.annotations.Operation;
4+
import io.swagger.v3.oas.annotations.media.Content;
5+
import io.swagger.v3.oas.annotations.media.Schema;
6+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
7+
import io.swagger.v3.oas.annotations.responses.ApiResponses;
8+
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
9+
import io.swagger.v3.oas.annotations.tags.Tag;
10+
import kattsyn.dev.rentplace.dtos.PropertyDTO;
11+
import kattsyn.dev.rentplace.services.FavouritesService;
12+
import lombok.RequiredArgsConstructor;
13+
import org.springframework.http.ResponseEntity;
14+
import org.springframework.security.core.Authentication;
15+
import org.springframework.stereotype.Controller;
16+
import org.springframework.web.bind.annotation.*;
17+
18+
import java.util.List;
19+
20+
@Controller
21+
@RequestMapping("/favourites")
22+
@RequiredArgsConstructor
23+
@Tag(name = "FavouritesController", description = "Контроллер для работы с избранными объявлениями пользователя")
24+
public class FavouritesController {
25+
26+
private final FavouritesService favouritesService;
27+
28+
@Operation(
29+
summary = "Получение избранных объявлений пользователем",
30+
description = "Метод, для получения избранных объявлений пользователем. Для авторизованных")
31+
@ApiResponses(value = {
32+
@ApiResponse(responseCode = "200", description = "Успешно", content = @Content(mediaType = "application/json", schema = @Schema(implementation = PropertyDTO[].class))),
33+
@ApiResponse(responseCode = "500", description = "Непредвиденная ошибка со стороны сервера", content = @Content)
34+
})
35+
@SecurityRequirement(name = "JWT")
36+
@GetMapping("/")
37+
public ResponseEntity<List<PropertyDTO>> findAll(Authentication authentication) {
38+
return ResponseEntity.ok(favouritesService.getUserFavouritesByUserEmail(authentication.getName()));
39+
}
40+
41+
@Operation(
42+
summary = "Добавить объявление в избранное"
43+
)
44+
@ApiResponses(value = {
45+
@ApiResponse(responseCode = "204", description = "Успешно. Без тела ответа", content = @Content),
46+
@ApiResponse(responseCode = "400", description = "Получен некорректный ID", content = @Content),
47+
@ApiResponse(responseCode = "401", description = "Не авторизован", content = @Content),
48+
@ApiResponse(responseCode = "404", description = "Объявление не найдено", content = @Content),
49+
@ApiResponse(responseCode = "422", description = "Ошибка валидации", content = @Content),
50+
@ApiResponse(responseCode = "500", description = "Непредвиденная ошибка со стороны сервера", content = @Content)
51+
})
52+
@SecurityRequirement(name = "JWT")
53+
@PatchMapping(path = "/add/{id}")
54+
public ResponseEntity<Void> addPropertyToFavourites(@PathVariable long id, Authentication authentication) {
55+
favouritesService.addPropertyToFavourites(id, authentication.getName());
56+
57+
return ResponseEntity.ok().build();
58+
}
59+
60+
@Operation(
61+
summary = "Добавить объявление в избранное"
62+
)
63+
@ApiResponses(value = {
64+
@ApiResponse(responseCode = "204", description = "Успешно. Без тела ответа", content = @Content),
65+
@ApiResponse(responseCode = "400", description = "Получен некорректный ID", content = @Content),
66+
@ApiResponse(responseCode = "401", description = "Не авторизован", content = @Content),
67+
@ApiResponse(responseCode = "404", description = "Объявление не найдено", content = @Content),
68+
@ApiResponse(responseCode = "422", description = "Ошибка валидации", content = @Content),
69+
@ApiResponse(responseCode = "500", description = "Непредвиденная ошибка со стороны сервера", content = @Content)
70+
})
71+
@SecurityRequirement(name = "JWT")
72+
@PatchMapping(path = "/remove/{id}")
73+
public ResponseEntity<Void> removePropertyFromFavourites(@PathVariable long id, Authentication authentication) {
74+
favouritesService.removePropertyFromFavourites(id, authentication.getName());
75+
76+
return ResponseEntity.ok().build();
77+
}
78+
79+
}

0 commit comments

Comments
 (0)