implemented storing of appointments
+ attachments next: attach links, tags Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
/* © SRSoftware 2024 */
|
||||
package de.srsoftware.cal;
|
||||
|
||||
import static de.srsoftware.cal.db.Fields.*;
|
||||
import static de.srsoftware.tools.Optionals.nullIfEmpty;
|
||||
import static de.srsoftware.tools.Optionals.nullable;
|
||||
import static java.lang.System.*;
|
||||
@@ -9,6 +10,7 @@ import static java.time.format.DateTimeFormatter.ISO_DATE_TIME;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import de.srsoftware.cal.api.Appointment;
|
||||
import de.srsoftware.cal.api.Link;
|
||||
import de.srsoftware.cal.db.Database;
|
||||
import de.srsoftware.tools.Error;
|
||||
import de.srsoftware.tools.PathHandler;
|
||||
@@ -25,12 +27,8 @@ import org.json.JSONObject;
|
||||
|
||||
public class ApiHandler extends PathHandler {
|
||||
private static final Logger LOG = getLogger(ApiHandler.class.getSimpleName());
|
||||
private static final String SLUG = "slug";
|
||||
private static final String DESCRIPTION = "description";
|
||||
private static final String TITLE = "title";
|
||||
private static final String START = "start";
|
||||
private static final String END = "end";
|
||||
private static final String LOCATION = "location";
|
||||
private static final String ATTACHMENTS = "attachments";
|
||||
private static final String LINKS = "links";
|
||||
private final Database db;
|
||||
|
||||
public ApiHandler(Database db) {
|
||||
@@ -89,8 +87,33 @@ public class ApiHandler extends PathHandler {
|
||||
var serverSlug = Database.slug(location, startDate);
|
||||
if (!serverSlug.equals(clientSlug)) return sendContent(ex, Error.of("Slug mismatch!"));
|
||||
var event = new BaseAppointment(0, title, description, startDate, endDate, location, serverSlug);
|
||||
db.add(event);
|
||||
return sendContent(ex, Error.of("createEvent not implemented"));
|
||||
if (json.has(ATTACHMENTS)) {
|
||||
json.getJSONArray(ATTACHMENTS).forEach(att -> {
|
||||
Payload //
|
||||
.of(att.toString())
|
||||
.map(BaseImporter::url)
|
||||
.map(BaseImporter::toAttachment)
|
||||
.optional()
|
||||
.ifPresent(event::add);
|
||||
});
|
||||
}
|
||||
if (json.has(LINKS)) {
|
||||
json.getJSONArray(LINKS).forEach(o -> {
|
||||
if (o instanceof JSONObject j) toLink(j).optional().ifPresent(event::addLinks);
|
||||
});
|
||||
}
|
||||
var res = db.add(event).map(ApiHandler::toJson);
|
||||
return sendContent(ex, res);
|
||||
}
|
||||
|
||||
protected static Result<Link> toLink(JSONObject json) {
|
||||
try {
|
||||
var description = json.getString(DESCRIPTION);
|
||||
return Payload.of(json.getString(URL)).map(BaseImporter::url).map(url -> BaseImporter.link(url, description));
|
||||
|
||||
} catch (Exception e) {
|
||||
return Error.of("Failed to create link from %s".formatted(json), e);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean update(HttpExchange ex, Appointment event, JSONObject json) throws IOException {
|
||||
|
||||
@@ -140,6 +140,8 @@ async function slug(start,location){
|
||||
}
|
||||
|
||||
async function saveEvent(){
|
||||
element('save').toggleAttribute("disabled");
|
||||
|
||||
var location = element('location').value;
|
||||
var start = element('start').value;
|
||||
var slugVal = await slug(start,location);
|
||||
|
||||
Reference in New Issue
Block a user