This commit is contained in:
parent
3ef31c9c3c
commit
a03206a8e1
|
@ -1,5 +1,7 @@
|
|||
package link;
|
||||
|
||||
import category.Category;
|
||||
import category.CategoryId;
|
||||
import category.CategoryName;
|
||||
import category.CategoryRepository;
|
||||
import exeptions.CategroyDoesNotExist;
|
||||
|
@ -8,6 +10,7 @@ import tag.TaggingUseCase;
|
|||
import user.Username;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class LinkUseCase {
|
||||
|
@ -45,19 +48,21 @@ public class LinkUseCase {
|
|||
}
|
||||
|
||||
public Set<LinkDto> getLinks() {
|
||||
return linkRepository
|
||||
.getAll()
|
||||
.stream()
|
||||
.map(link -> new LinkDto(link.getCreator(),
|
||||
link.getUrl(),
|
||||
link
|
||||
.getCategoryIds()
|
||||
.stream()
|
||||
.map(categoryRepository::getById)
|
||||
.map(optional -> optional.orElseThrow(() -> new CategroyDoesNotExist(
|
||||
"A Category for a certain id does not exits. You must create it first.")))
|
||||
.collect(Collectors.toSet()),
|
||||
link.getTags()))
|
||||
.collect(Collectors.toSet());
|
||||
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."));
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue