Skip to content

Commit

Permalink
Making REST endpoints exhibit more expected and standard behavior
Browse files Browse the repository at this point in the history
Let's have the POST and PUTs return the object they created instead of an HTTP status, eh?

Signed-off-by: Ryan Cloherty <cloherty.ryan@gmail.com>
  • Loading branch information
IncPlusPlus committed Jun 28, 2019
1 parent eb3e287 commit 09d1964
Showing 1 changed file with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.incplusplus.thermostat.nodeObjects;

import com.mongodb.MongoClient;
import io.github.incplusplus.thermostat.exceptions.NodeNotFoundException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.mongodb.core.MongoOperations;
Expand All @@ -10,9 +11,11 @@
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

Expand All @@ -27,23 +30,27 @@ public class NodeController

@RequestMapping(method = RequestMethod.POST, value = "/addNode", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public HttpStatus addNode(@RequestParam(name = "name", required = true) String name,
public Node addNode(@RequestParam(name = "name", required = true) String name,
@RequestParam(name = "heatingSupported", required = false, defaultValue = "false") boolean heatingSupported,
@RequestParam(name = "airConditioningSupported", required = false, defaultValue = "false") boolean airConditioningSupported,
@RequestParam(name = "fanSupported", required = false, defaultValue = "false") boolean fanSupported)
@RequestParam(name = "fanSupported", required = false, defaultValue = "false") boolean fanSupported,
HttpServletResponse response)
{
Node n = new Node(name, heatingSupported, airConditioningSupported, fanSupported);
mongoOps.insert(n);
log.info("Inserted new Node: " + n);
return HttpStatus.CREATED;
// TODO add check to see if this Node already exists and return a CONFLICT if it does
// TODO maybe add check to see if object made it into Mongo

return n;
}

@RequestMapping(value = "/getNodes", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<Node> getNodes()
{

log.info("findAll entered...");
log.info("Listing all nodes...");

return mongoOps.findAll(Node.class);
}
Expand All @@ -57,8 +64,10 @@ public Node getNode(@PathVariable("id") String id)

Query query = new Query(Criteria.where("id").is(id));
Node foundNode = mongoOps.findOne(query, Node.class);
System.out.println(foundNode);
return foundNode;
// System.out.println(foundNode);
if(foundNode != null)
{return foundNode;}
throw new NodeNotFoundException("Node with id '"+id+"' was not found.");
}

@RequestMapping(method = RequestMethod.PUT, value = "/{id:[\\da-f]+}/updateStatus", produces = MediaType.APPLICATION_JSON_VALUE)
Expand All @@ -82,23 +91,25 @@ public HttpStatus updateStatus(@PathVariable("id") String id,
mongoOps.save(foundNode);
return HttpStatus.OK;
}
return HttpStatus.NOT_FOUND;
throw new NodeNotFoundException("Node with id '"+id+"' was not found.");
}

@RequestMapping(method = RequestMethod.DELETE, value = "/{id:[\\da-f]+}", produces = MediaType.APPLICATION_JSON_VALUE)
@RequestMapping(method = RequestMethod.DELETE, value = "/{id:[\\da-f]+}")
@ResponseBody
public HttpStatus deleteNode(@PathVariable("id") String id)
public void deleteNode(@PathVariable("id") String id, HttpServletResponse response)
{

log.info("updateStatus entered: id= " + id);

Query query = new Query(Criteria.where("id").is(id));
Node foundNode = mongoOps.findOne(query, Node.class);
if (foundNode != null)
{
mongoOps.remove(foundNode);
return HttpStatus.OK;
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
}
else
{
throw new NodeNotFoundException("Node with id '"+id+"' was not found.");
}
return HttpStatus.NOT_FOUND;
}
}

0 comments on commit 09d1964

Please sign in to comment.