update posts

This commit is contained in:
Stepan Usatiuk
2023-12-17 15:04:19 +01:00
parent 8940b3d6c6
commit 18ac8ded71
10 changed files with 109 additions and 31 deletions

View File

@@ -64,6 +64,15 @@ public class PostController {
return PostMapper.makeDto(post.get());
}
@PatchMapping(path = "/{id}")
public PostTo update(Principal principal, @PathVariable long id, @RequestBody PostCreateTo postCreateTo) {
var post = postService.readById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
if (!Objects.equals(post.getAuthor().getUuid(), principal.getName()))
throw new ResponseStatusException(HttpStatus.FORBIDDEN);
post.setText(postCreateTo.text());
postService.update(post);
return PostMapper.makeDto(post);
}
@DeleteMapping(path = "/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)

View File

@@ -12,7 +12,7 @@ public interface CrudService<T extends EntityWithId<ID>, ID extends Serializable
Iterable<T> readAll();
void update(ID id, T e);
void update(T e);
void deleteById(ID id);
}

View File

@@ -27,8 +27,8 @@ public abstract class CrudServiceImpl<T extends EntityWithId<ID>, ID extends Ser
}
@Override
public void update(ID id, T e) {
public void update(T e) {
getRepository().save(e);
}
@Override