Commit 6fa5248e authored by Øyvin Halfdan Thuv's avatar Øyvin Halfdan Thuv
Browse files

Store incidents in memory.

parent 8e7fe3df
......@@ -3,6 +3,7 @@ package statelessws;
import java.util.ArrayList;
import java.util.Date;
import java.util.UUID;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -10,10 +11,16 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
@RestController
public class IncidentController {
/*
* A place to "store" incidents
*/
final private ArrayList<Incident> incidents = new ArrayList<Incident>();
public IncidentController () {
}
......@@ -23,6 +30,10 @@ public class IncidentController {
*/
@RequestMapping(value="/incidents/{id}", method=RequestMethod.DELETE)
public boolean deleteIncident(@PathVariable("id") UUID id) {
for (Incident i : incidents) {
if (i.getId().equals(id))
incidents.remove(i);
}
return true;
}
......@@ -31,7 +42,6 @@ public class IncidentController {
*/
@RequestMapping("/incidents")
public ArrayList<Incident> incidents() {
ArrayList<Incident> incidents = new ArrayList<Incident>();
return incidents;
}
......@@ -40,7 +50,12 @@ public class IncidentController {
*/
@RequestMapping(value="/incidents/{id}", method=RequestMethod.GET)
public Incident readIncident(@PathVariable("id") UUID id) {
return new Incident(id, "name");
for (Incident i : incidents) {
if (i.getId().equals(id))
return i;
}
throw new ResponseStatusException
(HttpStatus.NOT_FOUND, "No such incident");
}
/*
......@@ -52,6 +67,7 @@ public class IncidentController {
Date timestamp = new Date();
i.setId(id);
i.setTimestamp(timestamp);
incidents.add(i);
return i;
}
......@@ -61,7 +77,11 @@ public class IncidentController {
@RequestMapping(value="/incidents/{id}", method=RequestMethod.PUT)
public Incident updateIncident(@PathVariable("id") UUID id,
@RequestBody Incident i) {
i.setId(id);
for (Incident oldI : incidents) {
if (oldI.getId().equals(id))
incidents.remove(oldI);
}
incidents.add(i);
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