Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bugs with author and user id in postController #631

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.softserveinc.dokazovi.dto.post.PostPublishedAtDTO;
import com.softserveinc.dokazovi.dto.post.PostSaveFromUserDTO;
import com.softserveinc.dokazovi.dto.post.PostStatusDTO;
import com.softserveinc.dokazovi.entity.AuthorEntity;
import com.softserveinc.dokazovi.entity.DirectionEntity;
import com.softserveinc.dokazovi.entity.PostEntity;
import com.softserveinc.dokazovi.entity.UserEntity;
Expand Down Expand Up @@ -213,8 +214,10 @@ public Page<PostDTO> findAllByDirection(
@Override
public Page<PostDTO> findPostsByAuthorIdAndDirections(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good idea to rename the method, to make sure you don't accidentally skip some calls where id type got changed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, while this is not applicable to your current design, some apps (especially based on domain-driven-development idioms, which you might see someday) declare dedicated classes for ID types, that allows type-checking the IDs you are passing to your methods.

Pageable pageable, Integer authorId, Set<Integer> directions) {

return postRepository.findPostsByAuthorIdAndDirections(pageable, authorId, directions)
AuthorEntity author = authorRepository.findById(authorId)
.orElseThrow(() -> new EntityNotFoundException("Author with id " + authorId + " not found"));
Integer userId = author.getProfile().getId();
return postRepository.findPostsByAuthorIdAndDirections(pageable, userId, directions)
.map(postMapper::toPostDTO);
}

Expand Down Expand Up @@ -338,34 +341,40 @@ public Page<PostMainPageDTO> findLatestByPostTypesAndOriginsForMobile(Pageable p
@Override
public Page<PostDTO> findAllByExpertAndTypeAndDirections(Integer expertId, Set<Integer> typeId,
Set<Integer> directionId, Pageable pageable) {
AuthorEntity author = authorRepository.findById(expertId)
.orElseThrow(() -> new EntityNotFoundException("Author with id " + expertId + " not found"));
Integer userId = author.getProfile().getId();
if (typeId == null && directionId == null) {
return postRepository.findAllByAuthorIdAndStatusOrderByPublishedAtDesc(expertId, PostStatus.PUBLISHED,
return postRepository.findAllByAuthorIdAndStatusOrderByPublishedAtDesc(userId, PostStatus.PUBLISHED,
pageable)
.map(postMapper::toPostDTO);
}
if (typeId == null) {
return postRepository.findPostsByAuthorIdAndDirections(pageable, expertId, directionId)
return postRepository.findPostsByAuthorIdAndDirections(pageable, userId, directionId)
.map(postMapper::toPostDTO);
}
if (directionId == null) {
return postRepository
.findAllByAuthorIdAndTypeIdInAndStatus(expertId, typeId, PostStatus.PUBLISHED, pageable)
.findAllByAuthorIdAndTypeIdInAndStatus(userId, typeId, PostStatus.PUBLISHED, pageable)
.map(postMapper::toPostDTO);
}
return postRepository.findAllByExpertAndByDirectionsAndByPostType(expertId, typeId, directionId, pageable)
return postRepository.findAllByExpertAndByDirectionsAndByPostType(userId, typeId, directionId, pageable)
.map(postMapper::toPostDTO);
}

@Override
public Page<PostDTO> findAllByExpertAndTypeAndStatus(Integer expertId, Set<Integer> typeId,
PostStatus postStatus, Pageable pageable) {
AuthorEntity author = authorRepository.findById(expertId)
.orElseThrow(() -> new EntityNotFoundException("Author with id " + expertId + " not found"));
Integer userId = author.getProfile().getId();
if (typeId == null) {
return postRepository
.findAllByAuthorIdAndStatusOrderByPublishedAtDesc(expertId, postStatus, pageable)
.findAllByAuthorIdAndStatusOrderByPublishedAtDesc(userId, postStatus, pageable)
.map(postMapper::toPostDTO);
}
return postRepository
.findAllByAuthorIdAndTypeIdInAndStatus(expertId, typeId, postStatus, pageable)
.findAllByAuthorIdAndTypeIdInAndStatus(userId, typeId, postStatus, pageable)
.map(postMapper::toPostDTO);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anySet;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
Expand All @@ -84,6 +85,7 @@ class PostServiceImplTest {
private PostServiceImpl postService;
private Page<PostEntity> postEntityPage;
private UserEntity userEntity;
private AuthorEntity authorEntity;
@Mock
private GoogleAnalytics googleAnalytics;
@Mock
Expand All @@ -107,6 +109,10 @@ void init() {
.password("12345")
.role(roleEntity)
.build();
authorEntity = AuthorEntity.builder()
.id(1)
.profile(userEntity)
.build();
}

@Test
Expand Down Expand Up @@ -292,6 +298,7 @@ void findImportantPosts() {
void findPostsByAuthorIdAndDirections_WhenWrong_ThrowException() {
Set<Integer> directions = Set.of(1, 4);
Pageable pageable = PageRequest.of(0, 12);
when(authorRepository.findById(anyInt())).thenReturn(Optional.of(authorEntity));
when(postRepository.findPostsByAuthorIdAndDirections(any(), any(), any()))
.thenThrow(new EntityNotFoundException("Id does not exist"));
assertThrows(EntityNotFoundException.class, () -> postService
Expand All @@ -302,6 +309,7 @@ void findPostsByAuthorIdAndDirections_WhenWrong_ThrowException() {
void findPostsByAuthorIdAndDirections() {
Set<Integer> directions = Set.of(1, 4);
Pageable pageable = PageRequest.of(0, 12);
when(authorRepository.findById(anyInt())).thenReturn(Optional.of(authorEntity));
when(postRepository.findPostsByAuthorIdAndDirections(any(), any(), any()))
.thenReturn(postEntityPage);
postService.findPostsByAuthorIdAndDirections(pageable, 1, directions);
Expand Down Expand Up @@ -355,6 +363,7 @@ void findAllByDirectionAndTypeAndTags() {
@Test
void findAllByExpert() {
Integer expertId = 3;
when(authorRepository.findById(anyInt())).thenReturn(Optional.of(authorEntity));
when(postRepository.findAllByAuthorIdAndStatusOrderByPublishedAtDesc(
any(Integer.class), any(PostStatus.class), any(Pageable.class)))
.thenReturn(postEntityPage);
Expand All @@ -366,6 +375,7 @@ void findAllByExpert() {
void findAllByExpertAndType() {
Integer expertId = 5;
Set<Integer> typeId = Set.of(1, 2);
when(authorRepository.findById(anyInt())).thenReturn(Optional.of(authorEntity));
when(postRepository.findAllByAuthorIdAndTypeIdInAndStatus(any(Integer.class),
anySet(), any(PostStatus.class), any(Pageable.class)))
.thenReturn(postEntityPage);
Expand All @@ -377,6 +387,7 @@ void findAllByExpertAndType() {
void findAllByExpertAndDirections() {
Integer expertId = 5;
Set<Integer> directionId = Set.of(2, 3);
when(authorRepository.findById(anyInt())).thenReturn(Optional.of(authorEntity));
when(postRepository.findPostsByAuthorIdAndDirections(any(Pageable.class), any(Integer.class),
anySet()))
.thenReturn(postEntityPage);
Expand All @@ -389,6 +400,7 @@ void findAllByExpertAndTypeAndDirection() {
Integer expertId = 5;
Set<Integer> directionId = Set.of(2, 3);
Set<Integer> typeId = Set.of(1, 2);
when(authorRepository.findById(anyInt())).thenReturn(Optional.of(authorEntity));
when(postRepository.findAllByExpertAndByDirectionsAndByPostType(any(Integer.class),
anySet(), anySet(), any(Pageable.class)))
.thenReturn(postEntityPage);
Expand All @@ -400,6 +412,7 @@ void findAllByExpertAndTypeAndDirection() {
void findAllByExpertAndStatus() {
Integer expertId = 3;
PostStatus postStatus = PostStatus.DRAFT;
when(authorRepository.findById(anyInt())).thenReturn(Optional.of(authorEntity));
when(postRepository.findAllByAuthorIdAndStatusOrderByPublishedAtDesc(
any(Integer.class), any(PostStatus.class), any(Pageable.class)))
.thenReturn(postEntityPage);
Expand All @@ -412,6 +425,7 @@ void findAllByExpertAndTypeAndStatus() {
Integer expertId = 5;
Set<Integer> typeId = Set.of(1, 2);
PostStatus postStatus = PostStatus.DRAFT;
when(authorRepository.findById(anyInt())).thenReturn(Optional.of(authorEntity));
when(postRepository.findAllByAuthorIdAndTypeIdInAndStatus(any(Integer.class),
anySet(), any(PostStatus.class), any(Pageable.class)))
.thenReturn(postEntityPage);
Expand Down
Loading