mirror of
https://github.com/usatiuk/y.git
synced 2025-10-28 10:37:47 +01:00
readbyfollowees
This commit is contained in:
@@ -7,10 +7,8 @@ import com.usatiuk.tjv.y.server.entity.Person;
|
||||
import com.usatiuk.tjv.y.server.service.PersonService;
|
||||
import com.usatiuk.tjv.y.server.service.exceptions.UserAlreadyExistsException;
|
||||
import com.usatiuk.tjv.y.server.service.exceptions.UserNotFoundException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.Optional;
|
||||
@@ -41,7 +39,7 @@ public class PersonController {
|
||||
public PersonTo get(@PathVariable String username) throws UserNotFoundException {
|
||||
Optional<Person> found = personService.readByUsername(username);
|
||||
|
||||
if (found.isEmpty()) throw new ResponseStatusException(HttpStatus.NOT_FOUND);
|
||||
if (found.isEmpty()) throw new UserNotFoundException();
|
||||
|
||||
return PersonMapper.makeDto(found.get());
|
||||
}
|
||||
|
||||
@@ -43,6 +43,11 @@ public class PostController {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
@GetMapping(path = "/following")
|
||||
public Stream<PostTo> readAllByFollowees(Principal principal) {
|
||||
return postService.readByPersonFollowees(principal.getName()).stream().map(PostMapper::makeDto);
|
||||
}
|
||||
|
||||
@GetMapping(path = "/{id}")
|
||||
public PostTo get(@PathVariable long id) {
|
||||
var post = postService.readById(id);
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.usatiuk.tjv.y.server.repository;
|
||||
import com.usatiuk.tjv.y.server.entity.Post;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
|
||||
@@ -11,5 +12,8 @@ import java.util.Collection;
|
||||
public interface PostRepository extends PagingAndSortingRepository<Post, Long>, CrudRepository<Post, Long> {
|
||||
Collection<Post> findByAuthorUuid(String authorUuid);
|
||||
|
||||
Page<Post> findByAuthorUuid(String authorUuid, Pageable pageable);
|
||||
@Query(value = "SELECT p FROM Post p " +
|
||||
"WHERE EXISTS " +
|
||||
"(SELECT u FROM Person u LEFT JOIN u.following f where u.uuid = :personUuid and f.uuid = p.author.uuid)")
|
||||
Collection<Post> findByPersonFollowees(String personUuid);
|
||||
}
|
||||
|
||||
@@ -6,4 +6,6 @@ import java.util.Collection;
|
||||
|
||||
public interface PostService extends CrudService<Post, Long> {
|
||||
Collection<Post> readByAuthorId(String authorUuid);
|
||||
|
||||
Collection<Post> readByPersonFollowees(String personUuid);
|
||||
}
|
||||
|
||||
@@ -24,4 +24,9 @@ public class PostServiceImpl extends CrudServiceImpl<Post, Long> implements Post
|
||||
public Collection<Post> readByAuthorId(String authorId) {
|
||||
return postRepository.findByAuthorUuid(authorId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Post> readByPersonFollowees(String personUuid) {
|
||||
return postRepository.findByPersonFollowees(personUuid);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,14 +72,14 @@ public abstract class DemoDataDbTest {
|
||||
new Person()
|
||||
.setUsername("person2")
|
||||
.setFullName("Person 2")
|
||||
.setPassword(passwordEncoder.encode(person2Password)));
|
||||
.setPassword(passwordEncoder.encode(person2Password)).setFollowing(List.of(person1)));
|
||||
person2Auth = new TokenResponse(tokenService.generateToken(person2.getUuid()));
|
||||
person3 = personRepository.save(
|
||||
new Person()
|
||||
.setUsername("person3")
|
||||
.setFullName("Person 3")
|
||||
.setPassword(passwordEncoder.encode(person3Password))
|
||||
.setFollowing(List.of(person2)));
|
||||
.setFollowing(List.of(person2, person1)));
|
||||
person3Auth = new TokenResponse(tokenService.generateToken(person3.getUuid()));
|
||||
|
||||
post1 = postRepository.save(new Post().setAuthor(person1).setText("post 1"));
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.usatiuk.tjv.y.server.controller;
|
||||
|
||||
import com.usatiuk.tjv.y.server.dto.PersonSignupRequest;
|
||||
import com.usatiuk.tjv.y.server.dto.PersonTo;
|
||||
import com.usatiuk.tjv.y.server.dto.converters.PersonMapper;
|
||||
import com.usatiuk.tjv.y.server.repository.PersonRepository;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -10,6 +11,9 @@ import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class PersonControllerTest extends DemoDataDbTest {
|
||||
@Autowired
|
||||
private PersonRepository personRepository;
|
||||
@@ -59,12 +63,24 @@ public class PersonControllerTest extends DemoDataDbTest {
|
||||
|
||||
Assertions.assertEquals(1, personToResponse.length);
|
||||
Assertions.assertEquals(personToResponse[0].fullName(), person3.getFullName());
|
||||
|
||||
response = restTemplate.exchange(addr + "/person/followers",
|
||||
HttpMethod.GET, new HttpEntity<>(createAuthHeaders(person1Auth)), PersonTo[].class);
|
||||
|
||||
Assertions.assertNotNull(response);
|
||||
Assertions.assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
|
||||
personToResponse = response.getBody();
|
||||
Assertions.assertNotNull(personToResponse);
|
||||
|
||||
Assertions.assertEquals(2, personToResponse.length);
|
||||
Assertions.assertIterableEquals(Arrays.asList(personToResponse), List.of(PersonMapper.makeDto(person2), PersonMapper.makeDto(person3)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldGetFollowees() {
|
||||
var response = restTemplate.exchange(addr + "/person/following",
|
||||
HttpMethod.GET, new HttpEntity<>(createAuthHeaders(person3Auth)), PersonTo[].class);
|
||||
HttpMethod.GET, new HttpEntity<>(createAuthHeaders(person2Auth)), PersonTo[].class);
|
||||
|
||||
Assertions.assertNotNull(response);
|
||||
Assertions.assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
@@ -73,8 +89,19 @@ public class PersonControllerTest extends DemoDataDbTest {
|
||||
Assertions.assertNotNull(personToResponse);
|
||||
|
||||
Assertions.assertEquals(1, personToResponse.length);
|
||||
Assertions.assertEquals(personToResponse[0].fullName(), person2.getFullName());
|
||||
Assertions.assertEquals(personToResponse[0].fullName(), person1.getFullName());
|
||||
|
||||
response = restTemplate.exchange(addr + "/person/following",
|
||||
HttpMethod.GET, new HttpEntity<>(createAuthHeaders(person3Auth)), PersonTo[].class);
|
||||
|
||||
Assertions.assertNotNull(response);
|
||||
Assertions.assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
|
||||
personToResponse = response.getBody();
|
||||
Assertions.assertNotNull(personToResponse);
|
||||
|
||||
Assertions.assertEquals(2, personToResponse.length);
|
||||
Assertions.assertIterableEquals(Arrays.asList(personToResponse), List.of(PersonMapper.makeDto(person2), PersonMapper.makeDto(person1)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class PostControllerTest extends DemoDataDbTest {
|
||||
@Autowired
|
||||
@@ -72,4 +73,16 @@ public class PostControllerTest extends DemoDataDbTest {
|
||||
Assertions.assertIterableEquals(Arrays.asList(parsedResponse), repoResponse.stream().map(PostMapper::makeDto).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldGetPostsByFollowees() {
|
||||
var response = restTemplate.exchange(addr + "/post/following",
|
||||
HttpMethod.GET, new HttpEntity<>(createAuthHeaders(person3Auth)), PostTo[].class);
|
||||
|
||||
Assertions.assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
var parsedResponse = response.getBody();
|
||||
|
||||
Assertions.assertNotNull(parsedResponse);
|
||||
Assertions.assertEquals(2, parsedResponse.length);
|
||||
Assertions.assertIterableEquals(Arrays.asList(parsedResponse), List.of(PostMapper.makeDto(post1), PostMapper.makeDto(post2)));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user