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