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

@ -7,33 +7,52 @@ import java.util.stream.Collectors;
public class LinkCommands extends Subcommand { public class LinkCommands extends Subcommand {
private final LinkCliAdapter linkCliAdapter; private final LinkCliAdapter linkCliAdapter;
public LinkCommands(LinkCliAdapter linkCliAdapter) { public LinkCommands(LinkCliAdapter linkCliAdapter) {
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
public String getSubcommand() { public String getSubcommand() {
return "link"; return "link";
} }
@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) {
linkCliAdapter.addLink(args[1], linkCliAdapter.addLink(args[1],
Arrays.stream(Arrays.copyOfRange(args, 3, args.length)).collect(Collectors.toSet()), Arrays.stream(Arrays.copyOfRange(args, 3, args.length)).collect(Collectors.toSet()),
args[2]); args[2]);
return "Added the new Link"; return "Added the new Link";
} }
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]));
}
} }

View File

@ -2,32 +2,34 @@ 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;
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
public String getSubcommand() { public String getSubcommand() {
return "tag"; return "tag";
} }
@Override @Override
public String getUsage() { public String getUsage() {
return "Usage: " + System.lineSeparator()+ return "Usage: " + System.lineSeparator() +
getSubcommand() + " add tagName tagRegexExpression"; getSubcommand() + " add tagName tagRegexExpression";
} }
private String addCustomTag(String[] args) { private String addCustomTag(String[] args) {
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());
}
} }

View File

@ -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;
@ -11,19 +12,31 @@ import java.util.stream.Collectors;
public class LinkCliAdapter { public class LinkCliAdapter {
private final LinkUseCase linkUseCase; private final LinkUseCase linkUseCase;
public LinkCliAdapter(LinkUseCase linkUseCase) { public LinkCliAdapter(LinkUseCase linkUseCase) {
this.linkUseCase = linkUseCase; this.linkUseCase = linkUseCase;
} }
public void addLink(String url, Set<String> categoryNames, String creator) { public void addLink(String url, Set<String> categoryNames, String creator) {
linkUseCase.addLink(new LinkUrl(url), linkUseCase.addLink(new LinkUrl(url),
categoryNames.stream().map(CategoryName::new).collect(Collectors.toSet()), categoryNames.stream().map(CategoryName::new).collect(Collectors.toSet()),
new Username(creator)); new Username(creator));
} }
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());
}
} }

View File

@ -4,16 +4,22 @@ 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;
public CustomTagsCliAdapter(CustomTagsUseCase customTagsUseCase) { public CustomTagsCliAdapter(CustomTagsUseCase customTagsUseCase) {
this.customTagsUseCase = customTagsUseCase; this.customTagsUseCase = customTagsUseCase;
} }
public void addCustomTagMatcher(String name, String regexString) { public void addCustomTagMatcher(String name, String regexString) {
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());
}
} }

View File

@ -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,55 +15,60 @@ 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 CategoryRepository categoryRepository;
private final TaggingUseCase taggingUseCase;
private final RandomLinkIdGenerator linkIdGenerator;
private final LinkRepository linkRepository; public LinkUseCase(LinkRepository linkRepository,
private final CategoryRepository categoryRepository; CategoryRepository categoryRepository,
private final TaggingUseCase taggingUseCase; TaggingUseCase taggingUseCase,
private final RandomLinkIdGenerator linkIdGenerator; RandomLinkIdGenerator linkIdGenerator) {
this.linkRepository = linkRepository;
this.categoryRepository = categoryRepository;
this.taggingUseCase = taggingUseCase;
this.linkIdGenerator = linkIdGenerator;
}
public LinkUseCase(LinkRepository linkRepository, public void addLink(LinkUrl url, Set<CategoryName> categoryNames, Username creator) {
CategoryRepository categoryRepository, if (!OnlineCheck.isReachable(url)) {
TaggingUseCase taggingUseCase, throw new URLIsNotReachable("The url " + url + " does not seem online. Check if it is correct and you have internet access.");
RandomLinkIdGenerator linkIdGenerator) { }
this.linkRepository = linkRepository; var categoryIds = categoryNames.stream().map(categoryRepository::getIdByName).collect(Collectors.toSet());
this.categoryRepository = categoryRepository; var tags = taggingUseCase.getTagsFor(url);
this.taggingUseCase = taggingUseCase; var id = linkIdGenerator.generateId();
this.linkIdGenerator = linkIdGenerator; var link = new Link(id, creator, url, categoryIds, tags);
} linkRepository.add(link);
}
public void addLink(LinkUrl url, Set<CategoryName> categoryNames, Username creator) {
if (!OnlineCheck.isReachable(url)) { public Set<LinkDto> getLinks() {
throw new URLIsNotReachable("The url " + url + " does not seem online. Check if it is correct and you have internet access."); return linkRepository.getAll().stream().map(this::convertLink).collect(Collectors.toSet());
} }
var categoryIds = categoryNames.stream().map(categoryRepository::getIdByName).collect(Collectors.toSet()); public Set<LinkDto> getLinksForCategory(CategoryName categoryName) {
return linkRepository.getByCategory(categoryRepository.getIdByName(categoryName).id()).stream().map(this::convertLink).collect(Collectors.toSet());
}
var tags = taggingUseCase.getTagsFor(url); 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());
}
var id = linkIdGenerator.generateId(); private LinkDto convertLink(Link link) {
return new LinkDto(link.getCreator(), link.getUrl(), getCategoriesOf(link), link.getTags());
}
var link = new Link(id, creator, url, categoryIds, tags); private Set<Category> getCategoriesOf(Link link) {
return link.getCategoryIds().stream().map(getCategoryForId()).collect(Collectors.toSet());
}
linkRepository.add(link); private Function<CategoryId, Category> getCategoryForId() {
} return id -> categoryRepository
.getById(id)
public Set<LinkDto> getLinks() { .orElseThrow(() -> new CategroyDoesNotExist(
return linkRepository.getAll().stream().map(convertLink()).collect(Collectors.toSet()); "A Category for a certain id does not exits. You must create it first."));
} }
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."));
}
} }

View File

@ -2,15 +2,22 @@ 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;
public CustomTagsUseCase(TagMatcherRepository tagMatcherRepository) { public CustomTagsUseCase(TagMatcherRepository tagMatcherRepository) {
this.tagMatcherRepository = tagMatcherRepository; this.tagMatcherRepository = tagMatcherRepository;
} }
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());
}
} }

View File

@ -5,58 +5,73 @@ 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 {
final private CustomSet<Link> links; final private CustomSet<Link> links;
public LinkRepository(PersistenceAdapter<Link> linkPersistenceAdapter) { public LinkRepository(PersistenceAdapter<Link> linkPersistenceAdapter) {
this.links = new CustomSetPersistenceDecorator<>(linkPersistenceAdapter); this.links = new CustomSetPersistenceDecorator<>(linkPersistenceAdapter);
} }
public Optional<Link> getById(LinkId id) { public Optional<Link> getById(LinkId id) {
return links.find(link -> link.getId().equals(id)); return links.find(link -> link.getId().equals(id));
} }
public Optional<Link> getByUrl(LinkUrl url) { public Optional<Link> getByUrl(LinkUrl url) {
return links.find(link -> link.getUrl().equals(url)); return links.find(link -> link.getUrl().equals(url));
} }
public void removeById(LinkId id) { public void removeById(LinkId id) {
getById(id).ifPresentOrElse(this::remove, () -> { getById(id).ifPresentOrElse(this::remove, () -> {
throw new LinkDoesNotExist("Tried removing a link with id " + id + " but it does not exist."); throw new LinkDoesNotExist("Tried removing a link with id " + id + " but it does not exist.");
}); });
} }
public void removeByUrl(LinkUrl url) { public void removeByUrl(LinkUrl url) {
getByUrl(url).ifPresentOrElse(this::remove, () -> { getByUrl(url).ifPresentOrElse(this::remove, () -> {
throw new LinkDoesNotExist("Tried removing a link with url " + url + " but it does not exist."); throw new LinkDoesNotExist("Tried removing a link with url " + url + " but it does not exist.");
}); });
} }
public void add(Link link) { public void add(Link link) {
checkDuplicates(link); checkDuplicates(link);
links.add(link); links.add(link);
} }
public Set<Link> getAll() { public Set<Link> getAll() {
return links.getSet(); return links.getSet();
} }
private void remove(Link link) { private void remove(Link link) {
links.remove(link); links.remove(link);
} }
private void checkDuplicates(Link newLink) { private void checkDuplicates(Link newLink) {
if (this.getById(newLink.getId()).isPresent()) { if (this.getById(newLink.getId()).isPresent()) {
throw new LinkAlreadyExists("A link with the id " + newLink.getId() + " already exitsts"); throw new LinkAlreadyExists("A link with the id " + newLink.getId() + " already exitsts");
} }
if (this.getByUrl(newLink.getUrl()).isPresent()) { if (this.getByUrl(newLink.getUrl()).isPresent()) {
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());
}
} }

View File

@ -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) {