Добавлены новые методы в контроллеры
This commit is contained in:
parent
59262cd474
commit
a05eb93a91
@ -17,6 +17,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.example.nekontakte.posts.model.PostEntity;
|
||||
import com.example.nekontakte.posts.service.PostService;
|
||||
import com.example.nekontakte.users.service.UserService;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
@ -25,11 +26,13 @@ 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) {
|
||||
public PostController(PostService postService, ModelMapper modelMapper, UserService userService) {
|
||||
this.modelMapper = modelMapper;
|
||||
this.postService = postService;
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
private PostEntity toEntity(PostDTO dto) {
|
||||
@ -45,6 +48,11 @@ public class PostController {
|
||||
return postService.getAll().stream().map(this::toDTO).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/user/{userId}")
|
||||
public List<PostDTO> getAllByUserId(@PathVariable(name = "userId") Integer userId) {
|
||||
return postService.getAllByUser(userService.get(userId)).stream().map(this::toDTO).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public PostDTO get(@PathVariable(name = "id") Integer id) {
|
||||
return toDTO(postService.get(id));
|
||||
|
@ -3,6 +3,7 @@ package com.example.nekontakte.subscribes.api;
|
||||
import java.util.List;
|
||||
|
||||
import com.example.nekontakte.core.configurations.Constants;
|
||||
import com.example.nekontakte.posts.api.PostDTO;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
@ -16,6 +17,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.example.nekontakte.subscribes.model.SubscribeEntity;
|
||||
import com.example.nekontakte.subscribes.service.SubscribeService;
|
||||
import com.example.nekontakte.users.service.UserService;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
@ -23,11 +25,13 @@ import jakarta.validation.Valid;
|
||||
@RequestMapping(Constants.API_URL + "/subscribe")
|
||||
public class SubscribeController {
|
||||
private final SubscribeService subscribeService;
|
||||
private final UserService userService;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
public SubscribeController(SubscribeService subscribeService, ModelMapper modelMapper) {
|
||||
public SubscribeController(SubscribeService subscribeService, ModelMapper modelMapper, UserService userService) {
|
||||
this.modelMapper = modelMapper;
|
||||
this.subscribeService = subscribeService;
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
private SubscribeEntity toEntity(SubscribeDTO dto) {
|
||||
@ -43,6 +47,11 @@ public class SubscribeController {
|
||||
return subscribeService.getAll().stream().map(this::toDTO).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/user/{userId}")
|
||||
public List<SubscribeDTO> getAllByUserId(@PathVariable(name = "userId") Integer userId) {
|
||||
return subscribeService.getAllByUser(userService.get(userId)).stream().map(this::toDTO).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public SubscribeDTO get(@PathVariable(name = "id") Integer id) {
|
||||
return toDTO(subscribeService.get(id));
|
||||
|
@ -22,7 +22,7 @@ public class SubscribeService {
|
||||
return repository.getAll();
|
||||
}
|
||||
|
||||
public List<SubscribeEntity> getAllSubscribesByUser(UserEntity user) {
|
||||
public List<SubscribeEntity> getAllByUser(UserEntity user) {
|
||||
return repository.getAll().stream().filter(item -> item.getUser().equals(user)).toList();
|
||||
}
|
||||
|
||||
|
@ -82,7 +82,7 @@ public class SubscribeServiceTests {
|
||||
|
||||
Assertions.assertEquals(secondSubscribe, subscribeService.get(2));
|
||||
Assertions.assertEquals(3, subscribeService.getAll().size());
|
||||
Assertions.assertEquals(2, subscribeService.getAllSubscribesByUser(firstUser).size());
|
||||
Assertions.assertEquals(2, subscribeService.getAllByUser(firstUser).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
x
Reference in New Issue
Block a user