Skip to content

Commit

Permalink
Update articles endpoint to allow updates only when the url is not ye…
Browse files Browse the repository at this point in the history
…t used and delete for the AWS support
  • Loading branch information
USER3411 committed Jun 2, 2021
1 parent c083410 commit a4bd30c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public Collection<Article> findAll() {
@PutMapping
@RolesAllowed(Role.ADMIN)
public Article save(@RequestBody Article article) {
return articleService.save(article);
return articleService.saveIfNotExists(article);
}

@DeleteMapping(path = "/{id}")
@RolesAllowed(Role.ADMIN)
public void delete(@PathVariable Long id){
articleService.deleteById(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface ArticleRepository extends JpaRepository<Article, Long> {
Optional<Article> findByUrl(String url);
}
10 changes: 8 additions & 2 deletions src/main/java/dj/personal/website/article/ArticleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.Collection;
import java.util.Comparator;
import java.util.Optional;

import static java.util.stream.Collectors.toList;

Expand All @@ -24,7 +25,12 @@ public Collection<Article> findAll() {
.collect(toList());
}

public Article save(Article article) {
return articleRepository.save(article);
public Article saveIfNotExists(Article article) {
return articleRepository.findByUrl(article.getUrl())
.orElseGet(() -> articleRepository.save(article));
}

public void deleteById(Long id) {
articleRepository.deleteById(id);
}
}
29 changes: 0 additions & 29 deletions src/test/java/dj/personal/website/book/BookRunnerTest.java

This file was deleted.

0 comments on commit a4bd30c

Please sign in to comment.