Refactor Category and Link Repository to use generic the persistence adapter
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
a1a67b3a7c
commit
e634fbd1aa
|
@ -1,17 +1,22 @@
|
|||
package persistence;
|
||||
|
||||
import abstraction.PersistenceAdapter;
|
||||
import category.Category;
|
||||
import category.CategoryPersistenceAdapter;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class CSVCategoryPersistenceAdapter implements CategoryPersistenceAdapter {
|
||||
public class CSVCategoryPersistenceAdapter implements PersistenceAdapter<Category> {
|
||||
|
||||
@Override
|
||||
public Set<Category> getAll() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean remove(Category value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean add(Category category) {
|
||||
return null;
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package category;
|
||||
|
||||
public interface CategoryIdGenerator
|
||||
{
|
||||
public interface CategoryIdGenerator {
|
||||
CategoryId generateId();
|
||||
}
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
package category;
|
||||
|
||||
import abstraction.CustomSet;
|
||||
import abstraction.PersistenceAdapter;
|
||||
import abstraction.CustomSetPersistenceDecorator;
|
||||
import exeptions.CategoryAlreadyExists;
|
||||
import exeptions.NonUniqueId;
|
||||
|
||||
|
@ -8,12 +11,11 @@ import java.util.Set;
|
|||
|
||||
public class CategoryRepository {
|
||||
|
||||
final private CategoryPersistenceAdapter categoryPersistenceAdapter;
|
||||
final private Set<Category> categories;
|
||||
|
||||
public CategoryRepository(CategoryPersistenceAdapter categoryPersistenceAdapter) {
|
||||
this.categoryPersistenceAdapter = categoryPersistenceAdapter;
|
||||
this.categories = categoryPersistenceAdapter.getAll();
|
||||
final private CustomSet<Category> categories;
|
||||
|
||||
public CategoryRepository(PersistenceAdapter<Category> categoryPersistenceAdapter) {
|
||||
this.categories = new CustomSetPersistenceDecorator<Category>(categoryPersistenceAdapter);
|
||||
}
|
||||
|
||||
public Optional<Category> getByName(CategoryName name) {
|
||||
|
@ -25,16 +27,18 @@ public class CategoryRepository {
|
|||
}
|
||||
|
||||
public Set<Category> getAll(){
|
||||
return categories;
|
||||
return categories.getSet();
|
||||
}
|
||||
|
||||
public void add(Category category) {
|
||||
checkDuplicates(category);
|
||||
|
||||
categories.add(category);
|
||||
categoryPersistenceAdapter.add(category);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for duplicates with the domain rules
|
||||
* @param category
|
||||
*/
|
||||
private void checkDuplicates(Category category) {
|
||||
if (this.getByName(category.getName()).isPresent()) {
|
||||
throw new CategoryAlreadyExists("A category with the name " + category.getName() + " already exits.");
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
package exeptions;
|
||||
|
||||
public class ElementAlreadyInSet extends RuntimeException {
|
||||
|
||||
public ElementAlreadyInSet(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
8
3-Domain/src/main/java/exeptions/ElementNotInSet.java
Normal file
8
3-Domain/src/main/java/exeptions/ElementNotInSet.java
Normal file
|
@ -0,0 +1,8 @@
|
|||
package exeptions;
|
||||
|
||||
public class ElementNotInSet extends RuntimeException {
|
||||
|
||||
public ElementNotInSet(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
8
3-Domain/src/main/java/exeptions/LinkAlreadyExists.java
Normal file
8
3-Domain/src/main/java/exeptions/LinkAlreadyExists.java
Normal file
|
@ -0,0 +1,8 @@
|
|||
package exeptions;
|
||||
|
||||
public class LinkAlreadyExists extends RuntimeException {
|
||||
|
||||
public LinkAlreadyExists(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
8
3-Domain/src/main/java/exeptions/LinkDoesNotExist.java
Normal file
8
3-Domain/src/main/java/exeptions/LinkDoesNotExist.java
Normal file
|
@ -0,0 +1,8 @@
|
|||
package exeptions;
|
||||
|
||||
public class LinkDoesNotExist extends RuntimeException {
|
||||
|
||||
public LinkDoesNotExist(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
|
@ -1,18 +1,19 @@
|
|||
package link;
|
||||
|
||||
import abstraction.CustomSet;
|
||||
import abstraction.CustomSetPersistenceDecorator;
|
||||
import abstraction.PersistenceAdapter;
|
||||
import exeptions.LinkAlreadyExists;
|
||||
import exeptions.LinkDoesNotExist;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
public class LinkRepository {
|
||||
|
||||
final private LinkPersistenceAdapter linkPersistenceAdapter;
|
||||
final private Set<Link> links;
|
||||
final private CustomSet<Link> links;
|
||||
|
||||
public LinkRepository(LinkPersistenceAdapter linkPersistenceAdapter) {
|
||||
this.linkPersistenceAdapter = linkPersistenceAdapter;
|
||||
this.links = linkPersistenceAdapter.getAll();
|
||||
public LinkRepository(PersistenceAdapter<Link> linkPersistenceAdapter) {
|
||||
this.links = new CustomSetPersistenceDecorator(linkPersistenceAdapter);
|
||||
}
|
||||
|
||||
public Optional<Link> getById(LinkId id) {
|
||||
|
@ -23,10 +24,25 @@ public class LinkRepository {
|
|||
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) {
|
||||
checkDuplicates(link);
|
||||
links.add(link);
|
||||
}
|
||||
|
||||
|
||||
private void remove(Link link) {
|
||||
links.remove(link);
|
||||
}
|
||||
|
||||
private void checkDuplicates(Link newLink) {
|
||||
|
|
Reference in a new issue