les publications marchent, plus qu'à faire l'authent

This commit is contained in:
Guams 2024-12-09 22:36:30 +01:00
parent fa920bd840
commit 38b0f46f7f
11 changed files with 82 additions and 36 deletions

View File

@ -1,7 +1,8 @@
package com.guams.review.controller;
import com.guams.review.exception.NotFoundException;
import com.guams.review.modele.dao.Author;
import com.guams.review.model.dao.Author;
import com.guams.review.model.dao.Post;
import com.guams.review.service.AuthorService;
import com.guams.review.service.mapper.Mapper;
import com.guams.review.service.mapper.ReturnableAuthor;
@ -58,4 +59,15 @@ public class AuthorController {
authorService.delete(authorToDelete);
}
@PutMapping("/{id}/posts")
public void updateUserPosts(@PathVariable("id") UUID authorId, @RequestBody List<Long> postIds) {
Author author = authorService.findById(authorId).orElseThrow(() -> new NotFoundException("Author not found"));
authorService.insertPublications(author.getId(), postIds);
}
@GetMapping("/{id}/posts")
public List<Post> getUserPosts(@PathVariable UUID id) {
Author author = authorService.findById(id).orElseThrow(() -> new NotFoundException("Author not found"));
return authorService.listPublicationOfAuthor(author.getId());
}
}

View File

@ -1,7 +1,7 @@
package com.guams.review.controller;
import com.guams.review.exception.NotFoundException;
import com.guams.review.modele.dao.Post;
import com.guams.review.model.dao.Post;
import com.guams.review.service.PostService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;

View File

@ -0,0 +1,24 @@
package com.guams.review.model;
import com.guams.review.model.dao.Author;
import org.springframework.data.jdbc.repository.query.Modifying;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.UUID;
@Repository
public interface AuthorRepository extends CrudRepository<Author, UUID> {
@Modifying
@Query("insert into publication (author_fk, post_fk) values (:authorId, :postId)")
void insertPublication(UUID authorId, Long postId);
@Modifying
@Query("delete from publication where author_fk=:authorId and post_fk=:postId")
void deletePublication(UUID authorId, Long postId);
List<Author> findAll();
}

View File

@ -0,0 +1,19 @@
package com.guams.review.model;
import com.guams.review.model.dao.Post;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.UUID;
@Repository
public interface PostRepository extends CrudRepository<Post, Long> {
@Query("select id, description, illustration, title, body, category, publication_date " +
"from post join publication on post.id=publication.post_fk where publication.author_fk=:authorId")
List<Post> findPostsOfAuthor(UUID authorId);
List<Post> findAll();
}

View File

@ -1,4 +1,4 @@
package com.guams.review.modele.dao;
package com.guams.review.model.dao;
import lombok.Getter;

View File

@ -1,4 +1,4 @@
package com.guams.review.modele.dao;
package com.guams.review.model.dao;
import lombok.Getter;
import lombok.Setter;

View File

@ -1,14 +0,0 @@
package com.guams.review.modele;
import com.guams.review.modele.dao.Author;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.UUID;
@Repository
public interface AuthorRepository extends CrudRepository<Author, UUID> {
List<Author> findAll();
}

View File

@ -1,12 +0,0 @@
package com.guams.review.modele;
import com.guams.review.modele.dao.Post;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface PostRepository extends CrudRepository<Post, Long> {
List<Post> findAll();
}

View File

@ -1,11 +1,14 @@
package com.guams.review.service;
import com.guams.review.modele.AuthorRepository;
import com.guams.review.modele.dao.Author;
import com.guams.review.model.AuthorRepository;
import com.guams.review.model.PostRepository;
import com.guams.review.model.dao.Author;
import com.guams.review.model.dao.Post;
import com.guams.review.service.mapper.Mapper;
import com.guams.review.service.mapper.ReturnableAuthor;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
@ -17,6 +20,7 @@ public class AuthorService
{
private final Mapper mapper;
private final AuthorRepository authorRepository;
private final PostRepository postRepository;
public List<ReturnableAuthor> list() {
return authorRepository.findAll().stream()
@ -24,6 +28,10 @@ public class AuthorService
.toList();
}
public List<Post> listPublicationOfAuthor(UUID authorId) {
return postRepository.findPostsOfAuthor(authorId);
}
public Optional<Author> findById(UUID id) {
return authorRepository.findById(id);
}
@ -32,6 +40,16 @@ public class AuthorService
return mapper.mapAuthor(authorRepository.save(author));
}
@Transactional
public void insertPublications(UUID authorId, List<Long> postIds) {
for (Long postId : postIds) {
authorRepository.deletePublication(authorId, postId);
if (postRepository.findById(postId).isPresent()) {
authorRepository.insertPublication(authorId, postId);
}
}
}
public void delete(Author author) {
authorRepository.delete(author);
}

View File

@ -1,9 +1,8 @@
package com.guams.review.service;
import com.guams.review.exception.NotFoundException;
import com.guams.review.modele.dao.Post;
import com.guams.review.modele.PostRepository;
import com.guams.review.model.dao.Post;
import com.guams.review.model.PostRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

View File

@ -1,6 +1,6 @@
package com.guams.review.service.mapper;
import com.guams.review.modele.dao.Author;
import com.guams.review.model.dao.Author;
import org.springframework.stereotype.Component;
@Component