Browse Source

implemented updating of event base data. next: update links, attachments, tags

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
main
Stephan Richter 6 months ago
parent
commit
eee672a3f6
  1. 2
      de.srsoftware.cal.db/build.gradle.kts
  2. 2
      de.srsoftware.cal.db/src/main/java/de/srsoftware/cal/db/Database.java
  3. 19
      de.srsoftware.cal.db/src/main/java/de/srsoftware/cal/db/MariaDB.java
  4. 35
      de.srsoftware.cal.web/src/main/java/de/srsoftware/cal/ApiHandler.java

2
de.srsoftware.cal.db/build.gradle.kts

@ -4,7 +4,7 @@ dependencies { @@ -4,7 +4,7 @@ dependencies {
implementation(project(":de.srsoftware.cal.api"))
implementation(project(":de.srsoftware.cal.base"))
implementation("de.srsoftware:tools.jdbc:1.1.1")
implementation("de.srsoftware:tools.jdbc:1.1.2")
implementation("de.srsoftware:tools.optionals:1.0.0")
implementation("de.srsoftware:tools.util:1.2.3")
}

2
de.srsoftware.cal.db/src/main/java/de/srsoftware/cal/db/Database.java

@ -51,4 +51,6 @@ public interface Database { @@ -51,4 +51,6 @@ public interface Database {
Result<Appointment> loadEvent(String location, LocalDateTime start);
Result<List<String>> findTags(String infix);
Result<Appointment> update(Appointment baseAppointment);
}

19
de.srsoftware.cal.db/src/main/java/de/srsoftware/cal/db/MariaDB.java

@ -289,4 +289,23 @@ public class MariaDB implements Database { @@ -289,4 +289,23 @@ public class MariaDB implements Database {
public List<Appointment> listByTags(Set<String> tags, Integer count, Integer offset) {
return List.of();
}
@Override
public Result<Appointment> update(Appointment event) {
var end = event.end().map(Timestamp::valueOf).orElse(null);
try {
Query
.update(APPOINTMENTS) //
.set(TITLE, DESCRIPTION, START, END, LOCATION, COORDS)
.where(AID, equal(event.id()))
.prepare(connection)
.apply(event.title(), event.description(), Timestamp.valueOf(event.start()), end, event.location(), event.coords().orElse(null));
// TODO: update links, attachments, tags
return Payload.of(event);
} catch (SQLException sqle) {
return Error.of("Failed to update database entry", sqle);
}
}
}

35
de.srsoftware.cal.web/src/main/java/de/srsoftware/cal/ApiHandler.java

@ -62,24 +62,31 @@ public class ApiHandler extends PathHandler { @@ -62,24 +62,31 @@ public class ApiHandler extends PathHandler {
var start = json.has(START) ? LocalDateTime.parse(json.getString(START)) : null;
if (allSet(location, start)) {
var existingAppointment = db.loadEvent(location, start).optional();
if (existingAppointment.isPresent()) return update(ex, existingAppointment.get(), json);
if (existingAppointment.isPresent()) {
var event = existingAppointment.get();
json.put(AID,event.id());
return update(ex,toEvent(json));
}
}
return createEvent(ex, json);
}
// spotless:on
private boolean createEvent(HttpExchange ex, JSONObject json) throws IOException {
private Result<BaseAppointment> toEvent(JSONObject json) {
var description = json.has(DESCRIPTION) ? nullIfEmpty(json.getString(DESCRIPTION)) : null;
var title = json.has(TITLE) ? nullIfEmpty(json.getString(TITLE)) : null;
if (title == null) return sendContent(ex, Error.of("title missing"));
if (title == null) return Error.of("title missing");
var start = json.has(START) ? nullIfEmpty(json.getString(START)) : null;
if (start == null) return sendContent(ex, Error.of("start missing"));
if (start == null) return Error.of("start missing");
var startDate = nullable(start).map(dt -> LocalDateTime.parse(dt, ISO_DATE_TIME)).orElse(null);
var end = json.has(END) ? nullIfEmpty(json.getString(END)) : null;
var endDate = nullable(end).map(dt -> LocalDateTime.parse(dt, ISO_DATE_TIME)).orElse(null);
var location = json.has(LOCATION) ? json.getString(LOCATION) : null;
if (location == null) return sendContent(ex, Error.of("location missing"));
var event = new BaseAppointment(0, title, description, startDate, endDate, location);
if (location == null) return Error.of("location missing");
var aid = json.has(AID) ? json.getLong(AID) : 0;
var event = new BaseAppointment(aid, title, description, startDate, endDate, location);
if (json.has(ATTACHMENTS)) {
json.getJSONArray(ATTACHMENTS).forEach(att -> {
Payload //
@ -96,8 +103,15 @@ public class ApiHandler extends PathHandler { @@ -96,8 +103,15 @@ public class ApiHandler extends PathHandler {
});
}
if (json.has(TAGS)) json.getJSONArray(TAGS).forEach(o -> event.tags(o.toString()));
var res = db.add(event).map(ApiHandler::toJson);
return sendContent(ex, res);
return Payload.of(event);
}
private boolean createEvent(HttpExchange ex, JSONObject json) throws IOException {
var eventRes = toEvent(json);
if (eventRes.optional().isPresent()) {
return sendContent(ex, db.add(eventRes.optional().get()).map(ApiHandler::toJson));
}
return sendContent(ex, eventRes);
}
protected static Result<Link> toLink(JSONObject json) {
@ -110,8 +124,9 @@ public class ApiHandler extends PathHandler { @@ -110,8 +124,9 @@ public class ApiHandler extends PathHandler {
}
}
private boolean update(HttpExchange ex, Appointment event, JSONObject json) throws IOException {
return sendContent(ex, Error.of("update not implemented"));
private boolean update(HttpExchange ex, Result<BaseAppointment> event) throws IOException {
if (event.optional().isPresent()) return sendContent(ex, db.update(event.optional().get()).map(ApiHandler::toJson));
return sendContent(ex, event);
}
private boolean listTags(HttpExchange ex, Map<String, String> params) throws IOException {

Loading…
Cancel
Save