Posts API stage 2: /posts/{id} is one post, drop userEmail and /posts/search

- 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>
This commit is contained in:
SlimusMinus
2026-10-21 00:03:39 +03:00
parent 13d09d0073
commit d782bee645
10 changed files with 59 additions and 112 deletions

View File

@@ -1,6 +1,7 @@
package com.krylov.refound.service;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
@@ -11,11 +12,13 @@ import com.krylov.refound.dto.PostResponse;
import com.krylov.refound.entity.Post;
import com.krylov.refound.entity.User;
import com.krylov.refound.enums.ErrorCode;
import com.krylov.refound.enums.PostStatus;
import com.krylov.refound.exception.ApiException;
import com.krylov.refound.mapper.PostMapper;
import com.krylov.refound.repository.PostRepository;
import com.krylov.refound.util.PostVisibility;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
@@ -73,6 +76,37 @@ class PostServiceUserPostsTest {
verify(postRepository, never()).findByUserId(5L, pageable);
}
@Test
void singlePostUnderModerationIsHiddenFromOthers() {
User owner = new User();
owner.setId(5L);
Post post = new Post();
post.setUser(owner);
post.setStatus(PostStatus.MODERATION);
when(postRepository.findById(7L)).thenReturn(Optional.of(post));
when(userService.getCurrentUser())
.thenThrow(new ApiException(ErrorCode.UNAUTHORIZED, "Unauthorized", HttpStatus.UNAUTHORIZED));
assertThatThrownBy(() -> service.getPostById(7L))
.isInstanceOfSatisfying(ApiException.class,
e -> assertThat(e.getStatus()).isEqualTo(HttpStatus.NOT_FOUND));
}
@Test
void singlePostUnderModerationIsVisibleToOwner() {
User owner = new User();
owner.setId(5L);
Post post = new Post();
post.setUser(owner);
post.setStatus(PostStatus.MODERATION);
when(postRepository.findById(7L)).thenReturn(Optional.of(post));
when(userService.getCurrentUser()).thenReturn(owner);
PostResponse dto = new PostResponse();
when(mapper.toResponse(post)).thenReturn(dto);
assertThat(service.getPostById(7L)).isSameAs(dto);
}
@Test
void anonymousGetsOnlyPublishedPosts() {
when(userService.getCurrentUser())