refactor long method
continuous-integration/drone/push Build is passing Details

This commit is contained in:
qvalentin 2022-05-17 16:53:32 +02:00
parent 3ef31c9c3c
commit a03206a8e1
Signed by: qvalentin
GPG Key ID: C979FA1EAFCABF1C
1 changed files with 19 additions and 14 deletions

View File

@ -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."));
}
}