- GET /api/v1/posts/{id} returns a single post (owner-only for unpublished);
/detail/{id} stays as an alias; user posts live at /api/v1/users/{userId}/posts
- POST /api/v1/posts Location points to /api/v1/posts/{id}
- PostResponse.userEmail removed (use userLogin); unused DialogResponse removed
- GET /api/v1/posts/search?city= removed (same filter: GET /api/v1/posts?city=)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
126 lines
5.9 KiB
Java
126 lines
5.9 KiB
Java
package com.krylov.refound.controller;
|
||
|
||
import com.krylov.refound.dto.PostRequest;
|
||
import com.krylov.refound.dto.PostResponse;
|
||
import com.krylov.refound.dto.StatusUpdateRequest;
|
||
import com.krylov.refound.enums.ErrorCode;
|
||
import com.krylov.refound.exception.ApiException;
|
||
import com.krylov.refound.search.PostFeedFilter;
|
||
import com.krylov.refound.search.SearchSuggestService;
|
||
import com.krylov.refound.service.PostService;
|
||
import jakarta.validation.Valid;
|
||
import java.util.List;
|
||
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.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;
|
||
import org.springframework.web.bind.annotation.PatchMapping;
|
||
import org.springframework.web.bind.annotation.PathVariable;
|
||
import org.springframework.web.bind.annotation.PostMapping;
|
||
import org.springframework.web.bind.annotation.PutMapping;
|
||
import org.springframework.web.bind.annotation.RequestBody;
|
||
import org.springframework.web.bind.annotation.RequestMapping;
|
||
import org.springframework.web.bind.annotation.RequestParam;
|
||
import org.springframework.web.bind.annotation.RestController;
|
||
import org.springframework.web.multipart.MultipartFile;
|
||
|
||
@Slf4j
|
||
@RestController
|
||
@RequestMapping("/api/v1/posts")
|
||
@RequiredArgsConstructor
|
||
public class PostController {
|
||
|
||
private final PostService service;
|
||
private final SearchSuggestService searchSuggestService;
|
||
|
||
/** 201 с созданным объявлением и Location на него (раньше — эхо PostRequest). */
|
||
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||
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());
|
||
PostResponse created = service.create(request, images);
|
||
URI location = ServletUriComponentsBuilder.fromCurrentRequest()
|
||
.path("/{id}").buildAndExpand(created.getId()).toUri();
|
||
return ResponseEntity.created(location).body(created);
|
||
}
|
||
|
||
@GetMapping
|
||
public Page<PostResponse> getFeed(@RequestParam(required = false) String search,
|
||
@RequestParam(required = false) String type,
|
||
@RequestParam(name = "category", required = false) List<String> categories,
|
||
@RequestParam(name = "city", required = false) List<String> cities,
|
||
@PageableDefault(size = 20, sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable) {
|
||
log.info("get feed: search='{}', type={}, categories={}, cities={}", search, type, categories, cities);
|
||
PostFeedFilter filter;
|
||
try {
|
||
filter = PostFeedFilter.of(type, categories, cities);
|
||
} catch (IllegalArgumentException e) {
|
||
throw new ApiException(ErrorCode.VALIDATION_ERROR, "Некорректный фильтр ленты: " + e.getMessage(),
|
||
HttpStatus.BAD_REQUEST);
|
||
}
|
||
return service.getFeed(search, filter, pageable);
|
||
}
|
||
|
||
/** Подсказки для последнего слова строки поиска: «потерян барс» → [барсетка, барсик]. */
|
||
@GetMapping("/search/suggest")
|
||
public List<String> suggest(@RequestParam(name = "q", defaultValue = "") String query) {
|
||
return searchSuggestService.suggest(query);
|
||
}
|
||
|
||
/**
|
||
* Одно объявление. Неопубликованное (MODERATION, REJECTED) видит только
|
||
* владелец, остальным — 404. Объявления пользователя —
|
||
* GET /api/v1/users/{userId}/posts.
|
||
*/
|
||
@GetMapping("/{id:\\d+}")
|
||
public PostResponse getById(@PathVariable Long id) {
|
||
log.info("get post by id {}", id);
|
||
return service.getPostById(id);
|
||
}
|
||
|
||
@PutMapping(value = "/{id}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||
public PostResponse update(@PathVariable Long id, @RequestParam String type, @RequestParam String title, @RequestParam(required = false) String address,
|
||
@RequestParam(required = false) Double latitude, @RequestParam(required = false) Double longitude, @RequestParam(required = false) String description,
|
||
@RequestParam(required = false) String category, @RequestParam(required = false) String phone, @RequestParam(required = false) Boolean reward,
|
||
@RequestParam(required = false) String rewardText,
|
||
@RequestParam(value = "images", required = false) List<MultipartFile> images, @RequestParam(value = "existingImages", required = false) String existingImagesJson) {
|
||
log.info("update post {}", id);
|
||
return service.update(id, type, title, address, latitude, longitude, description, category, phone, reward, rewardText, images, existingImagesJson);
|
||
}
|
||
|
||
@DeleteMapping("/{id}")
|
||
public void delete(@PathVariable Long id) {
|
||
log.info("delete feed by id {}", id);
|
||
service.delete(id);
|
||
}
|
||
|
||
@PatchMapping("/{id}/status")
|
||
public PostResponse updateStatus(@PathVariable Long id, @RequestBody StatusUpdateRequest request) {
|
||
log.info("update feed {} on status {}", id, request.getStatus());
|
||
return service.updateStatus(id, request.getStatus());
|
||
}
|
||
|
||
@GetMapping("/cities")
|
||
public List<String> getCities() {
|
||
log.info("get distinct cities");
|
||
return service.getCities();
|
||
}
|
||
|
||
/** Прежний адрес одного объявления — алиас GET /api/v1/posts/{id}. */
|
||
@GetMapping("/detail/{postId}")
|
||
public PostResponse getPostDetail(@PathVariable Long postId) {
|
||
log.info("get post detail {}", postId);
|
||
return service.getPostById(postId);
|
||
}
|
||
|
||
}
|