This commit is contained in:
parent
e634fbd1aa
commit
1861bdfe74
8
2-Application/src/main/java/link/LinkIdGenerator.java
Normal file
8
2-Application/src/main/java/link/LinkIdGenerator.java
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
package link;
|
||||||
|
|
||||||
|
import category.CategoryId;
|
||||||
|
|
||||||
|
public interface LinkIdGenerator {
|
||||||
|
|
||||||
|
LinkId generateId();
|
||||||
|
}
|
44
2-Application/src/main/java/link/LinkUseCase.java
Normal file
44
2-Application/src/main/java/link/LinkUseCase.java
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
package link;
|
||||||
|
|
||||||
|
import category.CategoryName;
|
||||||
|
import category.CategoryRepository;
|
||||||
|
import tag.TaggingUseCase;
|
||||||
|
import user.Username;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class LinkUseCase {
|
||||||
|
|
||||||
|
private final LinkRepository linkRepository;
|
||||||
|
private final CategoryRepository categoryRepository;
|
||||||
|
private final TaggingUseCase taggingUseCase;
|
||||||
|
private final LinkIdGenerator linkIdGenerator;
|
||||||
|
|
||||||
|
public LinkUseCase(LinkRepository linkRepository,
|
||||||
|
CategoryRepository categoryRepository,
|
||||||
|
TaggingUseCase taggingUseCase,
|
||||||
|
LinkIdGenerator linkIdGenerator) {
|
||||||
|
this.linkRepository = linkRepository;
|
||||||
|
this.categoryRepository = categoryRepository;
|
||||||
|
this.taggingUseCase = taggingUseCase;
|
||||||
|
this.linkIdGenerator = linkIdGenerator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addLink(LinkUrl url, Set<CategoryName> categoryNames, Username creator) {
|
||||||
|
|
||||||
|
OnlineCheck.of(url);
|
||||||
|
|
||||||
|
var categoryIds = categoryNames.stream().map(categoryRepository::getIdByName).collect(Collectors.toSet());
|
||||||
|
|
||||||
|
var tags = taggingUseCase.getTagsFor(url);
|
||||||
|
|
||||||
|
var id = linkIdGenerator.generateId();
|
||||||
|
|
||||||
|
var link = new Link(id, creator, url, categoryIds, tags);
|
||||||
|
|
||||||
|
linkRepository.add(link);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,9 +1,10 @@
|
||||||
package category;
|
package category;
|
||||||
|
|
||||||
import abstraction.CustomSet;
|
import abstraction.CustomSet;
|
||||||
import abstraction.PersistenceAdapter;
|
|
||||||
import abstraction.CustomSetPersistenceDecorator;
|
import abstraction.CustomSetPersistenceDecorator;
|
||||||
|
import abstraction.PersistenceAdapter;
|
||||||
import exeptions.CategoryAlreadyExists;
|
import exeptions.CategoryAlreadyExists;
|
||||||
|
import exeptions.CategroyDoesNotExist;
|
||||||
import exeptions.NonUniqueId;
|
import exeptions.NonUniqueId;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
@ -11,7 +12,6 @@ import java.util.Set;
|
||||||
|
|
||||||
public class CategoryRepository {
|
public class CategoryRepository {
|
||||||
|
|
||||||
|
|
||||||
final private CustomSet<Category> categories;
|
final private CustomSet<Category> categories;
|
||||||
|
|
||||||
public CategoryRepository(PersistenceAdapter<Category> categoryPersistenceAdapter) {
|
public CategoryRepository(PersistenceAdapter<Category> categoryPersistenceAdapter) {
|
||||||
|
@ -22,6 +22,14 @@ public class CategoryRepository {
|
||||||
return categories.stream().filter(category -> category.getName().equals(name)).findFirst();
|
return categories.stream().filter(category -> category.getName().equals(name)).findFirst();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CategoryId getIdByName(CategoryName name) {
|
||||||
|
|
||||||
|
return getByName(name)
|
||||||
|
.orElseThrow(() -> new CategroyDoesNotExist("A Category with name " + name + " does not exits. You must create it first."))
|
||||||
|
.getId();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public Optional<Category> getById(CategoryId id) {
|
public Optional<Category> getById(CategoryId id) {
|
||||||
return categories.stream().filter(category -> category.getId().equals(id)).findFirst();
|
return categories.stream().filter(category -> category.getId().equals(id)).findFirst();
|
||||||
}
|
}
|
||||||
|
@ -37,6 +45,7 @@ public class CategoryRepository {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check for duplicates with the domain rules
|
* Check for duplicates with the domain rules
|
||||||
|
*
|
||||||
* @param category
|
* @param category
|
||||||
*/
|
*/
|
||||||
private void checkDuplicates(Category category) {
|
private void checkDuplicates(Category category) {
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
package exeptions;
|
||||||
|
|
||||||
|
public class CategroyDoesNotExist extends RuntimeException {
|
||||||
|
|
||||||
|
public CategroyDoesNotExist(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
Reference in a new issue