This commit is contained in:
parent
fd4a2c0e83
commit
71b28222a7
8 changed files with 220 additions and 152 deletions
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
Reference in a new issue