This commit is contained in:
parent
fd4a2c0e83
commit
71b28222a7
8 changed files with 220 additions and 152 deletions
|
@ -6,6 +6,7 @@ import category.CategoryName;
|
|||
import category.CategoryRepository;
|
||||
import exeptions.CategroyDoesNotExist;
|
||||
import exeptions.URLIsNotReachable;
|
||||
import tag.TagName;
|
||||
import tag.TaggingUseCase;
|
||||
import user.Username;
|
||||
|
||||
|
@ -14,55 +15,60 @@ import java.util.function.Function;
|
|||
import java.util.stream.Collectors;
|
||||
|
||||
public class LinkUseCase {
|
||||
private final LinkRepository linkRepository;
|
||||
private final CategoryRepository categoryRepository;
|
||||
private final TaggingUseCase taggingUseCase;
|
||||
private final RandomLinkIdGenerator linkIdGenerator;
|
||||
|
||||
private final LinkRepository linkRepository;
|
||||
private final CategoryRepository categoryRepository;
|
||||
private final TaggingUseCase taggingUseCase;
|
||||
private final RandomLinkIdGenerator linkIdGenerator;
|
||||
public LinkUseCase(LinkRepository linkRepository,
|
||||
CategoryRepository categoryRepository,
|
||||
TaggingUseCase taggingUseCase,
|
||||
RandomLinkIdGenerator linkIdGenerator) {
|
||||
this.linkRepository = linkRepository;
|
||||
this.categoryRepository = categoryRepository;
|
||||
this.taggingUseCase = taggingUseCase;
|
||||
this.linkIdGenerator = linkIdGenerator;
|
||||
}
|
||||
|
||||
public LinkUseCase(LinkRepository linkRepository,
|
||||
CategoryRepository categoryRepository,
|
||||
TaggingUseCase taggingUseCase,
|
||||
RandomLinkIdGenerator linkIdGenerator) {
|
||||
this.linkRepository = linkRepository;
|
||||
this.categoryRepository = categoryRepository;
|
||||
this.taggingUseCase = taggingUseCase;
|
||||
this.linkIdGenerator = linkIdGenerator;
|
||||
}
|
||||
public void addLink(LinkUrl url, Set<CategoryName> categoryNames, Username creator) {
|
||||
if (!OnlineCheck.isReachable(url)) {
|
||||
throw new URLIsNotReachable("The url " + url + " does not seem online. Check if it is correct and you have internet access.");
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
public void addLink(LinkUrl url, Set<CategoryName> categoryNames, Username creator) {
|
||||
|
||||
if (!OnlineCheck.isReachable(url)) {
|
||||
throw new URLIsNotReachable("The url " + url + " does not seem online. Check if it is correct and you have internet access.");
|
||||
}
|
||||
public Set<LinkDto> getLinks() {
|
||||
return linkRepository.getAll().stream().map(this::convertLink).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
var categoryIds = categoryNames.stream().map(categoryRepository::getIdByName).collect(Collectors.toSet());
|
||||
public Set<LinkDto> getLinksForCategory(CategoryName categoryName) {
|
||||
return linkRepository.getByCategory(categoryRepository.getIdByName(categoryName).id()).stream().map(this::convertLink).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
var tags = taggingUseCase.getTagsFor(url);
|
||||
public Set<LinkDto> getLinksForTag(TagName tagName) {
|
||||
return linkRepository.getByTag(tagName).stream().map(this::convertLink).collect(Collectors.toSet());
|
||||
}
|
||||
public Set<LinkDto> getLinksForUser(Username username) {
|
||||
return linkRepository.getByUser(username).stream().map(this::convertLink).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
var id = linkIdGenerator.generateId();
|
||||
private LinkDto convertLink(Link link) {
|
||||
return new LinkDto(link.getCreator(), link.getUrl(), getCategoriesOf(link), link.getTags());
|
||||
}
|
||||
|
||||
var link = new Link(id, creator, url, categoryIds, tags);
|
||||
private Set<Category> getCategoriesOf(Link link) {
|
||||
return link.getCategoryIds().stream().map(getCategoryForId()).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
linkRepository.add(link);
|
||||
}
|
||||
|
||||
public Set<LinkDto> getLinks() {
|
||||
return linkRepository.getAll().stream().map(convertLink()).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
private Function<Link, LinkDto> convertLink() {
|
||||
return link -> new LinkDto(link.getCreator(), link.getUrl(), getCategoriesOf(link), link.getTags());
|
||||
}
|
||||
|
||||
private Set<Category> getCategoriesOf(Link link) {
|
||||
return link.getCategoryIds().stream().map(getCategoryForId()).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
private Function<CategoryId, Category> getCategoryForId() {
|
||||
return id -> categoryRepository
|
||||
.getById(id)
|
||||
.orElseThrow(() -> new CategroyDoesNotExist(
|
||||
"A Category for a certain id does not exits. You must create it first."));
|
||||
}
|
||||
private Function<CategoryId, Category> getCategoryForId() {
|
||||
return id -> categoryRepository
|
||||
.getById(id)
|
||||
.orElseThrow(() -> new CategroyDoesNotExist(
|
||||
"A Category for a certain id does not exits. You must create it first."));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,15 +2,22 @@ package tag;
|
|||
|
||||
import tag.matcherImplementations.CustomTagMatcher;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CustomTagsUseCase {
|
||||
|
||||
private final TagMatcherRepository tagMatcherRepository;
|
||||
private final TagMatcherRepository tagMatcherRepository;
|
||||
|
||||
public CustomTagsUseCase(TagMatcherRepository tagMatcherRepository) {
|
||||
this.tagMatcherRepository = tagMatcherRepository;
|
||||
}
|
||||
public CustomTagsUseCase(TagMatcherRepository tagMatcherRepository) {
|
||||
this.tagMatcherRepository = tagMatcherRepository;
|
||||
}
|
||||
|
||||
public void addCustomTagMatcher(CustomTagMatcher customTagMatcher) {
|
||||
tagMatcherRepository.addCustomTagMatcher(customTagMatcher);
|
||||
}
|
||||
public void addCustomTagMatcher(CustomTagMatcher customTagMatcher) {
|
||||
tagMatcherRepository.addCustomTagMatcher(customTagMatcher);
|
||||
}
|
||||
|
||||
public Set<TagName> getAllTagNames() {
|
||||
return this.tagMatcherRepository.getTagMatchers().stream().map(TagMatcher::getTagName).collect(Collectors.toSet());
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue