diff --git a/nekontakte/src/main/java/com/example/nekontakte/posts/api/PostController.java b/nekontakte/src/main/java/com/example/nekontakte/posts/api/PostController.java index 20e8564..571e061 100644 --- a/nekontakte/src/main/java/com/example/nekontakte/posts/api/PostController.java +++ b/nekontakte/src/main/java/com/example/nekontakte/posts/api/PostController.java @@ -26,13 +26,11 @@ import jakarta.validation.Valid; public class PostController { private final PostService postService; - private final UserService userService; private final ModelMapper modelMapper; public PostController(PostService postService, ModelMapper modelMapper, UserService userService) { this.modelMapper = modelMapper; this.postService = postService; - this.userService = userService; } private PostEntity toEntity(PostDTO dto) { @@ -50,7 +48,7 @@ public class PostController { @GetMapping("/user/{userId}") public List getAllByUserId(@PathVariable(name = "userId") Integer userId) { - return postService.getAllByUser(userService.get(userId)).stream().map(this::toDTO).toList(); + return postService.getAllByUserId(userId).stream().map(this::toDTO).toList(); } @GetMapping("/{id}") diff --git a/nekontakte/src/main/java/com/example/nekontakte/posts/api/PostDTO.java b/nekontakte/src/main/java/com/example/nekontakte/posts/api/PostDTO.java index d766f1c..6ae92eb 100644 --- a/nekontakte/src/main/java/com/example/nekontakte/posts/api/PostDTO.java +++ b/nekontakte/src/main/java/com/example/nekontakte/posts/api/PostDTO.java @@ -11,6 +11,9 @@ public class PostDTO { @JsonProperty(access = JsonProperty.Access.READ_ONLY) private Integer id; + @NotNull + private Integer userId; + @NotNull private Date pubDate; @@ -50,4 +53,12 @@ public class PostDTO { this.html = html; } + public Integer getUserId() { + return userId; + } + + public void setUserId(Integer userId) { + this.userId = userId; + } + } diff --git a/nekontakte/src/main/java/com/example/nekontakte/posts/model/PostEntity.java b/nekontakte/src/main/java/com/example/nekontakte/posts/model/PostEntity.java index 6ecb913..0f8c79d 100644 --- a/nekontakte/src/main/java/com/example/nekontakte/posts/model/PostEntity.java +++ b/nekontakte/src/main/java/com/example/nekontakte/posts/model/PostEntity.java @@ -20,11 +20,14 @@ public class PostEntity extends BaseEntity { @ManyToOne @JoinColumn(name = "userId", nullable = false) private UserEntity user; + @Temporal(value = TemporalType.DATE) @Column(nullable = false) private Date pubDate; + @Column private String image; + @Column private String html; diff --git a/nekontakte/src/main/java/com/example/nekontakte/posts/repository/PostRepository.java b/nekontakte/src/main/java/com/example/nekontakte/posts/repository/PostRepository.java index 052929c..9914a0f 100644 --- a/nekontakte/src/main/java/com/example/nekontakte/posts/repository/PostRepository.java +++ b/nekontakte/src/main/java/com/example/nekontakte/posts/repository/PostRepository.java @@ -5,8 +5,6 @@ import org.springframework.data.repository.CrudRepository; import com.example.nekontakte.posts.model.PostEntity; import java.util.List; -import com.example.nekontakte.users.model.UserEntity; - public interface PostRepository extends CrudRepository { - List findByUser(UserEntity user); + List findByUserId(Integer userId); } diff --git a/nekontakte/src/main/java/com/example/nekontakte/posts/service/PostService.java b/nekontakte/src/main/java/com/example/nekontakte/posts/service/PostService.java index 749a92a..24f82d0 100644 --- a/nekontakte/src/main/java/com/example/nekontakte/posts/service/PostService.java +++ b/nekontakte/src/main/java/com/example/nekontakte/posts/service/PostService.java @@ -9,7 +9,6 @@ import org.springframework.transaction.annotation.Transactional; import com.example.nekontakte.core.errors.NotFoundException; import com.example.nekontakte.posts.model.PostEntity; import com.example.nekontakte.posts.repository.PostRepository; -import com.example.nekontakte.users.model.UserEntity; @Service public class PostService { @@ -20,8 +19,8 @@ public class PostService { } @Transactional(readOnly = true) - public List getAllByUser(UserEntity user) { - return repository.findByUser(user); + public List getAllByUserId(Integer userId) { + return repository.findByUserId(userId); } @Transactional(readOnly = true) diff --git a/nekontakte/src/test/java/com/example/nekontakte/PostServiceTests.java b/nekontakte/src/test/java/com/example/nekontakte/PostServiceTests.java index b4673a8..6dcc9bd 100644 --- a/nekontakte/src/test/java/com/example/nekontakte/PostServiceTests.java +++ b/nekontakte/src/test/java/com/example/nekontakte/PostServiceTests.java @@ -119,7 +119,7 @@ public class PostServiceTests { Assertions.assertEquals(firstUser, postService.get(firstPost.getId()).getUser()); Assertions.assertEquals(secondUser, postService.get(thirdPost.getId()).getUser()); Assertions.assertEquals(3, postService.getAll().size()); - Assertions.assertEquals(2, postService.getAllByUser(firstUser).size()); + Assertions.assertEquals(2, postService.getAllByUserId(firstUser.getId()).size()); } @Test