Refactor Category and Link Repository to use generic the persistence adapter
continuous-integration/drone/push Build is passing Details

This commit is contained in:
qvalentin 2022-04-22 10:31:59 +02:00
parent a1a67b3a7c
commit e634fbd1aa
Signed by: qvalentin
GPG Key ID: C979FA1EAFCABF1C
8 changed files with 75 additions and 19 deletions

View File

@ -1,17 +1,22 @@
package persistence; package persistence;
import abstraction.PersistenceAdapter;
import category.Category; import category.Category;
import category.CategoryPersistenceAdapter;
import java.util.Set; import java.util.Set;
public class CSVCategoryPersistenceAdapter implements CategoryPersistenceAdapter { public class CSVCategoryPersistenceAdapter implements PersistenceAdapter<Category> {
@Override @Override
public Set<Category> getAll() { public Set<Category> getAll() {
return null; return null;
} }
@Override
public Boolean remove(Category value) {
return null;
}
@Override @Override
public Boolean add(Category category) { public Boolean add(Category category) {
return null; return null;

View File

@ -1,6 +1,5 @@
package category; package category;
public interface CategoryIdGenerator public interface CategoryIdGenerator {
{
CategoryId generateId(); CategoryId generateId();
} }

View File

@ -1,5 +1,8 @@
package category; package category;
import abstraction.CustomSet;
import abstraction.PersistenceAdapter;
import abstraction.CustomSetPersistenceDecorator;
import exeptions.CategoryAlreadyExists; import exeptions.CategoryAlreadyExists;
import exeptions.NonUniqueId; import exeptions.NonUniqueId;
@ -8,12 +11,11 @@ import java.util.Set;
public class CategoryRepository { public class CategoryRepository {
final private CategoryPersistenceAdapter categoryPersistenceAdapter;
final private Set<Category> categories;
public CategoryRepository(CategoryPersistenceAdapter categoryPersistenceAdapter) { final private CustomSet<Category> categories;
this.categoryPersistenceAdapter = categoryPersistenceAdapter;
this.categories = categoryPersistenceAdapter.getAll(); public CategoryRepository(PersistenceAdapter<Category> categoryPersistenceAdapter) {
this.categories = new CustomSetPersistenceDecorator<Category>(categoryPersistenceAdapter);
} }
public Optional<Category> getByName(CategoryName name) { public Optional<Category> getByName(CategoryName name) {
@ -25,16 +27,18 @@ public class CategoryRepository {
} }
public Set<Category> getAll(){ public Set<Category> getAll(){
return categories; return categories.getSet();
} }
public void add(Category category) { public void add(Category category) {
checkDuplicates(category); checkDuplicates(category);
categories.add(category); categories.add(category);
categoryPersistenceAdapter.add(category);
} }
/**
* Check for duplicates with the domain rules
* @param category
*/
private void checkDuplicates(Category category) { private void checkDuplicates(Category category) {
if (this.getByName(category.getName()).isPresent()) { if (this.getByName(category.getName()).isPresent()) {
throw new CategoryAlreadyExists("A category with the name " + category.getName() + " already exits."); throw new CategoryAlreadyExists("A category with the name " + category.getName() + " already exits.");

View File

@ -0,0 +1,8 @@
package exeptions;
public class ElementAlreadyInSet extends RuntimeException {
public ElementAlreadyInSet(String message) {
super(message);
}
}

View File

@ -0,0 +1,8 @@
package exeptions;
public class ElementNotInSet extends RuntimeException {
public ElementNotInSet(String message) {
super(message);
}
}

View File

@ -0,0 +1,8 @@
package exeptions;
public class LinkAlreadyExists extends RuntimeException {
public LinkAlreadyExists(String message) {
super(message);
}
}

View File

@ -0,0 +1,8 @@
package exeptions;
public class LinkDoesNotExist extends RuntimeException {
public LinkDoesNotExist(String message) {
super(message);
}
}

View File

@ -1,18 +1,19 @@
package link; package link;
import abstraction.CustomSet;
import abstraction.CustomSetPersistenceDecorator;
import abstraction.PersistenceAdapter;
import exeptions.LinkAlreadyExists; import exeptions.LinkAlreadyExists;
import exeptions.LinkDoesNotExist;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
public class LinkRepository { public class LinkRepository {
final private LinkPersistenceAdapter linkPersistenceAdapter; final private CustomSet<Link> links;
final private Set<Link> links;
public LinkRepository(LinkPersistenceAdapter linkPersistenceAdapter) { public LinkRepository(PersistenceAdapter<Link> linkPersistenceAdapter) {
this.linkPersistenceAdapter = linkPersistenceAdapter; this.links = new CustomSetPersistenceDecorator(linkPersistenceAdapter);
this.links = linkPersistenceAdapter.getAll();
} }
public Optional<Link> getById(LinkId id) { public Optional<Link> getById(LinkId id) {
@ -23,10 +24,25 @@ public class LinkRepository {
return links.stream().filter(link -> link.getUrl().equals(url)).findFirst(); return links.stream().filter(link -> link.getUrl().equals(url)).findFirst();
} }
public void removeById(LinkId id) {
getById(id).ifPresentOrElse(this::remove, () -> {
throw new LinkDoesNotExist("Tried removing a link with id " + id + " but it does not exist.");
});
}
public void removeByUrl(LinkUrl url) {
getByUrl(url).ifPresentOrElse(this::remove, () -> {
throw new LinkDoesNotExist("Tried removing a link with url " + url + " but it does not exist.");
});
}
public void add(Link link) { public void add(Link link) {
checkDuplicates(link); checkDuplicates(link);
links.add(link);
}
private void remove(Link link) {
links.remove(link);
} }
private void checkDuplicates(Link newLink) { private void checkDuplicates(Link newLink) {