Skip to content

Commit 82f0681

Browse files
author
Bradley
committed
added backend with DELETE and PUT routes
1 parent fbc4c2f commit 82f0681

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

backend/src/main/java/merhoo/backend/App.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,40 @@ public static void main(String[] args) {
8181
return gson.toJson(new StructuredResponse("ok", "" + newId, null));
8282
}
8383
});
84+
85+
// PUT route for updating a row in the DataStore. This is almost
86+
// exactly the same as POST
87+
Spark.put("/messages/:id", (request, response) -> {
88+
// If we can't get an ID or can't parse the JSON, Spark will send
89+
// a status 500
90+
int idx = Integer.parseInt(request.params("id"));
91+
SimpleRequest req = gson.fromJson(request.body(), SimpleRequest.class);
92+
// ensure status 200 OK, with a MIME type of JSON
93+
response.status(200);
94+
response.type("application/json");
95+
DataRow result = dataStore.updateOne(idx, req.mTitle, req.mMessage);
96+
if (result == null) {
97+
return gson.toJson(new StructuredResponse("error", "unable to update row " + idx, null));
98+
} else {
99+
return gson.toJson(new StructuredResponse("ok", null, result));
100+
}
101+
});
102+
103+
// DELETE route for removing a row from the DataStore
104+
Spark.delete("/messages/:id", (request, response) -> {
105+
// If we can't get an ID, Spark will send a status 500
106+
int idx = Integer.parseInt(request.params("id"));
107+
// ensure status 200 OK, with a MIME type of JSON
108+
response.status(200);
109+
response.type("application/json");
110+
// NB: we won't concern ourselves too much with the quality of the
111+
// message sent on a successful delete
112+
boolean result = dataStore.deleteOne(idx);
113+
if (!result) {
114+
return gson.toJson(new StructuredResponse("error", "unable to delete row " + idx, null));
115+
} else {
116+
return gson.toJson(new StructuredResponse("ok", null, null));
117+
}
118+
});
84119
}
85120
}

backend/src/main/java/merhoo/backend/DataStore.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,47 @@ public synchronized ArrayList<DataRowLite> readAll() {
8383
}
8484
return data;
8585
}
86+
87+
/**
88+
* Update the title and content of a row in the DataStore
89+
*
90+
* @param id The Id of the row to update
91+
* @param title The new title for the row
92+
* @param content The new content for the row
93+
* @return a copy of the data in the row, if it exists, or null otherwise
94+
*/
95+
public synchronized DataRow updateOne(int id, String title, String content) {
96+
// Do not update if we don't have valid data
97+
if (title == null || content == null)
98+
return null;
99+
// Only update if the current entry is valid (not null)
100+
if (id >= mRows.size())
101+
return null;
102+
DataRow data = mRows.get(id);
103+
if (data == null)
104+
return null;
105+
// Update and then return a copy of the data, as a DataRow
106+
data.mTitle = title;
107+
data.mContent = content;
108+
return new DataRow(data);
109+
}
110+
111+
/**
112+
* Delete a row from the DataStore
113+
*
114+
* @param id The Id of the row to delete
115+
* @return true if the row was deleted, false otherwise
116+
*/
117+
public synchronized boolean deleteOne(int id) {
118+
// Deletion fails for an invalid Id or an Id that has already been
119+
// deleted
120+
if (id >= mRows.size())
121+
return false;
122+
if (mRows.get(id) == null)
123+
return false;
124+
// Delete by setting to null, so that any Ids used by other clients
125+
// still refer to the same positions in the ArrayList.
126+
mRows.set(id, null);
127+
return true;
128+
}
86129
}

0 commit comments

Comments
 (0)