4 changed files with 74 additions and 5 deletions
@ -0,0 +1,62 @@ |
|||||||
|
/* © SRSoftware 2024 */ |
||||||
|
package de.srsoftware.cal.app; |
||||||
|
|
||||||
|
import static de.srsoftware.tools.TagFilter.ofType; |
||||||
|
import static java.lang.System.Logger.Level.DEBUG; |
||||||
|
import static java.lang.System.Logger.Level.INFO; |
||||||
|
|
||||||
|
import com.sun.net.httpserver.HttpExchange; |
||||||
|
import de.srsoftware.cal.api.Appointment; |
||||||
|
import de.srsoftware.cal.db.Database; |
||||||
|
import de.srsoftware.tools.PathHandler; |
||||||
|
import de.srsoftware.tools.Tag; |
||||||
|
import java.io.IOException; |
||||||
|
import java.sql.SQLException; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
public class EventList extends PathHandler { |
||||||
|
private static final System.Logger LOG = System.getLogger(EventList.class.getSimpleName()); |
||||||
|
|
||||||
|
private final Database db; |
||||||
|
|
||||||
|
public EventList(Database db) { |
||||||
|
this.db = db; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean doGet(String path, HttpExchange ex) throws IOException { |
||||||
|
try { |
||||||
|
var events = db.list(null, null); |
||||||
|
LOG.log(DEBUG, () -> "Found %s events in database".formatted(events.size())); |
||||||
|
var scaffold = scaffold(); |
||||||
|
var body = scaffold.find(ofType("body")).getFirst(); |
||||||
|
body.add(createTable(events)); |
||||||
|
return sendContent(ex, scaffold.toString(2)); |
||||||
|
} catch (SQLException e) { |
||||||
|
return serverError(ex, "Failed to fetch list of events!"); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private Tag createTable(List<Appointment> events) { |
||||||
|
var table = Tag.of("table"); |
||||||
|
var head = Tag.of("tr"); |
||||||
|
head.add(Tag.of("th").content("Start")).add(Tag.of("th").content("Ort")).add(Tag.of("th").content("Event")); |
||||||
|
table.add(head); |
||||||
|
for (var event : events) { |
||||||
|
LOG.log(INFO, event.title()); |
||||||
|
var row = Tag.of("tr"); |
||||||
|
row.add(Tag.of("td").content(event.start().toString())).add(Tag.of("td").content(event.location())).add(Tag.of("td").content(event.title())).addTo(table); |
||||||
|
} |
||||||
|
return table; |
||||||
|
} |
||||||
|
|
||||||
|
private Tag scaffold() { |
||||||
|
var html = new Tag("html"); |
||||||
|
var head = new Tag("head") //
|
||||||
|
.add(new Tag("meta").attr("charset", "UTF-8")) |
||||||
|
.add(new Tag("title").content("OpenCloudCal")); |
||||||
|
var body = new Tag("body"); |
||||||
|
html.add(head, body); |
||||||
|
return html; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue