|
|
|
@ -53,7 +53,9 @@ public class ApiEndpoint extends PathHandler {
@@ -53,7 +53,9 @@ public class ApiEndpoint extends PathHandler {
|
|
|
|
|
var title = getHeader(ex, TITLE); |
|
|
|
|
if (title.isEmpty()) return HttpError.of(400,"Missing title header"); |
|
|
|
|
if (!title.get().equals(event.title())) return HttpError.of(400,"Title mismatch!"); |
|
|
|
|
return db.add(opt.get()).map(ApiEndpoint::toJson); |
|
|
|
|
String hostname = hostname(ex); |
|
|
|
|
String urlTemplate = hostname+"/static/event?id={}"; |
|
|
|
|
return db.add(opt.get()).map(ev -> toJson(ev,urlTemplate)); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
return error(e, "Failed to read event data from request body"); |
|
|
|
|
} |
|
|
|
@ -96,10 +98,10 @@ public class ApiEndpoint extends PathHandler {
@@ -96,10 +98,10 @@ public class ApiEndpoint extends PathHandler {
|
|
|
|
|
String prodId = "OpenCloudCal@"+hostname.split("://",2)[1]; |
|
|
|
|
ex.getResponseHeaders().add("Access-Control-Allow-Origin","*"); |
|
|
|
|
return switch (path) { |
|
|
|
|
case "/event" -> sendContent(ex,getEvent(ex).map(ApiEndpoint::toJson).map(ApiEndpoint::httpError)); |
|
|
|
|
case "/event" -> sendContent(ex,getEvent(ex).map(event -> toJson(event,urlTemplate)).map(ApiEndpoint::httpError)); |
|
|
|
|
case "/event/ical"-> sendContent(ex,getEvent(ex).map(event -> toIcal(event, hostname, urlTemplate)).map(ical -> Util.wrapIcal(ical,prodId)).map(ApiEndpoint::httpError)); |
|
|
|
|
case "/events/ical"-> sendContent(ex,eventList(ex).map(list -> toIcalList(list, hostname, urlTemplate)).map(ical -> Util.wrapIcal(ical,prodId)).map(ApiEndpoint::httpError)); |
|
|
|
|
case "/events/json" -> sendContent(ex,eventList(ex).map(ApiEndpoint::toJsonList).map(ApiEndpoint::httpError)); |
|
|
|
|
case "/events/json" -> sendContent(ex,eventList(ex).map(list -> toJsonList(list,urlTemplate)).map(ApiEndpoint::httpError)); |
|
|
|
|
case "/tags" -> listTags(ex); |
|
|
|
|
default -> unknownPath(ex, path); |
|
|
|
|
}; |
|
|
|
@ -282,15 +284,15 @@ public class ApiEndpoint extends PathHandler {
@@ -282,15 +284,15 @@ public class ApiEndpoint extends PathHandler {
|
|
|
|
|
return Payload.of(list); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static Result<JSONObject> toJson(Result<? extends Appointment> res) { |
|
|
|
|
private static Result<JSONObject> toJson(Result<? extends Appointment> res, String urlTemplate) { |
|
|
|
|
var opt = res.optional(); |
|
|
|
|
return opt.isEmpty() ? transform(res) : Payload.of(opt.get().json()); |
|
|
|
|
return opt.isEmpty() ? transform(res) : Payload.of(opt.get().json(urlTemplate)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static Result<List<JSONObject>> toJsonList(Result<List<Appointment>> res) { |
|
|
|
|
private static Result<List<JSONObject>> toJsonList(Result<List<Appointment>> res, String urlTemplate) { |
|
|
|
|
var opt = res.optional(); |
|
|
|
|
if (opt.isEmpty()) return transform(res); |
|
|
|
|
var list = opt.get().stream().map(Appointment::json).toList(); |
|
|
|
|
var list = opt.get().stream().map(event -> event.json(urlTemplate)).toList(); |
|
|
|
|
return Payload.of(list); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -318,7 +320,9 @@ public class ApiEndpoint extends PathHandler {
@@ -318,7 +320,9 @@ public class ApiEndpoint extends PathHandler {
|
|
|
|
|
var title = getHeader(ex, TITLE); |
|
|
|
|
if (title.isEmpty()) return HttpError.of(400,"Missing title header"); |
|
|
|
|
if (!title.get().equals(event.title())) return HttpError.of(400,"Title mismatch!"); |
|
|
|
|
return db.update(opt.get()).map(ApiEndpoint::toJson); |
|
|
|
|
String hostname = hostname(ex); |
|
|
|
|
String urlTemplate = hostname+"/static/event?id={}"; |
|
|
|
|
return db.update(opt.get()).map(ev -> toJson(ev,urlTemplate)); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
return error(e, "Failed to read event data from request body"); |
|
|
|
|
} |
|
|
|
|