Added tags CRUD operations.

This commit is contained in:
Donato Rimenti
2018-03-20 00:04:56 +01:00
parent 53ec7a8313
commit d192cf8c9c
3 changed files with 87 additions and 6 deletions
@@ -11,7 +11,7 @@ import java.util.List;
public class Post {
/**
* Title of the post.
* Title of the post. Must be unique.
*/
private String title;
@@ -8,11 +8,15 @@ import java.util.stream.StreamSupport;
import org.bson.Document;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Updates;
import com.mongodb.client.result.UpdateResult;
/**
* Repository used to manage tags for a blog post.
@@ -89,6 +93,36 @@ public class TagRepository implements Closeable {
.collect(Collectors.toList());
}
/**
* Adds a list of tags to the blog post with the given title.
*
* @param title
* the title of the blog post
* @param tags
* a list of tags to add
* @return the outcome of the operation
*/
public boolean addTags(String title, List<String> tags) {
UpdateResult result = collection.updateOne(new BasicDBObject(DBCollection.ID_FIELD_NAME, title),
Updates.addEachToSet(TAGS_FIELD, tags));
return result.getModifiedCount() == 1;
}
/**
* Removes a list of tags to the blog post with the given title.
*
* @param title
* the title of the blog post
* @param tags
* a list of tags to remove
* @return the outcome of the operation
*/
public boolean removeTags(String title, List<String> tags) {
UpdateResult result = collection.updateOne(new BasicDBObject(DBCollection.ID_FIELD_NAME, title),
Updates.pullAll(TAGS_FIELD, tags));
return result.getModifiedCount() == 1;
}
/**
* Utility method used to map a MongoDB document into a {@link Post}.
*
@@ -100,9 +134,9 @@ public class TagRepository implements Closeable {
@SuppressWarnings("unchecked")
private static Post documentToPost(Document document) {
Post post = new Post();
post.setTitle(document.getString(DBCollection.ID_FIELD_NAME));
post.setArticle(document.getString("article"));
post.setAuthor(document.getString("author"));
post.setTitle(document.getString("title"));
post.setTags((List<String>) document.get(TAGS_FIELD));
return post;
}