From 6a73a65af032a01044742cfae678369fe88c2879 Mon Sep 17 00:00:00 2001 From: "ns.potapov" Date: Sat, 11 May 2024 13:00:29 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BC=D0=BE=D0=B4=D0=B5=D0=BB=D1=8C=20=D0=BF=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D0=B0,=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B0?= =?UTF-8?q?=D0=B2=20userId,=20=D0=BF=D0=BE=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B5=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=20=D0=BF=D0=BE=D1=81=D1=82=D0=BE=D0=B2=20=D0=BF=D0=BE=20userId?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/nekontakte/posts/api/PostController.java | 4 +--- .../com/example/nekontakte/posts/api/PostDTO.java | 11 +++++++++++ .../example/nekontakte/posts/model/PostEntity.java | 3 +++ .../nekontakte/posts/repository/PostRepository.java | 4 +--- .../example/nekontakte/posts/service/PostService.java | 5 ++--- .../java/com/example/nekontakte/PostServiceTests.java | 2 +- 6 files changed, 19 insertions(+), 10 deletions(-) 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