Skip to content

Commit

Permalink
Merge pull request #2 from AO-StreetArt/innerObjUpdateSupport
Browse files Browse the repository at this point in the history
Add support for making updates to inner data elements within a project
  • Loading branch information
AO-StreetArt committed Dec 24, 2018
2 parents 752daaa + 72dbd46 commit b295217
Show file tree
Hide file tree
Showing 4 changed files with 772 additions and 13 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/ao/aeselprojects/ApMongoConfiguration.java
Expand Up @@ -142,4 +142,10 @@ public MongoClient mongoClient() {
protected String getDatabaseName() {
return "_projects";
}

// Definition for accessing underlying Mongo Driver
@Bean
public MongoDatabase mongoDatabase() {
return mongoDbFactory().getDb();
}
}
139 changes: 133 additions & 6 deletions src/main/java/com/ao/aeselprojects/controller/ProjectController.java
Expand Up @@ -20,6 +20,17 @@
import com.ao.aeselprojects.dao.ProjectRepository;
import com.ao.aeselprojects.model.Project;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Sorts;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.result.UpdateResult;

import java.io.IOException;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
Expand All @@ -31,16 +42,23 @@
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import org.bson.Document;
import org.bson.types.ObjectId;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -50,6 +68,7 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
Expand All @@ -71,6 +90,19 @@ public class ProjectController {
private static final Logger logger =
LogManager.getLogger("aeselprojects.ProjectController");

@Autowired
MongoDatabase mongoDb;
MongoCollection<Document> mongoCollection = null;
private String mongoCollectionName = "projects";

/**
* Use the Mongo Client to access the database and collection.
*/
@PostConstruct
public void init() {
mongoCollection = mongoDb.getCollection(mongoCollectionName);
}

/**
* Get a Project.
*/
Expand Down Expand Up @@ -168,21 +200,116 @@ public ResponseEntity<Project> createProject(
return new ResponseEntity<Project>(responseProject, responseHeaders, returnCode);
}

private BasicDBObject genUpdateQuery(String attrKey, String attrVal, String opType) {
BasicDBObject update = new BasicDBObject();
update.put(attrKey, attrVal);
return new BasicDBObject(opType, update);
}

private BasicDBObject genIdQuery(String id) {
BasicDBObject query = new BasicDBObject();
query.put("_id", new ObjectId(id));
return query;
}

/**
* Update an existing Project.
*/
@PostMapping("/v1/project/{id}")
@ResponseBody
public ResponseEntity<Project> updateProject(
public ResponseEntity<String> updateProject(
@PathVariable String id,
@RequestBody Project inpProject) {
logger.info("Responding to Project Create Request");

BasicDBObject updateQuery = new BasicDBObject();
if (inpProject.getName() != null && !(inpProject.getName().isEmpty())) {
updateQuery.put("name", inpProject.getName());
}
if (inpProject.getDescription() != null && !(inpProject.getDescription().isEmpty())) {
updateQuery.put("description", inpProject.getDescription());
}
if (inpProject.getCategory() != null && !(inpProject.getCategory().isEmpty())) {
updateQuery.put("category", inpProject.getCategory());
}
if (inpProject.getThumbnail() != null && !(inpProject.getThumbnail().isEmpty())) {
updateQuery.put("thumbnail", inpProject.getThumbnail());
}

UpdateResult result = mongoCollection.updateOne(genIdQuery(id),
new BasicDBObject("$set", updateQuery), new UpdateOptions());

// Set the http response code
HttpStatus returnCode = HttpStatus.OK;
if (result.getModifiedCount() < 1) {
returnCode = HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE;
logger.debug("No documents modified for array attribute update");
}
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("Content-Type", "application/json");
inpProject.setId(id);
Project responseProject = projects.save(inpProject);
return new ResponseEntity<Project>(responseProject, responseHeaders, returnCode);
return new ResponseEntity<String>("", responseHeaders, returnCode);
}

private ResponseEntity<String> updateArrayAttr(String projectKey,
String attrKey, String attrVal, String updType) {
BasicDBObject updateQuery = genUpdateQuery(attrKey, attrVal, updType);
BasicDBObject query = genIdQuery(projectKey);
UpdateResult result = mongoCollection.updateOne(query, updateQuery, new UpdateOptions());
// Set the http response code
HttpStatus returnCode = HttpStatus.OK;
if (result.getModifiedCount() < 1) {
returnCode = HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE;
logger.debug("No documents modified for array attribute update");
}
// Set up a response header to return a valid HTTP Response
HttpHeaders responseHeaders = new HttpHeaders();
return new ResponseEntity<String>("", responseHeaders, returnCode);
}

/**
* Add a tag to an existing Project.
*/
@PutMapping("/v1/project/{projectKey}/tags/{tagValue}")
@ResponseBody
public ResponseEntity<String> addTagToProject(
@PathVariable String projectKey,
@PathVariable String tagValue) {
logger.info("Adding tag to Project");
return updateArrayAttr(projectKey, "tags", tagValue, "$push");
}

/**
* Add an Asset Collection to an existing Project.
*/
@PutMapping("/v1/project/{projectKey}/collections/{collectionId}")
@ResponseBody
public ResponseEntity<String> addCollectionToProject(
@PathVariable String projectKey,
@PathVariable String collectionId) {
logger.info("Adding Asset Collection to Project");
return updateArrayAttr(projectKey, "assetCollectionIds", collectionId, "$push");
}

/**
* Remove a tag from an existing Project.
*/
@DeleteMapping("/v1/project/{projectKey}/tags/{tagValue}")
@ResponseBody
public ResponseEntity<String> removeTagFromProject(
@PathVariable String projectKey,
@PathVariable String tagValue) {
logger.info("Removing tag from Project");
return updateArrayAttr(projectKey, "tags", tagValue, "$pull");
}

/**
* Remove an Asset Collection from an existing Project.
*/
@DeleteMapping("/v1/project/{projectKey}/collections/{collectionId}")
@ResponseBody
public ResponseEntity<String> removeCollectionFromProject(
@PathVariable String projectKey,
@PathVariable String collectionId) {
logger.info("Removing Asset Collection to Project");
return updateArrayAttr(projectKey, "assetCollectionIds", collectionId, "$pull");
}
}

0 comments on commit b295217

Please sign in to comment.