Posts API: user posts page, 201 on create, userLogin, deprecate old routes

- GET /api/v1/users/{userId}/posts returns Page<PostResponse> (owner sees unpublished)
- POST /api/v1/posts returns 201 with the created PostResponse and Location
- PostResponse.userLogin; userEmail kept with the same value as deprecated
- GET /api/v1/posts/{userId} and /search?city= marked deprecated; search is paged
- CORS exposes Retry-After and Location to the frontend

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
SlimusMinus
2026-10-20 11:50:05 +03:00
parent ba6935bb1f
commit ee5fb4e0e0
9 changed files with 197 additions and 26 deletions

View File

@@ -18,6 +18,9 @@ import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import java.net.URI;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
@@ -40,10 +43,15 @@ public class PostController {
private final PostService service;
private final SearchSuggestService searchSuggestService;
/** 201 с созданным объявлением и Location на него (раньше — эхо PostRequest). */
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public PostRequest create(@Valid @ModelAttribute PostRequest request, @RequestParam(value = "images", required = false) List<MultipartFile> images) {
public ResponseEntity<PostResponse> create(@Valid @ModelAttribute PostRequest request, @RequestParam(value = "images", required = false) List<MultipartFile> images) {
log.info("create post type={}, title='{}'", request.getType(), request.getTitle());
return service.create(request, images);
PostResponse created = service.create(request, images);
// Одно объявление пока отдаёт /detail/{id}; /posts/{id} поменяет смысл вторым этапом
URI location = ServletUriComponentsBuilder.fromCurrentRequest()
.path("/detail/{id}").buildAndExpand(created.getId()).toUri();
return ResponseEntity.created(location).body(created);
}
@GetMapping
@@ -69,6 +77,12 @@ public class PostController {
return searchSuggestService.suggest(query);
}
/**
* Объявления ПОЛЬЗОВАТЕЛЯ с id {@code id} (историческое имя маршрута).
* Новый маршрут — GET /api/v1/users/{userId}/posts (постранично). Этот
* останется до деплоя фронта, затем /posts/{id} станет одним объявлением.
*/
@Deprecated
@GetMapping("/{id:\\d+}")
public List<PostResponse> getById(@PathVariable Long id) {
log.info("get feed by id {}", id);
@@ -97,10 +111,13 @@ public class PostController {
return service.updateStatus(id, request.getStatus());
}
/** Устарело: используйте GET /api/v1/posts?city= (лента с тем же фильтром). */
@Deprecated
@GetMapping("/search")
public List<PostResponse> search(@RequestParam(required = false) String city) {
public Page<PostResponse> search(@RequestParam(required = false) String city,
@PageableDefault(size = 20, sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable) {
log.info("search posts by city {}", city);
return service.searchByCity(city);
return service.searchByCity(city, pageable);
}
@GetMapping("/cities")

View File

@@ -0,0 +1,32 @@
package com.krylov.refound.controller;
import com.krylov.refound.dto.PostResponse;
import com.krylov.refound.service.PostService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* Объявления пользователя: GET /api/v1/users/{userId}/posts. Публичный —
* владельцу (с токеном) отдаются и неопубликованные (MODERATION, REJECTED).
*/
@Slf4j
@RestController
@RequiredArgsConstructor
public class UserPostController {
private final PostService postService;
@GetMapping("/api/v1/users/{userId:\\d+}/posts")
public Page<PostResponse> getUserPosts(@PathVariable Long userId,
@PageableDefault(size = 20, sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable) {
log.info("get posts of user {}", userId);
return postService.getPostsByUser(userId, pageable);
}
}