Route pour supprimé un post update
This commit is contained in:
parent
559446c64d
commit
02ea44d57d
@ -2,9 +2,9 @@ package com.guams.review.controller;
|
|||||||
|
|
||||||
import com.guams.review.configuration.JwtTokenUtil;
|
import com.guams.review.configuration.JwtTokenUtil;
|
||||||
import com.guams.review.exception.AlreadyExistsException;
|
import com.guams.review.exception.AlreadyExistsException;
|
||||||
|
import com.guams.review.exception.ForbiddenExecption;
|
||||||
import com.guams.review.exception.InvalidNameOrPasswordException;
|
import com.guams.review.exception.InvalidNameOrPasswordException;
|
||||||
import com.guams.review.exception.NotFoundException;
|
import com.guams.review.exception.NotFoundException;
|
||||||
import com.guams.review.exception.ForbiddenExecption;
|
|
||||||
import com.guams.review.model.AuthorRepository;
|
import com.guams.review.model.AuthorRepository;
|
||||||
import com.guams.review.model.dao.Author;
|
import com.guams.review.model.dao.Author;
|
||||||
import com.guams.review.model.dao.AuthorToken;
|
import com.guams.review.model.dao.AuthorToken;
|
||||||
@ -33,7 +33,6 @@ import java.util.UUID;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class AuthorController {
|
public class AuthorController {
|
||||||
|
|
||||||
|
|
||||||
private final PasswordEncoder passwordEncoder;
|
private final PasswordEncoder passwordEncoder;
|
||||||
private final AuthenticationManager authenticationManager;
|
private final AuthenticationManager authenticationManager;
|
||||||
private final AuthorService authorService;
|
private final AuthorService authorService;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package com.guams.review.controller;
|
package com.guams.review.controller;
|
||||||
|
|
||||||
import com.guams.review.exception.NotFoundException;
|
|
||||||
import com.guams.review.exception.ForbiddenExecption;
|
import com.guams.review.exception.ForbiddenExecption;
|
||||||
|
import com.guams.review.exception.NotFoundException;
|
||||||
import com.guams.review.model.AuthorRepository;
|
import com.guams.review.model.AuthorRepository;
|
||||||
import com.guams.review.model.dao.Author;
|
import com.guams.review.model.dao.Author;
|
||||||
import com.guams.review.model.dao.Post;
|
import com.guams.review.model.dao.Post;
|
||||||
@ -17,7 +17,8 @@ import org.springframework.web.bind.annotation.*;
|
|||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.time.LocalDate;
|
import java.sql.Timestamp;
|
||||||
|
import java.time.Instant;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@ -58,7 +59,7 @@ public class PostController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping(value = "{id}/illustration", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
|
@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) {
|
if (authentication == null) {
|
||||||
throw new ForbiddenExecption("You have to login to do that");
|
throw new ForbiddenExecption("You have to login to do that");
|
||||||
}
|
}
|
||||||
@ -77,7 +78,7 @@ public class PostController {
|
|||||||
if (authentication == null) {
|
if (authentication == null) {
|
||||||
throw new ForbiddenExecption("You have to login to do that");
|
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}")
|
@DeleteMapping("{id}")
|
||||||
@ -88,7 +89,7 @@ public class PostController {
|
|||||||
Author authenticatedAuthor = authorRepository.findByName(authentication.getName());
|
Author authenticatedAuthor = authorRepository.findByName(authentication.getName());
|
||||||
if (authorService.listPublicationOfAuthor(authenticatedAuthor.getId()).stream().map(Post::getId).toList().contains(id)) {
|
if (authorService.listPublicationOfAuthor(authenticatedAuthor.getId()).stream().map(Post::getId).toList().contains(id)) {
|
||||||
Post postToDelete = postService.findById(id).orElseThrow(() -> new NotFoundException("Post not found"));
|
Post postToDelete = postService.findById(id).orElseThrow(() -> new NotFoundException("Post not found"));
|
||||||
postService.delete(postToDelete.getId());
|
postService.delete(authenticatedAuthor.getId(), postToDelete.getId());
|
||||||
} else {
|
} else {
|
||||||
throw new ForbiddenExecption("You do not have permission to delete this post");
|
throw new ForbiddenExecption("You do not have permission to delete this post");
|
||||||
}
|
}
|
||||||
|
@ -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.Column;
|
||||||
import org.springframework.data.relational.core.mapping.Table;
|
import org.springframework.data.relational.core.mapping.Table;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.sql.Timestamp;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@ -27,6 +27,6 @@ public class Post {
|
|||||||
@Column("category")
|
@Column("category")
|
||||||
String category;
|
String category;
|
||||||
@Column("publication_date")
|
@Column("publication_date")
|
||||||
LocalDate publicationDate;
|
Timestamp publicationDate;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,19 +1,23 @@
|
|||||||
package com.guams.review.service;
|
package com.guams.review.service;
|
||||||
|
|
||||||
|
|
||||||
|
import com.guams.review.model.AuthorRepository;
|
||||||
import com.guams.review.model.dao.Post;
|
import com.guams.review.model.dao.Post;
|
||||||
import com.guams.review.model.PostRepository;
|
import com.guams.review.model.PostRepository;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class PostService
|
public class PostService
|
||||||
{
|
{
|
||||||
private final PostRepository postRepository;
|
private final PostRepository postRepository;
|
||||||
|
private final AuthorRepository authorRepository;
|
||||||
|
|
||||||
public List<Post> list() {
|
public List<Post> list() {
|
||||||
return postRepository.findAll();
|
return postRepository.findAll();
|
||||||
@ -27,8 +31,10 @@ public class PostService
|
|||||||
return postRepository.save(post);
|
return postRepository.save(post);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void delete(Long id) {
|
@Transactional
|
||||||
postRepository.deleteById(id);
|
public void delete(UUID authorId, Long postId) {
|
||||||
|
authorRepository.deletePublication(authorId, postId);
|
||||||
|
postRepository.deleteById(postId);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,3 +4,6 @@ spring.application.name=reView
|
|||||||
spring.datasource.url=jdbc:postgresql://localhost:5432/reviewDB
|
spring.datasource.url=jdbc:postgresql://localhost:5432/reviewDB
|
||||||
spring.datasource.username=postgres
|
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
|
||||||
|
@ -8,8 +8,8 @@ create type author_role as enum ('READER', 'WRITER', 'ADMIN');
|
|||||||
create table author
|
create table author
|
||||||
(
|
(
|
||||||
id uuid default gen_random_uuid() primary key,
|
id uuid default gen_random_uuid() primary key,
|
||||||
name varchar(255) unique not null,
|
name varchar(255) unique not null,
|
||||||
password text not null,
|
password text not null,
|
||||||
profile_picture bytea,
|
profile_picture bytea,
|
||||||
role author_role default 'READER' not null
|
role author_role default 'READER' not null
|
||||||
);
|
);
|
||||||
@ -22,7 +22,7 @@ create table post
|
|||||||
title varchar(50) not null,
|
title varchar(50) not null,
|
||||||
body text not null,
|
body text not null,
|
||||||
category varchar(50) not null,
|
category varchar(50) not null,
|
||||||
publication_date date not null
|
publication_date timestamp not null
|
||||||
);
|
);
|
||||||
|
|
||||||
create table publication
|
create table publication
|
||||||
|
Loading…
Reference in New Issue
Block a user