implemented storing of appointments
+ attachments next: attach links, tags Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -4,7 +4,7 @@ dependencies {
|
||||
implementation(project(":de.srsoftware.cal.api"))
|
||||
implementation(project(":de.srsoftware.cal.base"))
|
||||
|
||||
implementation("de.srsoftware:tools.jdbc:1.1.0")
|
||||
implementation("de.srsoftware:tools.jdbc:1.1.1")
|
||||
implementation("de.srsoftware:tools.optionals:1.0.0")
|
||||
implementation("de.srsoftware:tools.util:1.2.2")
|
||||
implementation("de.srsoftware:tools.util:1.2.3")
|
||||
}
|
||||
|
||||
@@ -17,9 +17,9 @@ public interface Database {
|
||||
/**
|
||||
* add an appointment to the database
|
||||
* @param appointment the appointment to store
|
||||
* @return the Database object
|
||||
* @return a clone of the provided appointment with id field set
|
||||
*/
|
||||
public Database add(Appointment appointment);
|
||||
public Result<Appointment> add(Appointment appointment);
|
||||
|
||||
/**
|
||||
* list appointments unfiltered
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
/* © SRSoftware 2024 */
|
||||
package de.srsoftware.cal.db;
|
||||
|
||||
public class Fields {
|
||||
public static final String AID = "aid";
|
||||
public static final String ALL = "*";
|
||||
public static final String COORDS = "coords";
|
||||
public static final String DESCRIPTION = "description";
|
||||
public static final String END = "end";
|
||||
public static final String KEYWORD = "keyword";
|
||||
public static final String LOCATION = "location";
|
||||
public static final String MIME = "mime";
|
||||
public static final String SLUG = "slug";
|
||||
public static final String START = "start";
|
||||
public static final String TID = "tid";
|
||||
public static final String TITLE = "title";
|
||||
public static final String UID = "uid";
|
||||
public static final String URL = "url";
|
||||
|
||||
private Fields() {
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,8 @@
|
||||
package de.srsoftware.cal.db;
|
||||
|
||||
import static de.srsoftware.cal.db.Database.slug;
|
||||
import static de.srsoftware.tools.NotImplemented.notImplemented;
|
||||
import static de.srsoftware.cal.db.Fields.*;
|
||||
import static de.srsoftware.cal.db.Fields.ALL;
|
||||
import static de.srsoftware.tools.Optionals.*;
|
||||
import static de.srsoftware.tools.Result.transform;
|
||||
import static de.srsoftware.tools.jdbc.Condition.*;
|
||||
@@ -19,6 +20,7 @@ import de.srsoftware.tools.Result;
|
||||
import de.srsoftware.tools.jdbc.Query;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.sql.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
@@ -27,17 +29,9 @@ public class MariaDB implements Database {
|
||||
private static final System.Logger LOG = System.getLogger(MariaDB.class.getSimpleName());
|
||||
private static final String ADD_SLUG = "ALTER TABLE appointments ADD slug VARCHAR(255) UNIQUE";
|
||||
private static final String APPOINTMENTS = "appointments";
|
||||
private static final String AID = "aid";
|
||||
private static final String ALL = "*";
|
||||
private static final String KEYWORD = "keyword";
|
||||
private static final String APPOINTMENT_TAGS = "appointment_tags";
|
||||
private static final String URL = "url";
|
||||
private static final String APPOINTMENT_URLS = "appointment_urls";
|
||||
private static final String DESCRIPTION = "description";
|
||||
private static final String UID = "uid";
|
||||
private static final String URLS = "urls";
|
||||
private static final String TID = "tid";
|
||||
private static final String MIME = "mime";
|
||||
private static final String APPOINTMENT_ATTACHMENTS = "appointment_attachments";
|
||||
private static final String TAGS = "tags";
|
||||
private static final String SLUG = "slug";
|
||||
@@ -106,8 +100,45 @@ public class MariaDB implements Database {
|
||||
|
||||
|
||||
@Override
|
||||
public Database add(Appointment appointment) {
|
||||
throw notImplemented(this, "add(Appointment)");
|
||||
public Result<Appointment> add(Appointment appointment) {
|
||||
try {
|
||||
ResultSet keys = Query //
|
||||
.insertInto(APPOINTMENTS, TITLE, DESCRIPTION, START, END, LOCATION, COORDS, SLUG)
|
||||
.values(appointment.title(), appointment.description(), appointment.start(), appointment.end().orElse(null), appointment.location(), appointment.coords().orElse(null), appointment.slug())
|
||||
.execute(connection)
|
||||
.getGeneratedKeys();
|
||||
Appointment saved = null;
|
||||
if (keys.next()) saved = appointment.clone(keys.getLong(1));
|
||||
keys.close();
|
||||
if (saved == null) return Error.of("Insert query did not return appointment id!");
|
||||
var attachments = saved.attachments();
|
||||
Query.InsertQuery assignQuery = null;
|
||||
|
||||
for (var attachment : attachments){
|
||||
if (assignQuery == null) assignQuery = Query.insertInto(APPOINTMENT_ATTACHMENTS,AID,UID,MIME);
|
||||
var urlId = getOrCreateUrl(attachment.url());
|
||||
if (urlId.isPresent()) assignQuery.values(saved.id(), urlId.get(),attachment.mime());
|
||||
}
|
||||
if (assignQuery != null) assignQuery.execute(connection);
|
||||
|
||||
return Payload.of(saved);
|
||||
} catch (SQLException e) {
|
||||
LOG.log(ERROR,"Failed to store appointment",e);
|
||||
return Error.of("Failed to store appointment", e);
|
||||
}
|
||||
}
|
||||
|
||||
private Optional<Long> getOrCreateUrl(URL url) throws SQLException {
|
||||
var rs = Query.select(UID).from(URLS).where(URL,equal(url.toString())).exec(connection);
|
||||
Long uid = null;
|
||||
if (rs.next()) uid = rs.getLong(1);
|
||||
rs.close();
|
||||
if (uid == null){
|
||||
rs = Query.insertInto(URLS,URL).values(url.toString()).execute(connection).getGeneratedKeys();
|
||||
if (rs.next()) uid = rs.getLong(1);
|
||||
rs.close();
|
||||
}
|
||||
return nullable(uid);
|
||||
}
|
||||
|
||||
public static Database connect(String jdbc, String user, String pass) throws SQLException {
|
||||
|
||||
Reference in New Issue
Block a user