Commit 99668adb authored by Øyvin Halfdan Thuv's avatar Øyvin Halfdan Thuv
Browse files

Purging DB code from master to simplify.

parent 09bebb5b
......@@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<version>2.0.5.RELEASE</version>
</parent>
<dependencies>
......@@ -31,11 +31,6 @@
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>3.1.1</version>
</dependency>
</dependencies>
<properties>
......
package statelessws;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import java.util.concurrent.atomic.AtomicLong;
import java.util.ArrayList;
import java.util.Date;
import java.util.UUID;
......@@ -20,78 +14,37 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
public class IncidentController {
Cluster cluster;
Session session;
PreparedStatement deleteStmt;
PreparedStatement createStmt;
PreparedStatement readStmt;
PreparedStatement updateStmt;
/*
* Initialize controller with database connection.
*
* 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");
deleteStmt = session
.prepare("DELETE FROM incident WHERE id = ?");
createStmt = session
.prepare("INSERT INTO incident" +
" (id, name, timestamp)" +
" VALUES (?, ?, ?)");
readStmt = session
.prepare("SELECT * FROM incident WHERE id = ?");
updateStmt = session
.prepare("UPDATE incident SET name = ? WHERE id = ?");
}
/*
* Delete an incident from the database.
* Delete an incident.
*/
@RequestMapping(value="/incidents/{id}", method=RequestMethod.DELETE)
public boolean deleteIncident(@PathVariable("id") UUID id) {
return session.execute(deleteStmt.bind(id)).wasApplied();
return true;
}
/*
* Get all incidents from the database.
* Get all incidents.
*/
@RequestMapping("/incidents")
public ArrayList<Incident> incidents() {
ResultSet rows =
session.execute("SELECT * FROM incident");
ArrayList<Incident> incidents = new ArrayList<Incident>();
for (Row row : rows) {
incidents.add(new Incident(row.getUUID("id"),
row.getString("name")));
}
return incidents;
}
/*
* Get an incident from the database
* Get a specific incident.
*/
@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"));
return new Incident(id, "name");
}
/*
* Create a new incident and store it in the database.
* Create a new incident.
*/
@RequestMapping(value="/incidents", method=RequestMethod.POST)
public Incident createIncident(@RequestBody Incident i) {
......@@ -99,18 +52,15 @@ public class IncidentController {
Date timestamp = new Date();
i.setId(id);
i.setTimestamp(timestamp);
session.execute(createStmt.bind
(i.getId(), i.getName(), i.getTimestamp()));
return i;
}
/*
* Store (update) an incident in the database.
* Update an incident.
*/
@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