deleting posts

This commit is contained in:
Stepan Usatiuk
2023-12-16 21:07:39 +01:00
parent d98906a23f
commit f556f67db6
9 changed files with 105 additions and 10 deletions

View File

@@ -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);
}
}

View File

@@ -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;
}
}