Commit 73bdb925 authored by Øyvin Halfdan Thuv's avatar Øyvin Halfdan Thuv
Browse files

Added DELETE, GET and PUT methods.

parent 4df6e7f2
......@@ -8,6 +8,7 @@ import com.datastax.driver.core.Session;
import java.util.concurrent.atomic.AtomicLong;
import java.util.ArrayList;
import java.util.UUID;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
......@@ -20,32 +21,48 @@ public class IncidentController {
Cluster cluster;
Session session;
PreparedStatement insertStmt;
PreparedStatement deleteStmt;
PreparedStatement createStmt;
PreparedStatement readStmt;
PreparedStatement updateStmt;
/*
* Initialize controller with database connection.
*
* CREATE KEYSPACE storage WITH REPLICATION = {
* Prerequisite for database:
*
* CREATE KEYSPACE storage WITH REPLICATION = {
* 'class': 'SimpleStrategy',
* 'replication_factor': 1
* };
* };
*
* USE storage;
* CREATE TABLE incident ( id uuid PRIMARY KEY, name text );
*/
public IncidentController () {
cluster = Cluster.builder()
.addContactPoint("127.0.0.1").build();
session = cluster.connect("storage");
insertStmt = session
deleteStmt = session
.prepare("DELETE FROM incident WHERE id = ?");
createStmt = session
.prepare("INSERT INTO incident (id, name) VALUES (?, ?)");
readStmt = session
.prepare("SELECT * FROM incident WHERE id = ?");
updateStmt = session
.prepare("UPDATE incident SET name = ? WHERE id = ?");
}
/*
* Delete an incident from the database.
*/
@RequestMapping(value="/incidents/{id}", method=RequestMethod.DELETE)
public boolean deleteIncident(@PathVariable("id") UUID id) {
return session.execute(deleteStmt.bind(id)).wasApplied();
}
/*
* Get all incidents from the database.
*
* Prerequisite for DB:
*
* USE storage;
* CREATE TABLE incident ( id uuid PRIMARY KEY, name text );
*/
@RequestMapping("/incidents")
public ArrayList<Incident> incidents() {
......@@ -59,18 +76,35 @@ public class IncidentController {
return incidents;
}
/*
* Get an incident from the database
*/
@RequestMapping(value="/incidents/{id}", method=RequestMethod.GET)
public Incident readIncident(@PathVariable("id") UUID id) {
ResultSet rows =
session.execute(readStmt.bind(id));
Row row = rows.one();
return new Incident(row.getUUID("id"), row.getString("name"));
}
/*
* Create a new incident and store it in the database.
*
* Prerequisite for DB:
*
* USE storage;
* CREATE TABLE incident ( id uuid PRIMARY KEY, name text );
*/
@RequestMapping(value="/incidents", method=RequestMethod.POST)
public Incident newIncident(@RequestBody Incident i) {
public Incident createIncident(@RequestBody Incident i) {
UUID id = UUID.randomUUID();
session.execute(insertStmt.bind(i.getId(), i.getName()));
session.execute(createStmt.bind(i.getId(), i.getName()));
i.setId(id);
return i;
}
/*
* Store (update) an incident in the database.
*/
@RequestMapping(value="/incidents/{id}", method=RequestMethod.PUT)
public Incident updateIncident(@PathVariable("id") UUID id,
@RequestBody Incident i) {
session.execute(updateStmt.bind(i.getName(), id));
i.setId(id);
return i;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment