This commit is contained in:
parent
fd4a2c0e83
commit
71b28222a7
|
@ -13,6 +13,9 @@ public class LinkCommands extends Subcommand {
|
||||||
this.linkCliAdapter = linkCliAdapter;
|
this.linkCliAdapter = linkCliAdapter;
|
||||||
commands.put("add", this::addLink);
|
commands.put("add", this::addLink);
|
||||||
commands.put("get", this::getAll);
|
commands.put("get", this::getAll);
|
||||||
|
commands.put("category", this::getByCategory);
|
||||||
|
commands.put("user", this::getByUser);
|
||||||
|
commands.put("tag", this::getByTag);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -23,7 +26,11 @@ public class LinkCommands extends Subcommand {
|
||||||
@Override
|
@Override
|
||||||
public String getUsage() {
|
public String getUsage() {
|
||||||
return "Usage: " + System.lineSeparator() +
|
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) {
|
private String addLink(String[] args) {
|
||||||
|
@ -36,4 +43,16 @@ public class LinkCommands extends Subcommand {
|
||||||
private String getAll(String[] args) {
|
private String getAll(String[] args) {
|
||||||
return String.join(System.lineSeparator(), linkCliAdapter.getLinks());
|
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]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,6 @@ package cli.tag;
|
||||||
|
|
||||||
import cli.Subcommand;
|
import cli.Subcommand;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
public class TagCommands extends Subcommand {
|
public class TagCommands extends Subcommand {
|
||||||
|
|
||||||
final private CustomTagsCliAdapter customTagsCliAdapter;
|
final private CustomTagsCliAdapter customTagsCliAdapter;
|
||||||
|
@ -13,6 +10,7 @@ public class TagCommands extends Subcommand {
|
||||||
public TagCommands(CustomTagsCliAdapter customTagsCliAdapter) {
|
public TagCommands(CustomTagsCliAdapter customTagsCliAdapter) {
|
||||||
this.customTagsCliAdapter = customTagsCliAdapter;
|
this.customTagsCliAdapter = customTagsCliAdapter;
|
||||||
commands.put("add", this::addCustomTag);
|
commands.put("add", this::addCustomTag);
|
||||||
|
commands.put("get", this::getTags);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -22,7 +20,7 @@ public class TagCommands extends Subcommand {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getUsage() {
|
public String getUsage() {
|
||||||
return "Usage: " + System.lineSeparator()+
|
return "Usage: " + System.lineSeparator() +
|
||||||
getSubcommand() + " add tagName tagRegexExpression";
|
getSubcommand() + " add tagName tagRegexExpression";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,4 +28,8 @@ public class TagCommands extends Subcommand {
|
||||||
customTagsCliAdapter.addCustomTagMatcher(args[1], args[2]);
|
customTagsCliAdapter.addCustomTagMatcher(args[1], args[2]);
|
||||||
return "Added the new Tag matcher";
|
return "Added the new Tag matcher";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String getTags(String[] args) {
|
||||||
|
return String.join(System.lineSeparator(), customTagsCliAdapter.getAllTagNames());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import category.CategoryName;
|
||||||
import link.LinkDto;
|
import link.LinkDto;
|
||||||
import link.LinkUrl;
|
import link.LinkUrl;
|
||||||
import link.LinkUseCase;
|
import link.LinkUseCase;
|
||||||
|
import tag.TagName;
|
||||||
import user.Username;
|
import user.Username;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -26,4 +27,16 @@ public class LinkCliAdapter {
|
||||||
public Set<String> getLinks() {
|
public Set<String> getLinks() {
|
||||||
return linkUseCase.getLinks().stream().map(LinkDto::toString).collect(Collectors.toSet());
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,9 @@ import tag.CustomTagsUseCase;
|
||||||
import tag.TagName;
|
import tag.TagName;
|
||||||
import tag.matcherImplementations.CustomTagMatcher;
|
import tag.matcherImplementations.CustomTagMatcher;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class CustomTagsCliAdapter {
|
public class CustomTagsCliAdapter {
|
||||||
|
|
||||||
private final CustomTagsUseCase customTagsUseCase;
|
private final CustomTagsUseCase customTagsUseCase;
|
||||||
|
@ -16,4 +19,7 @@ public class CustomTagsCliAdapter {
|
||||||
customTagsUseCase.addCustomTagMatcher(new CustomTagMatcher(new TagName(name), regexString));
|
customTagsUseCase.addCustomTagMatcher(new CustomTagMatcher(new TagName(name), regexString));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Set<String> getAllTagNames() {
|
||||||
|
return customTagsUseCase.getAllTagNames().stream().map(TagName::toString).collect(Collectors.toSet());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import category.CategoryName;
|
||||||
import category.CategoryRepository;
|
import category.CategoryRepository;
|
||||||
import exeptions.CategroyDoesNotExist;
|
import exeptions.CategroyDoesNotExist;
|
||||||
import exeptions.URLIsNotReachable;
|
import exeptions.URLIsNotReachable;
|
||||||
|
import tag.TagName;
|
||||||
import tag.TaggingUseCase;
|
import tag.TaggingUseCase;
|
||||||
import user.Username;
|
import user.Username;
|
||||||
|
|
||||||
|
@ -14,7 +15,6 @@ import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class LinkUseCase {
|
public class LinkUseCase {
|
||||||
|
|
||||||
private final LinkRepository linkRepository;
|
private final LinkRepository linkRepository;
|
||||||
private final CategoryRepository categoryRepository;
|
private final CategoryRepository categoryRepository;
|
||||||
private final TaggingUseCase taggingUseCase;
|
private final TaggingUseCase taggingUseCase;
|
||||||
|
@ -31,28 +31,34 @@ public class LinkUseCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addLink(LinkUrl url, Set<CategoryName> categoryNames, Username creator) {
|
public void addLink(LinkUrl url, Set<CategoryName> categoryNames, Username creator) {
|
||||||
|
|
||||||
if (!OnlineCheck.isReachable(url)) {
|
if (!OnlineCheck.isReachable(url)) {
|
||||||
throw new URLIsNotReachable("The url " + url + " does not seem online. Check if it is correct and you have internet access.");
|
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 categoryIds = categoryNames.stream().map(categoryRepository::getIdByName).collect(Collectors.toSet());
|
||||||
|
|
||||||
var tags = taggingUseCase.getTagsFor(url);
|
var tags = taggingUseCase.getTagsFor(url);
|
||||||
|
|
||||||
var id = linkIdGenerator.generateId();
|
var id = linkIdGenerator.generateId();
|
||||||
|
|
||||||
var link = new Link(id, creator, url, categoryIds, tags);
|
var link = new Link(id, creator, url, categoryIds, tags);
|
||||||
|
|
||||||
linkRepository.add(link);
|
linkRepository.add(link);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Set<LinkDto> getLinks() {
|
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() {
|
public Set<LinkDto> getLinksForCategory(CategoryName categoryName) {
|
||||||
return link -> new LinkDto(link.getCreator(), link.getUrl(), getCategoriesOf(link), link.getTags());
|
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) {
|
private Set<Category> getCategoriesOf(Link link) {
|
||||||
|
|
|
@ -2,6 +2,9 @@ package tag;
|
||||||
|
|
||||||
import tag.matcherImplementations.CustomTagMatcher;
|
import tag.matcherImplementations.CustomTagMatcher;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class CustomTagsUseCase {
|
public class CustomTagsUseCase {
|
||||||
|
|
||||||
private final TagMatcherRepository tagMatcherRepository;
|
private final TagMatcherRepository tagMatcherRepository;
|
||||||
|
@ -13,4 +16,8 @@ public class CustomTagsUseCase {
|
||||||
public void addCustomTagMatcher(CustomTagMatcher customTagMatcher) {
|
public void addCustomTagMatcher(CustomTagMatcher customTagMatcher) {
|
||||||
tagMatcherRepository.addCustomTagMatcher(customTagMatcher);
|
tagMatcherRepository.addCustomTagMatcher(customTagMatcher);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Set<TagName> getAllTagNames() {
|
||||||
|
return this.tagMatcherRepository.getTagMatchers().stream().map(TagMatcher::getTagName).collect(Collectors.toSet());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,9 +5,12 @@ import abstraction.PersistenceAdapter;
|
||||||
import datastructures.set.CustomSet;
|
import datastructures.set.CustomSet;
|
||||||
import exeptions.LinkAlreadyExists;
|
import exeptions.LinkAlreadyExists;
|
||||||
import exeptions.LinkDoesNotExist;
|
import exeptions.LinkDoesNotExist;
|
||||||
|
import tag.TagName;
|
||||||
|
import user.Username;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class LinkRepository {
|
public class LinkRepository {
|
||||||
|
|
||||||
|
@ -59,4 +62,16 @@ public class LinkRepository {
|
||||||
throw new LinkAlreadyExists("A link with the url " + newLink.getUrl() + " already exitsts");
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ import java.util.Optional;
|
||||||
|
|
||||||
public class Tag {
|
public class Tag {
|
||||||
|
|
||||||
private TagName name;
|
private final TagName name;
|
||||||
private Optional<String> additionalData = Optional.empty();
|
private Optional<String> additionalData = Optional.empty();
|
||||||
|
|
||||||
public Tag(TagName name) {
|
public Tag(TagName name) {
|
||||||
|
|
Reference in a new issue