Compare commits
2 Commits
a43e17addb
...
6a73a65af0
Author | SHA1 | Date | |
---|---|---|---|
6a73a65af0 | |||
edc6422797 |
@ -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<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}")
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
|
@ -3,6 +3,8 @@ package com.example.nekontakte.posts.repository;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import com.example.nekontakte.posts.model.PostEntity;
|
||||
import java.util.List;
|
||||
|
||||
public interface PostRepository extends CrudRepository<PostEntity, Integer> {
|
||||
List<PostEntity> findByUserId(Integer userId);
|
||||
}
|
||||
|
@ -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<PostEntity> getAllByUser(UserEntity user) {
|
||||
return getAll().stream().filter(item -> item.getUser().equals(user)).toList();
|
||||
public List<PostEntity> getAllByUserId(Integer userId) {
|
||||
return repository.findByUserId(userId);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user