start adding Link repo

This commit is contained in:
qvalentin 2022-04-22 09:44:28 +02:00
parent c1852b8bac
commit 9c974db33f
Signed by: qvalentin
GPG Key ID: C979FA1EAFCABF1C
3 changed files with 89 additions and 0 deletions

View File

@ -21,4 +21,42 @@ public class Link {
this.categoryIds = categoryIds;
this.tags = tags;
}
public LinkId getId() {
return id;
}
public Username getCreator() {
return creator;
}
public LinkUrl getUrl() {
return url;
}
public Set<CategoryId> getCategoryIds() {
return categoryIds;
}
public Set<Tag> getTags() {
return tags;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Link link = (Link) o;
if (!id.equals(link.id)) return false;
return url.equals(link.url);
}
@Override
public int hashCode() {
int result = id.hashCode();
result = 31 * result + url.hashCode();
return result;
}
}

View File

@ -0,0 +1,10 @@
package link;
import java.util.Set;
public interface LinkPersistenceAdapter {
Set<Link> getAll();
Boolean add(Link link);
}

View File

@ -0,0 +1,41 @@
package link;
import exeptions.LinkAlreadyExists;
import java.util.Optional;
import java.util.Set;
public class LinkRepository {
final private LinkPersistenceAdapter linkPersistenceAdapter;
final private Set<Link> links;
public LinkRepository(LinkPersistenceAdapter linkPersistenceAdapter) {
this.linkPersistenceAdapter = linkPersistenceAdapter;
this.links = linkPersistenceAdapter.getAll();
}
public Optional<Link> getById(LinkId id) {
return links.stream().filter(link -> link.getId().equals(id)).findFirst();
}
public Optional<Link> getByUrl(LinkUrl url) {
return links.stream().filter(link -> link.getUrl().equals(url)).findFirst();
}
public void add(Link link) {
checkDuplicates(link);
}
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");
}
}
}