Поправил модель поста, добавав userId, поправил получение постов по userId

This commit is contained in:
Никита Потапов 2024-05-11 13:00:29 +04:00
parent edc6422797
commit 6a73a65af0
6 changed files with 19 additions and 10 deletions

View File

@ -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}")

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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<PostEntity, Integer> {
List<PostEntity> findByUser(UserEntity user);
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.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 repository.findByUser(user);
public List<PostEntity> getAllByUserId(Integer userId) {
return repository.findByUserId(userId);
}
@Transactional(readOnly = true)

View File

@ -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