preparing save/update of appointment
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -2,7 +2,9 @@
|
||||
package de.srsoftware.cal.db;
|
||||
|
||||
import de.srsoftware.cal.api.Appointment;
|
||||
import de.srsoftware.tools.Calc;
|
||||
import de.srsoftware.tools.Result;
|
||||
import de.srsoftware.tools.Strings;
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
@@ -48,5 +50,11 @@ public interface Database {
|
||||
|
||||
Result<Appointment> loadEvent(long id);
|
||||
|
||||
Result<Appointment> loadEvent(String slug);
|
||||
|
||||
Result<List<String>> findTags(String infix);
|
||||
|
||||
public static String slug(String location, LocalDateTime start) {
|
||||
return Calc.sha256(start + "@" + location).map(Strings::base64).orElse(null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
/* © SRSoftware 2024 */
|
||||
package de.srsoftware.cal.db;
|
||||
|
||||
import static de.srsoftware.cal.db.Database.slug;
|
||||
import static de.srsoftware.tools.NotImplemented.notImplemented;
|
||||
import static de.srsoftware.tools.Optionals.*;
|
||||
import static de.srsoftware.tools.Result.transform;
|
||||
import static de.srsoftware.tools.Strings.camelCase;
|
||||
import static de.srsoftware.tools.jdbc.Condition.*;
|
||||
import static de.srsoftware.tools.jdbc.Query.MARK;
|
||||
import static java.lang.System.Logger.Level.*;
|
||||
@@ -12,7 +13,6 @@ import de.srsoftware.cal.BaseAppointment;
|
||||
import de.srsoftware.cal.api.Appointment;
|
||||
import de.srsoftware.cal.api.Attachment;
|
||||
import de.srsoftware.cal.api.Link;
|
||||
import de.srsoftware.tools.Calc;
|
||||
import de.srsoftware.tools.Error;
|
||||
import de.srsoftware.tools.Payload;
|
||||
import de.srsoftware.tools.Result;
|
||||
@@ -40,6 +40,7 @@ public class MariaDB implements Database {
|
||||
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";
|
||||
private static Connection connection;
|
||||
|
||||
private MariaDB(Connection conn) throws SQLException {
|
||||
@@ -82,10 +83,9 @@ public class MariaDB implements Database {
|
||||
LOG.log(TRACE, () -> "%s: %s".formatted(id, title));
|
||||
var descr = rs.getString("description");
|
||||
if (allEmpty(title, descr)) continue;
|
||||
var start = nullable(rs.getTimestamp("start"));
|
||||
var start = nullable(rs.getTimestamp("start")).map(Timestamp::toLocalDateTime);
|
||||
if (start.isEmpty()) continue;
|
||||
var slug = "%s@%s".formatted(start.get().toLocalDateTime(), camelCase(location.get().replace(",", "")));
|
||||
if (slug.length() > 250) slug = slug.substring(0, 250);
|
||||
var slug = slug(location.get(), start.get());
|
||||
slugMap.put(id, slug);
|
||||
}
|
||||
rs.close();
|
||||
@@ -107,7 +107,7 @@ public class MariaDB implements Database {
|
||||
|
||||
@Override
|
||||
public Database add(Appointment appointment) {
|
||||
return null;
|
||||
throw notImplemented(this, "add(Appointment)");
|
||||
}
|
||||
|
||||
public static Database connect(String jdbc, String user, String pass) throws SQLException {
|
||||
@@ -148,6 +148,18 @@ public class MariaDB implements Database {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Appointment> loadEvent(String slug) {
|
||||
try {
|
||||
var rs = Query.select(ALL).from(APPOINTMENTS).where(SLUG, equal(slug)).exec(connection);
|
||||
Result<Appointment> result = rs.next() ? createAppointmentOf(rs).map(MariaDB::loadExtra) : Error.format("Failed to find appointment with slug %s", slug);
|
||||
rs.close();
|
||||
return result;
|
||||
} catch (SQLException e) {
|
||||
return Error.of("Failed to load appointment with slug = %s".formatted(slug), e);
|
||||
}
|
||||
}
|
||||
|
||||
private static Result<Appointment> loadExtra(Result<BaseAppointment> res) {
|
||||
return loadTags(res).map(MariaDB::loadLinks).map(MariaDB::loadAttachments);
|
||||
}
|
||||
@@ -221,7 +233,7 @@ public class MariaDB implements Database {
|
||||
var end = nullable(results.getTimestamp("end")).map(Timestamp::toLocalDateTime).orElse(null);
|
||||
var location = results.getString("location");
|
||||
var slug = results.getString("slug");
|
||||
if (slug == null) slug = Calc.hash(start + "@" + location).orElse(null);
|
||||
if (slug == null) slug = slug(location, start);
|
||||
var appointment = new BaseAppointment(id, title, description, start, end, location, slug);
|
||||
try {
|
||||
var tags = nullIfEmpty(results.getString("tags"));
|
||||
|
||||
Reference in New Issue
Block a user