Route pour supprimé un post update

This commit is contained in:
Guams 2024-12-28 01:30:36 +01:00
parent 559446c64d
commit 02ea44d57d
6 changed files with 24 additions and 15 deletions

View File

@ -2,9 +2,9 @@ package com.guams.review.controller;
import com.guams.review.configuration.JwtTokenUtil;
import com.guams.review.exception.AlreadyExistsException;
import com.guams.review.exception.ForbiddenExecption;
import com.guams.review.exception.InvalidNameOrPasswordException;
import com.guams.review.exception.NotFoundException;
import com.guams.review.exception.ForbiddenExecption;
import com.guams.review.model.AuthorRepository;
import com.guams.review.model.dao.Author;
import com.guams.review.model.dao.AuthorToken;
@ -33,7 +33,6 @@ import java.util.UUID;
@RequiredArgsConstructor
public class AuthorController {
private final PasswordEncoder passwordEncoder;
private final AuthenticationManager authenticationManager;
private final AuthorService authorService;

View File

@ -1,7 +1,7 @@
package com.guams.review.controller;
import com.guams.review.exception.NotFoundException;
import com.guams.review.exception.ForbiddenExecption;
import com.guams.review.exception.NotFoundException;
import com.guams.review.model.AuthorRepository;
import com.guams.review.model.dao.Author;
import com.guams.review.model.dao.Post;
@ -17,7 +17,8 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.time.LocalDate;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.List;
@RequiredArgsConstructor
@ -58,7 +59,7 @@ public class PostController {
}
@PutMapping(value = "{id}/illustration", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
public void updateIllustration(@PathVariable Long id, @RequestPart MultipartFile illustration, Authentication authentication) throws IOException {
public void updateIllustration(@PathVariable Long id, @RequestPart("illustration") MultipartFile illustration, Authentication authentication) throws IOException {
if (authentication == null) {
throw new ForbiddenExecption("You have to login to do that");
}
@ -77,7 +78,7 @@ public class PostController {
if (authentication == null) {
throw new ForbiddenExecption("You have to login to do that");
}
return new ResponseEntity<>(postService.insert(postToCreate.setPublicationDate(LocalDate.now())), HttpStatus.CREATED);
return new ResponseEntity<>(postService.insert(postToCreate.setPublicationDate(Timestamp.from(Instant.now()))), HttpStatus.CREATED);
}
@DeleteMapping("{id}")
@ -88,7 +89,7 @@ public class PostController {
Author authenticatedAuthor = authorRepository.findByName(authentication.getName());
if (authorService.listPublicationOfAuthor(authenticatedAuthor.getId()).stream().map(Post::getId).toList().contains(id)) {
Post postToDelete = postService.findById(id).orElseThrow(() -> new NotFoundException("Post not found"));
postService.delete(postToDelete.getId());
postService.delete(authenticatedAuthor.getId(), postToDelete.getId());
} else {
throw new ForbiddenExecption("You do not have permission to delete this post");
}

View File

@ -7,7 +7,7 @@ import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;
import java.time.LocalDate;
import java.sql.Timestamp;
@Getter
@Setter
@ -27,6 +27,6 @@ public class Post {
@Column("category")
String category;
@Column("publication_date")
LocalDate publicationDate;
Timestamp publicationDate;
}

View File

@ -1,19 +1,23 @@
package com.guams.review.service;
import com.guams.review.model.AuthorRepository;
import com.guams.review.model.dao.Post;
import com.guams.review.model.PostRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@Service
@RequiredArgsConstructor
public class PostService
{
private final PostRepository postRepository;
private final AuthorRepository authorRepository;
public List<Post> list() {
return postRepository.findAll();
@ -27,8 +31,10 @@ public class PostService
return postRepository.save(post);
}
public void delete(Long id) {
postRepository.deleteById(id);
@Transactional
public void delete(UUID authorId, Long postId) {
authorRepository.deletePublication(authorId, postId);
postRepository.deleteById(postId);
}
}

View File

@ -3,4 +3,7 @@ spring.security.oauth2.client.registration.github.client-secret=c660f476763404f4
spring.application.name=reView
spring.datasource.url=jdbc:postgresql://localhost:5432/reviewDB
spring.datasource.username=postgres
spring.datasource.password=Azerty1234
spring.datasource.password=Azerty1234
spring.servlet.multipart.enabled=true
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB

View File

@ -8,8 +8,8 @@ create type author_role as enum ('READER', 'WRITER', 'ADMIN');
create table author
(
id uuid default gen_random_uuid() primary key,
name varchar(255) unique not null,
password text not null,
name varchar(255) unique not null,
password text not null,
profile_picture bytea,
role author_role default 'READER' not null
);
@ -22,7 +22,7 @@ create table post
title varchar(50) not null,
body text not null,
category varchar(50) not null,
publication_date date not null
publication_date timestamp not null
);
create table publication