mirror of
https://github.com/usatiuk/y.git
synced 2025-10-28 18:37:47 +01:00
deleting posts
This commit is contained in:
@@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@@ -55,4 +56,16 @@ public class PostController {
|
||||
return PostMapper.makeDto(post.get());
|
||||
}
|
||||
|
||||
|
||||
@DeleteMapping(path = "/{id}")
|
||||
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||
public void delete(Principal principal, @PathVariable long id) {
|
||||
var read = postService.readById(id);
|
||||
if (read.isEmpty()) return;
|
||||
if (!Objects.equals(read.get().getAuthor().getId(), principal.getName())) {
|
||||
throw new ResponseStatusException(HttpStatus.FORBIDDEN);
|
||||
}
|
||||
postService.deleteById(id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -76,7 +76,9 @@ public class WebSecurityConfig {
|
||||
@Bean
|
||||
CorsConfigurationSource corsConfigurationSource() {
|
||||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||
source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
|
||||
var config = new CorsConfiguration().applyPermitDefaultValues();
|
||||
config.setAllowedMethods(List.of("*"));
|
||||
source.registerCorsConfiguration("/**", config);
|
||||
return source;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user