|
|
|
@ -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 { |
|
|
|
|