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 {
private final LinkCliAdapter linkCliAdapter;
private final LinkCliAdapter linkCliAdapter;
public LinkCommands(LinkCliAdapter linkCliAdapter) {
this.linkCliAdapter = linkCliAdapter;
commands.put("add", this::addLink);
commands.put("get", this::getAll);
}
public LinkCommands(LinkCliAdapter linkCliAdapter) {
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
public String getSubcommand() {
return "link";
}
@Override
public String getSubcommand() {
return "link";
}
@Override
public String getUsage() {
return "Usage: " + System.lineSeparator() +
getSubcommand() + "add http://example.org yourUsername [category1 category2 .. categoryN]";
}
@Override
public String getUsage() {
return "Usage: " + System.lineSeparator() +
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) {
linkCliAdapter.addLink(args[1],
Arrays.stream(Arrays.copyOfRange(args, 3, args.length)).collect(Collectors.toSet()),
args[2]);
return "Added the new Link";
}
private String addLink(String[] args) {
linkCliAdapter.addLink(args[1],
Arrays.stream(Arrays.copyOfRange(args, 3, args.length)).collect(Collectors.toSet()),
args[2]);
return "Added the new Link";
}
private String getAll(String[] args) {
return String.join(System.lineSeparator(), linkCliAdapter.getLinks());
}
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,32 +2,34 @@ package cli.tag;
import cli.Subcommand;
import java.util.HashMap;
import java.util.function.Function;
public class TagCommands extends Subcommand {
final private CustomTagsCliAdapter customTagsCliAdapter;
final private CustomTagsCliAdapter customTagsCliAdapter;
public TagCommands(CustomTagsCliAdapter customTagsCliAdapter) {
this.customTagsCliAdapter = customTagsCliAdapter;
commands.put("add", this::addCustomTag);
}
public TagCommands(CustomTagsCliAdapter customTagsCliAdapter) {
this.customTagsCliAdapter = customTagsCliAdapter;
commands.put("add", this::addCustomTag);
commands.put("get", this::getTags);
}
@Override
public String getSubcommand() {
return "tag";
}
@Override
public String getSubcommand() {
return "tag";
}
@Override
public String getUsage() {
return "Usage: " + System.lineSeparator()+
getSubcommand() + " add tagName tagRegexExpression";
}
@Override
public String getUsage() {
return "Usage: " + System.lineSeparator() +
getSubcommand() + " add tagName tagRegexExpression";
}
private String addCustomTag(String[] args) {
customTagsCliAdapter.addCustomTagMatcher(args[1], args[2]);
return "Added the new Tag matcher";
}
private String addCustomTag(String[] args) {
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;
@ -11,19 +12,31 @@ import java.util.stream.Collectors;
public class LinkCliAdapter {
private final LinkUseCase linkUseCase;
private final LinkUseCase linkUseCase;
public LinkCliAdapter(LinkUseCase linkUseCase) {
this.linkUseCase = linkUseCase;
}
public LinkCliAdapter(LinkUseCase linkUseCase) {
this.linkUseCase = linkUseCase;
}
public void addLink(String url, Set<String> categoryNames, String creator) {
linkUseCase.addLink(new LinkUrl(url),
categoryNames.stream().map(CategoryName::new).collect(Collectors.toSet()),
new Username(creator));
}
public void addLink(String url, Set<String> categoryNames, String creator) {
linkUseCase.addLink(new LinkUrl(url),
categoryNames.stream().map(CategoryName::new).collect(Collectors.toSet()),
new Username(creator));
}
public Set<String> getLinks() {
return linkUseCase.getLinks().stream().map(LinkDto::toString).collect(Collectors.toSet());
}
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,16 +4,22 @@ 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;
private final CustomTagsUseCase customTagsUseCase;
public CustomTagsCliAdapter(CustomTagsUseCase customTagsUseCase) {
this.customTagsUseCase = customTagsUseCase;
}
public CustomTagsCliAdapter(CustomTagsUseCase customTagsUseCase) {
this.customTagsUseCase = customTagsUseCase;
}
public void addCustomTagMatcher(String name, String regexString) {
customTagsUseCase.addCustomTagMatcher(new CustomTagMatcher(new TagName(name), regexString));
}
public void addCustomTagMatcher(String name, String 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 exeptions.CategroyDoesNotExist;
import exeptions.URLIsNotReachable;
import tag.TagName;
import tag.TaggingUseCase;
import user.Username;
@ -14,55 +15,60 @@ 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;
private final RandomLinkIdGenerator linkIdGenerator;
private final LinkRepository linkRepository;
private final CategoryRepository categoryRepository;
private final TaggingUseCase taggingUseCase;
private final RandomLinkIdGenerator linkIdGenerator;
public LinkUseCase(LinkRepository linkRepository,
CategoryRepository categoryRepository,
TaggingUseCase taggingUseCase,
RandomLinkIdGenerator linkIdGenerator) {
this.linkRepository = linkRepository;
this.categoryRepository = categoryRepository;
this.taggingUseCase = taggingUseCase;
this.linkIdGenerator = linkIdGenerator;
}
public LinkUseCase(LinkRepository linkRepository,
CategoryRepository categoryRepository,
TaggingUseCase taggingUseCase,
RandomLinkIdGenerator linkIdGenerator) {
this.linkRepository = linkRepository;
this.categoryRepository = categoryRepository;
this.taggingUseCase = taggingUseCase;
this.linkIdGenerator = linkIdGenerator;
}
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 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.");
}
public Set<LinkDto> getLinks() {
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);
}
public Set<LinkDto> getLinks() {
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."));
}
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 java.util.Set;
import java.util.stream.Collectors;
public class CustomTagsUseCase {
private final TagMatcherRepository tagMatcherRepository;
private final TagMatcherRepository tagMatcherRepository;
public CustomTagsUseCase(TagMatcherRepository tagMatcherRepository) {
this.tagMatcherRepository = tagMatcherRepository;
}
public CustomTagsUseCase(TagMatcherRepository tagMatcherRepository) {
this.tagMatcherRepository = tagMatcherRepository;
}
public void addCustomTagMatcher(CustomTagMatcher customTagMatcher) {
tagMatcherRepository.addCustomTagMatcher(customTagMatcher);
}
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,58 +5,73 @@ 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 {
final private CustomSet<Link> links;
final private CustomSet<Link> links;
public LinkRepository(PersistenceAdapter<Link> linkPersistenceAdapter) {
this.links = new CustomSetPersistenceDecorator<>(linkPersistenceAdapter);
}
public LinkRepository(PersistenceAdapter<Link> linkPersistenceAdapter) {
this.links = new CustomSetPersistenceDecorator<>(linkPersistenceAdapter);
}
public Optional<Link> getById(LinkId id) {
return links.find(link -> link.getId().equals(id));
}
public Optional<Link> getById(LinkId id) {
return links.find(link -> link.getId().equals(id));
}
public Optional<Link> getByUrl(LinkUrl url) {
return links.find(link -> link.getUrl().equals(url));
}
public Optional<Link> getByUrl(LinkUrl url) {
return links.find(link -> link.getUrl().equals(url));
}
public void removeById(LinkId id) {
getById(id).ifPresentOrElse(this::remove, () -> {
throw new LinkDoesNotExist("Tried removing a link with id " + id + " but it does not exist.");
});
}
public void removeById(LinkId id) {
getById(id).ifPresentOrElse(this::remove, () -> {
throw new LinkDoesNotExist("Tried removing a link with id " + id + " but it does not exist.");
});
}
public void removeByUrl(LinkUrl url) {
getByUrl(url).ifPresentOrElse(this::remove, () -> {
throw new LinkDoesNotExist("Tried removing a link with url " + url + " but it does not exist.");
});
}
public void removeByUrl(LinkUrl url) {
getByUrl(url).ifPresentOrElse(this::remove, () -> {
throw new LinkDoesNotExist("Tried removing a link with url " + url + " but it does not exist.");
});
}
public void add(Link link) {
checkDuplicates(link);
links.add(link);
}
public void add(Link link) {
checkDuplicates(link);
links.add(link);
}
public Set<Link> getAll() {
return links.getSet();
}
public Set<Link> getAll() {
return links.getSet();
}
private void remove(Link link) {
links.remove(link);
}
private void remove(Link link) {
links.remove(link);
}
private void checkDuplicates(Link newLink) {
if (this.getById(newLink.getId()).isPresent()) {
throw new LinkAlreadyExists("A link with the id " + newLink.getId() + " already exitsts");
}
private void checkDuplicates(Link newLink) {
if (this.getById(newLink.getId()).isPresent()) {
throw new LinkAlreadyExists("A link with the id " + newLink.getId() + " already exitsts");
}
if (this.getByUrl(newLink.getUrl()).isPresent()) {
throw new LinkAlreadyExists("A link with the url " + newLink.getUrl() + " already exitsts");
}
}
if (this.getByUrl(newLink.getUrl()).isPresent()) {
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) {