first version that creates event table
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -7,5 +7,8 @@ dependencies {
|
|||||||
|
|
||||||
implementation("de.srsoftware:configuration.api:1.0.0")
|
implementation("de.srsoftware:configuration.api:1.0.0")
|
||||||
implementation("de.srsoftware:configuration.json:1.0.0")
|
implementation("de.srsoftware:configuration.json:1.0.0")
|
||||||
|
implementation("de.srsoftware:tools.http:1.0.1")
|
||||||
|
implementation("de.srsoftware:tools.logging:1.0.1")
|
||||||
implementation("de.srsoftware:tools.util:1.2.1")
|
implementation("de.srsoftware:tools.util:1.2.1")
|
||||||
|
implementation("de.srsoftware:tools.web:1.3.4")
|
||||||
implementation("com.mysql:mysql-connector-j:9.1.0")}
|
implementation("com.mysql:mysql-connector-j:9.1.0")}
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
/* © SRSoftware 2024 */
|
/* © SRSoftware 2024 */
|
||||||
package de.srsoftware.cal.app;
|
package de.srsoftware.cal.app;
|
||||||
|
|
||||||
|
import com.sun.net.httpserver.HttpServer;
|
||||||
import de.srsoftware.cal.db.Database;
|
import de.srsoftware.cal.db.Database;
|
||||||
import de.srsoftware.cal.db.MariaDB;
|
import de.srsoftware.cal.db.MariaDB;
|
||||||
import de.srsoftware.configuration.Configuration;
|
import de.srsoftware.configuration.Configuration;
|
||||||
import de.srsoftware.configuration.JsonConfig;
|
import de.srsoftware.configuration.JsonConfig;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@@ -39,9 +41,11 @@ public class Application {
|
|||||||
* @param args default
|
* @param args default
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) throws NoSuchAlgorithmException, IOException, SQLException {
|
public static void main(String[] args) throws NoSuchAlgorithmException, IOException, SQLException {
|
||||||
JsonConfig jsonConfig = new JsonConfig("OpenCloudCal");
|
JsonConfig jsonConfig = new JsonConfig("OpenCloudCal");
|
||||||
var db = connect(jsonConfig);
|
var db = connect(jsonConfig);
|
||||||
var appointments = db.list(null, null);
|
|
||||||
for (var event : appointments) System.out.println(event);
|
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
|
||||||
|
new EventList(db).bindPath("/").on(server);
|
||||||
|
server.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -98,7 +98,7 @@ public class MariaDB implements Database {
|
|||||||
@Override
|
@Override
|
||||||
public List<Appointment> list(Integer count, Integer offset) throws SQLException {
|
public List<Appointment> list(Integer count, Integer offset) throws SQLException {
|
||||||
var list = new ArrayList<Appointment>();
|
var list = new ArrayList<Appointment>();
|
||||||
var results = Query.of(SELECT_APPOINTMENTS).execute(connection);
|
var results = Query.of(SELECT_APPOINTMENTS).orderBy("start").execute(connection);
|
||||||
while (results.next()) {
|
while (results.next()) {
|
||||||
var id = results.getInt("aid");
|
var id = results.getInt("aid");
|
||||||
var title = results.getString("title");
|
var title = results.getString("title");
|
||||||
|
|||||||
Reference in New Issue
Block a user