Compare commits

...

2 Commits

6 changed files with 20 additions and 7 deletions

View File

@ -26,13 +26,11 @@ import jakarta.validation.Valid;
public class PostController { public class PostController {
private final PostService postService; private final PostService postService;
private final UserService userService;
private final ModelMapper modelMapper; private final ModelMapper modelMapper;
public PostController(PostService postService, ModelMapper modelMapper, UserService userService) { public PostController(PostService postService, ModelMapper modelMapper, UserService userService) {
this.modelMapper = modelMapper; this.modelMapper = modelMapper;
this.postService = postService; this.postService = postService;
this.userService = userService;
} }
private PostEntity toEntity(PostDTO dto) { private PostEntity toEntity(PostDTO dto) {
@ -50,7 +48,7 @@ public class PostController {
@GetMapping("/user/{userId}") @GetMapping("/user/{userId}")
public List<PostDTO> getAllByUserId(@PathVariable(name = "userId") Integer userId) { public List<PostDTO> 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}") @GetMapping("/{id}")

View File

@ -11,6 +11,9 @@ public class PostDTO {
@JsonProperty(access = JsonProperty.Access.READ_ONLY) @JsonProperty(access = JsonProperty.Access.READ_ONLY)
private Integer id; private Integer id;
@NotNull
private Integer userId;
@NotNull @NotNull
private Date pubDate; private Date pubDate;
@ -50,4 +53,12 @@ public class PostDTO {
this.html = html; this.html = html;
} }
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
} }

View File

@ -20,11 +20,14 @@ public class PostEntity extends BaseEntity {
@ManyToOne @ManyToOne
@JoinColumn(name = "userId", nullable = false) @JoinColumn(name = "userId", nullable = false)
private UserEntity user; private UserEntity user;
@Temporal(value = TemporalType.DATE) @Temporal(value = TemporalType.DATE)
@Column(nullable = false) @Column(nullable = false)
private Date pubDate; private Date pubDate;
@Column @Column
private String image; private String image;
@Column @Column
private String html; private String html;

View File

@ -3,6 +3,8 @@ package com.example.nekontakte.posts.repository;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import com.example.nekontakte.posts.model.PostEntity; import com.example.nekontakte.posts.model.PostEntity;
import java.util.List;
public interface PostRepository extends CrudRepository<PostEntity, Integer> { public interface PostRepository extends CrudRepository<PostEntity, Integer> {
List<PostEntity> findByUserId(Integer userId);
} }

View File

@ -9,7 +9,6 @@ import org.springframework.transaction.annotation.Transactional;
import com.example.nekontakte.core.errors.NotFoundException; import com.example.nekontakte.core.errors.NotFoundException;
import com.example.nekontakte.posts.model.PostEntity; import com.example.nekontakte.posts.model.PostEntity;
import com.example.nekontakte.posts.repository.PostRepository; import com.example.nekontakte.posts.repository.PostRepository;
import com.example.nekontakte.users.model.UserEntity;
@Service @Service
public class PostService { public class PostService {
@ -20,8 +19,8 @@ public class PostService {
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public List<PostEntity> getAllByUser(UserEntity user) { public List<PostEntity> getAllByUserId(Integer userId) {
return getAll().stream().filter(item -> item.getUser().equals(user)).toList(); return repository.findByUserId(userId);
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)

View File

@ -119,7 +119,7 @@ public class PostServiceTests {
Assertions.assertEquals(firstUser, postService.get(firstPost.getId()).getUser()); Assertions.assertEquals(firstUser, postService.get(firstPost.getId()).getUser());
Assertions.assertEquals(secondUser, postService.get(thirdPost.getId()).getUser()); Assertions.assertEquals(secondUser, postService.get(thirdPost.getId()).getUser());
Assertions.assertEquals(3, postService.getAll().size()); Assertions.assertEquals(3, postService.getAll().size());
Assertions.assertEquals(2, postService.getAllByUser(firstUser).size()); Assertions.assertEquals(2, postService.getAllByUserId(firstUser.getId()).size());
} }
@Test @Test