Add querying use cases
continuous-integration/drone/push Build is failing Details

This commit is contained in:
qvalentin 2022-05-25 19:13:52 +02:00
parent fd4a2c0e83
commit 71b28222a7
8 changed files with 220 additions and 152 deletions

View File

@ -13,6 +13,9 @@ public class LinkCommands extends Subcommand {
this.linkCliAdapter = linkCliAdapter;
commands.put("add", this::addLink);
commands.put("get", this::getAll);
commands.put("category", this::getByCategory);
commands.put("user", this::getByUser);
commands.put("tag", this::getByTag);
}
@Override
@ -23,7 +26,11 @@ public class LinkCommands extends Subcommand {
@Override
public String getUsage() {
return "Usage: " + System.lineSeparator() +
getSubcommand() + "add http://example.org yourUsername [category1 category2 .. categoryN]";
getSubcommand() + "add http://example.org yourUsername [category1 category2 .. categoryN]" + System.lineSeparator() +
getSubcommand() + "get " + System.lineSeparator() +
getSubcommand() + "category aCategoryName " + System.lineSeparator() +
getSubcommand() + "tag aTagName " + System.lineSeparator() +
getSubcommand() + "user aUserName ";
}
private String addLink(String[] args) {
@ -36,4 +43,16 @@ public class LinkCommands extends Subcommand {
private String getAll(String[] args) {
return String.join(System.lineSeparator(), linkCliAdapter.getLinks());
}
private String getByCategory(String[] args) {
return String.join(System.lineSeparator(), linkCliAdapter.getLinksForCategory(args[1]));
}
private String getByUser(String[] args) {
return String.join(System.lineSeparator(), linkCliAdapter.getLinksForUser(args[1]));
}
private String getByTag(String[] args) {
return String.join(System.lineSeparator(), linkCliAdapter.getLinksForTag(args[1]));
}
}

View File

@ -2,9 +2,6 @@ package cli.tag;
import cli.Subcommand;
import java.util.HashMap;
import java.util.function.Function;
public class TagCommands extends Subcommand {
final private CustomTagsCliAdapter customTagsCliAdapter;
@ -13,6 +10,7 @@ public class TagCommands extends Subcommand {
public TagCommands(CustomTagsCliAdapter customTagsCliAdapter) {
this.customTagsCliAdapter = customTagsCliAdapter;
commands.put("add", this::addCustomTag);
commands.put("get", this::getTags);
}
@Override
@ -30,4 +28,8 @@ public class TagCommands extends Subcommand {
customTagsCliAdapter.addCustomTagMatcher(args[1], args[2]);
return "Added the new Tag matcher";
}
private String getTags(String[] args) {
return String.join(System.lineSeparator(), customTagsCliAdapter.getAllTagNames());
}
}

View File

@ -4,6 +4,7 @@ import category.CategoryName;
import link.LinkDto;
import link.LinkUrl;
import link.LinkUseCase;
import tag.TagName;
import user.Username;
import java.util.Set;
@ -26,4 +27,16 @@ public class LinkCliAdapter {
public Set<String> getLinks() {
return linkUseCase.getLinks().stream().map(LinkDto::toString).collect(Collectors.toSet());
}
public Set<String> getLinksForCategory(String categoryName) {
return linkUseCase.getLinksForCategory(new CategoryName(categoryName)).stream().map(LinkDto::toString).collect(Collectors.toSet());
}
public Set<String> getLinksForTag(String tagName) {
return linkUseCase.getLinksForTag(new TagName(tagName)).stream().map(LinkDto::toString).collect(Collectors.toSet());
}
public Set<String> getLinksForUser(String userName) {
return linkUseCase.getLinksForUser(new Username(userName)).stream().map(LinkDto::toString).collect(Collectors.toSet());
}
}

View File

@ -4,6 +4,9 @@ import tag.CustomTagsUseCase;
import tag.TagName;
import tag.matcherImplementations.CustomTagMatcher;
import java.util.Set;
import java.util.stream.Collectors;
public class CustomTagsCliAdapter {
private final CustomTagsUseCase customTagsUseCase;
@ -16,4 +19,7 @@ public class CustomTagsCliAdapter {
customTagsUseCase.addCustomTagMatcher(new CustomTagMatcher(new TagName(name), regexString));
}
public Set<String> getAllTagNames() {
return customTagsUseCase.getAllTagNames().stream().map(TagName::toString).collect(Collectors.toSet());
}
}

View File

@ -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,7 +15,6 @@ 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;
@ -31,28 +31,34 @@ public class LinkUseCase {
}
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 Set<LinkDto> getLinks() {
return linkRepository.getAll().stream().map(convertLink()).collect(Collectors.toSet());
return linkRepository.getAll().stream().map(this::convertLink).collect(Collectors.toSet());
}
private Function<Link, LinkDto> convertLink() {
return link -> new LinkDto(link.getCreator(), link.getUrl(), getCategoriesOf(link), link.getTags());
public Set<LinkDto> getLinksForCategory(CategoryName categoryName) {
return linkRepository.getByCategory(categoryRepository.getIdByName(categoryName).id()).stream().map(this::convertLink).collect(Collectors.toSet());
}
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());
}
private LinkDto convertLink(Link link) {
return new LinkDto(link.getCreator(), link.getUrl(), getCategoriesOf(link), link.getTags());
}
private Set<Category> getCategoriesOf(Link link) {

View File

@ -2,6 +2,9 @@ package tag;
import tag.matcherImplementations.CustomTagMatcher;
import java.util.Set;
import java.util.stream.Collectors;
public class CustomTagsUseCase {
private final TagMatcherRepository tagMatcherRepository;
@ -13,4 +16,8 @@ public class CustomTagsUseCase {
public void addCustomTagMatcher(CustomTagMatcher customTagMatcher) {
tagMatcherRepository.addCustomTagMatcher(customTagMatcher);
}
public Set<TagName> getAllTagNames() {
return this.tagMatcherRepository.getTagMatchers().stream().map(TagMatcher::getTagName).collect(Collectors.toSet());
}
}

View File

@ -5,9 +5,12 @@ import abstraction.PersistenceAdapter;
import datastructures.set.CustomSet;
import exeptions.LinkAlreadyExists;
import exeptions.LinkDoesNotExist;
import tag.TagName;
import user.Username;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
public class LinkRepository {
@ -59,4 +62,16 @@ public class LinkRepository {
throw new LinkAlreadyExists("A link with the url " + newLink.getUrl() + " already exitsts");
}
}
public Set<Link> getByCategory(int id) {
return links.stream().filter(it -> it.getCategoryIds().contains(id)).collect(Collectors.toSet());
}
public Set<Link> getByTag(TagName tagName) {
return links.stream().filter(it -> it.getTags().stream().anyMatch(tag -> tag.getName().equals(tagName))).collect(Collectors.toSet());
}
public Set<Link> getByUser(Username username) {
return links.stream().filter(it -> it.getCreator().equals(username)).collect(Collectors.toSet());
}
}

View File

@ -4,7 +4,7 @@ import java.util.Optional;
public class Tag {
private TagName name;
private final TagName name;
private Optional<String> additionalData = Optional.empty();
public Tag(TagName name) {