simple add remove follower

This commit is contained in:
Stepan Usatiuk
2023-12-17 12:01:21 +01:00
parent 871c6950c9
commit d752d20d92
22 changed files with 246 additions and 34 deletions

View File

@@ -7,6 +7,7 @@ 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.*;
@@ -79,4 +80,16 @@ public class PersonController {
return personService.getFollowing(principal.getName()).stream().map(PersonMapper::makeDto);
}
@PutMapping(path = "/following/{uuid}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void addFollowing(Principal principal, @PathVariable String uuid) throws UserNotFoundException {
personService.addFollower(principal.getName(), uuid);
}
@DeleteMapping(path = "/following/{uuid}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteFollowing(Principal principal, @PathVariable String uuid) throws UserNotFoundException {
personService.removeFollower(principal.getName(), uuid);
}
}

View File

@@ -36,8 +36,16 @@ public class PostController {
return PostMapper.makeDto(postService.create(post));
}
@GetMapping
public Stream<PostTo> readAllByAuthor(@RequestParam Optional<String> author) {
@GetMapping(path = "/by-author-uuid")
public Stream<PostTo> readAllByAuthorUuid(@RequestParam Optional<String> author) {
if (author.isPresent())
return postService.readByAuthorId(author.get()).stream().map(PostMapper::makeDto);
else
throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
}
@GetMapping(path = "/by-author-username")
public Stream<PostTo> readAllByAuthorUsername(@RequestParam Optional<String> author) {
if (author.isPresent())
return postService.readByAuthorId(author.get()).stream().map(PostMapper::makeDto);
else

View File

@@ -12,6 +12,8 @@ import java.util.Collection;
public interface PostRepository extends PagingAndSortingRepository<Post, Long>, CrudRepository<Post, Long> {
Collection<Post> findByAuthorUuid(String authorUuid);
Collection<Post> findByAuthorUsername(String authorUsername);
@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)")

View File

@@ -17,4 +17,8 @@ public interface PersonService extends CrudService<Person, String> {
Collection<Person> getFollowers(String uuid) throws UserNotFoundException;
Collection<Person> getFollowing(String uuid) throws UserNotFoundException;
void addFollower(String follower, String followee) throws UserNotFoundException;
void removeFollower(String follower, String followee) throws UserNotFoundException;
}

View File

@@ -4,6 +4,7 @@ import com.usatiuk.tjv.y.server.entity.Person;
import com.usatiuk.tjv.y.server.repository.PersonRepository;
import com.usatiuk.tjv.y.server.service.exceptions.UserAlreadyExistsException;
import com.usatiuk.tjv.y.server.service.exceptions.UserNotFoundException;
import jakarta.persistence.EntityManager;
import org.springframework.data.repository.CrudRepository;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
@@ -15,11 +16,13 @@ import java.util.Optional;
public class PersonServiceImpl extends CrudServiceImpl<Person, String> implements PersonService {
private final PersonRepository personRepository;
private final PasswordEncoder passwordEncoder;
private final EntityManager entityManager;
public PersonServiceImpl(PersonRepository personRepository,
PasswordEncoder passwordEncoder) {
PasswordEncoder passwordEncoder, EntityManager entityManager) {
this.personRepository = personRepository;
this.passwordEncoder = passwordEncoder;
this.entityManager = entityManager;
}
@Override
@@ -59,4 +62,18 @@ public class PersonServiceImpl extends CrudServiceImpl<Person, String> implement
public Collection<Person> getFollowing(String uuid) throws UserNotFoundException {
return personRepository.findById(uuid).orElseThrow(UserNotFoundException::new).getFollowing();
}
@Override
public void addFollower(String follower, String followee) throws UserNotFoundException {
var person = personRepository.findById(follower).orElseThrow(UserNotFoundException::new);
person.getFollowing().add(entityManager.getReference(Person.class, followee));
personRepository.save(person);
}
@Override
public void removeFollower(String follower, String followee) throws UserNotFoundException {
var person = personRepository.findById(follower).orElseThrow(UserNotFoundException::new);
person.getFollowing().remove(entityManager.getReference(Person.class, followee));
personRepository.save(person);
}
}

View File

@@ -7,5 +7,7 @@ import java.util.Collection;
public interface PostService extends CrudService<Post, Long> {
Collection<Post> readByAuthorId(String authorUuid);
Collection<Post> readByAuthorUsername(String authorUsername);
Collection<Post> readByPersonFollowees(String personUuid);
}

View File

@@ -25,6 +25,11 @@ public class PostServiceImpl extends CrudServiceImpl<Post, Long> implements Post
return postRepository.findByAuthorUuid(authorId);
}
@Override
public Collection<Post> readByAuthorUsername(String authorUsername) {
return postRepository.findByAuthorUsername(authorUsername);
}
@Override
public Collection<Post> readByPersonFollowees(String personUuid) {
return postRepository.findByPersonFollowees(personUuid);

View File

@@ -119,4 +119,25 @@ public class PersonControllerTest extends DemoDataDbTest {
Assertions.assertIterableEquals(Arrays.asList(personToResponse), List.of(PersonMapper.makeDto(person2), PersonMapper.makeDto(person1)));
}
@Test
void shouldAddFollowing() {
var response = restTemplate.exchange(addr + "/person/following/" + person3.getUuid(),
HttpMethod.PUT, new HttpEntity<>(createAuthHeaders(person1Auth)), Object.class);
Assertions.assertNotNull(response);
Assertions.assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
var response2 = restTemplate.exchange(addr + "/person/following",
HttpMethod.GET, new HttpEntity<>(createAuthHeaders(person1Auth)), PersonTo[].class);
Assertions.assertNotNull(response2);
Assertions.assertEquals(HttpStatus.OK, response2.getStatusCode());
var personToResponse = response2.getBody();
Assertions.assertNotNull(personToResponse);
Assertions.assertEquals(1, personToResponse.length);
Assertions.assertIterableEquals(Arrays.asList(personToResponse), List.of(PersonMapper.makeDto(person3)));
}
}

View File

@@ -60,7 +60,7 @@ public class PostControllerTest extends DemoDataDbTest {
@Test
void shouldGetByAuthor() {
var response = restTemplate.exchange(addr + "/post?author=" + person1.getUuid(), HttpMethod.GET,
var response = restTemplate.exchange(addr + "/post/by-author-uuid?author=" + person1.getUuid(), HttpMethod.GET,
HttpEntity.EMPTY, PostTo[].class);
Assertions.assertEquals(HttpStatus.OK, response.getStatusCode());