working on event detail page
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -1,81 +0,0 @@
|
||||
/* © SRSoftware 2024 */
|
||||
package de.srsoftware.cal.app;
|
||||
|
||||
import static de.srsoftware.tools.Optionals.nullable;
|
||||
import static java.lang.System.Logger;
|
||||
import static java.lang.System.Logger.Level.WARNING;
|
||||
import static java.lang.System.getLogger;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import de.srsoftware.cal.api.Appointment;
|
||||
import de.srsoftware.cal.db.Database;
|
||||
import de.srsoftware.tools.Error;
|
||||
import de.srsoftware.tools.PathHandler;
|
||||
import de.srsoftware.tools.Payload;
|
||||
import de.srsoftware.tools.Result;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Map;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class ApiHandler extends PathHandler {
|
||||
private static final Logger LOG = getLogger(ApiHandler.class.getSimpleName());
|
||||
private final Database db;
|
||||
|
||||
public ApiHandler(Database db) {
|
||||
this.db = db;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doGet(String path, HttpExchange ex) throws IOException {
|
||||
var params = queryParam(ex);
|
||||
return switch (path) {
|
||||
case "/event" -> loadEvent(ex,params);
|
||||
case "/events/list" -> listEvents(ex,params);
|
||||
default -> notFound(ex);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
private boolean listEvents(HttpExchange ex, Map<String, String> params) throws IOException {
|
||||
var start = nullable(params.get("start")).map(ApiHandler::toLocalDateTime).orElse(null);
|
||||
var end = nullable(params.get("end")).map(ApiHandler::toLocalDateTime).orElse(null);
|
||||
try {
|
||||
return sendContent(ex,db.list(start, end).stream().map(Appointment::json).toList());
|
||||
} catch (SQLException e) {
|
||||
LOG.log(WARNING,"Failed to fetch events (start = {0}, end = {1}!",start,end,e);
|
||||
}
|
||||
return notFound(ex);
|
||||
}
|
||||
|
||||
private boolean loadEvent(HttpExchange ex, Map<String, String> params) throws IOException {
|
||||
var id = params.get("id");
|
||||
if (id != null) try {
|
||||
return sendContent(ex,db.loadEvent(Long.parseLong(id)).map(ApiHandler::toJson));
|
||||
} catch (NumberFormatException | IOException nfe){
|
||||
return sendContent(ex, Error.format("%s is not a numeric event id!",id));
|
||||
}
|
||||
return sendContent(ex,Error.of("ID missing"));
|
||||
}
|
||||
|
||||
private static Result<JSONObject> toJson(Result<Appointment> appointmentResult) {
|
||||
var opt = appointmentResult.optional();
|
||||
return opt.isEmpty() ? transform(appointmentResult) :
|
||||
Payload.of(opt.get().json());
|
||||
}
|
||||
|
||||
private static <T> Result<T> transform(Result<?> res) {
|
||||
return res instanceof Error<?> err ? err.transform() : Error.format("Invalid parameter: %s", res.getClass().getSimpleName());
|
||||
}
|
||||
|
||||
private static LocalDateTime toLocalDateTime(String dateString) {
|
||||
try {
|
||||
return LocalDate.parse(dateString + "-01", DateTimeFormatter.ISO_LOCAL_DATE).atTime(0, 0);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,9 @@ package de.srsoftware.cal.app;
|
||||
import static java.lang.System.Logger.Level.*;
|
||||
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import de.srsoftware.cal.ApiHandler;
|
||||
import de.srsoftware.cal.IndexHandler;
|
||||
import de.srsoftware.cal.StaticHandler;
|
||||
import de.srsoftware.cal.db.Database;
|
||||
import de.srsoftware.cal.db.MariaDB;
|
||||
import de.srsoftware.configuration.Configuration;
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
/* © SRSoftware 2024 */
|
||||
package de.srsoftware.cal.app;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import de.srsoftware.tools.PathHandler;
|
||||
import java.io.IOException;
|
||||
|
||||
public class IndexHandler extends PathHandler {
|
||||
PathHandler staticPages;
|
||||
public IndexHandler(PathHandler staticPages) {
|
||||
this.staticPages = staticPages;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doGet(String path, HttpExchange ex) throws IOException {
|
||||
switch (path) {
|
||||
case "/":
|
||||
return staticPages.doGet("/index", ex);
|
||||
}
|
||||
return super.doGet(path, ex);
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
/* © SRSoftware 2024 */
|
||||
package de.srsoftware.cal.app;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import de.srsoftware.tools.PathHandler;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Optional;
|
||||
|
||||
|
||||
public class StaticHandler extends PathHandler {
|
||||
private final Optional<String> staticPath;
|
||||
|
||||
public StaticHandler(Optional<String> staticPath) {
|
||||
this.staticPath = staticPath;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doGet(String path, HttpExchange ex) throws IOException {
|
||||
if (!path.contains(".")) path += ".html";
|
||||
if (path.startsWith("/")) path = path.substring(1);
|
||||
var url = getClass().getClassLoader().getResource(path);
|
||||
if (staticPath.isPresent()) {
|
||||
var file = Path.of(staticPath.get()).resolve(path).toFile();
|
||||
if (file.exists() && file.isFile()) url = file.toURI().toURL();
|
||||
}
|
||||
if (url == null) return notFound(ex);
|
||||
var conn = url.openConnection();
|
||||
var mime = conn.getContentType();
|
||||
try (var input = conn.getInputStream()) {
|
||||
var bos = new ByteArrayOutputStream();
|
||||
input.transferTo(bos);
|
||||
ex.getResponseHeaders().add(CONTENT_TYPE, mime);
|
||||
return sendContent(ex, bos.toByteArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user