completed CRUD operations
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.2")
|
||||
implementation("de.srsoftware:tools.jdbc:1.1.3")
|
||||
implementation("de.srsoftware:tools.optionals:1.0.0")
|
||||
implementation("de.srsoftware:tools.util:1.2.3")
|
||||
}
|
||||
|
||||
@@ -2,10 +2,13 @@
|
||||
package de.srsoftware.cal.db;
|
||||
|
||||
import de.srsoftware.cal.api.Appointment;
|
||||
import de.srsoftware.tools.Error;
|
||||
import de.srsoftware.tools.Result;
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -19,6 +22,8 @@ public interface Database {
|
||||
*/
|
||||
public Result<Appointment> add(Appointment appointment);
|
||||
|
||||
Result<List<String>> findTags(String infix);
|
||||
|
||||
/**
|
||||
* list appointments unfiltered
|
||||
* @param count the maximum number of appointments to return
|
||||
@@ -50,7 +55,7 @@ public interface Database {
|
||||
|
||||
Result<Appointment> loadEvent(String location, LocalDateTime start);
|
||||
|
||||
Result<List<String>> findTags(String infix);
|
||||
Result<Long> removeAppointment(long id);
|
||||
|
||||
Result<Appointment> update(Appointment baseAppointment);
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import static de.srsoftware.tools.jdbc.Query.*;
|
||||
import static java.lang.System.Logger.Level.*;
|
||||
|
||||
import de.srsoftware.cal.BaseAppointment;
|
||||
import de.srsoftware.cal.Util;
|
||||
import de.srsoftware.cal.api.Appointment;
|
||||
import de.srsoftware.cal.api.Attachment;
|
||||
import de.srsoftware.cal.api.Link;
|
||||
@@ -251,17 +252,25 @@ public class MariaDB implements Database {
|
||||
|
||||
private Result<BaseAppointment> createAppointmentOf(ResultSet results) throws SQLException {
|
||||
var id = results.getInt(AID);
|
||||
var title = results.getString("title");
|
||||
var description = results.getString("description");
|
||||
var title = results.getString(TITLE);
|
||||
var description = results.getString(DESCRIPTION);
|
||||
if (allEmpty(title, description)) return Error.format("Title and Description of appointment %s are empty", id);
|
||||
var start = results.getTimestamp("start").toLocalDateTime();
|
||||
var end = nullable(results.getTimestamp("end")).map(Timestamp::toLocalDateTime).orElse(null);
|
||||
var location = results.getString("location");
|
||||
var start = results.getTimestamp(START).toLocalDateTime();
|
||||
var end = nullable(results.getTimestamp(END)).map(Timestamp::toLocalDateTime).orElse(null);
|
||||
var location = results.getString(LOCATION);
|
||||
var appointment = new BaseAppointment(id, title, description, start, end, location);
|
||||
|
||||
try {
|
||||
Util.extractCoords(results.getString(COORDS)).optional().ifPresent(appointment::coords);
|
||||
} catch (SQLException e) {
|
||||
LOG.log(WARNING, "Failed to read coordinates from database!");
|
||||
}
|
||||
|
||||
try {
|
||||
var tags = nullIfEmpty(results.getString("tags"));
|
||||
if (tags != null) appointment.tags(tags.split(","));
|
||||
} catch (SQLException e) {
|
||||
LOG.log(WARNING, "Failed to read tags from database!");
|
||||
}
|
||||
return Payload.of(appointment);
|
||||
}
|
||||
@@ -290,6 +299,19 @@ public class MariaDB implements Database {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Long> removeAppointment(long id) {
|
||||
try {
|
||||
Query.delete().from(APPOINTMENTS).where(AID,equal(id)).execute(connection);
|
||||
Query.delete().from(APPOINTMENT_TAGS).where(AID,equal(id)).execute(connection);
|
||||
Query.delete().from(APPOINTMENT_ATTACHMENTS).where(AID,equal(id)).execute(connection);
|
||||
Query.delete().from(APPOINTMENT_URLS).where(AID,equal(id)).execute(connection);
|
||||
return Payload.of(id);
|
||||
} catch (SQLException e) {
|
||||
return Error.of("Failed to delete event %s".formatted(id),e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Appointment> update(Appointment event) {
|
||||
var end = event.end().map(Timestamp::valueOf).orElse(null);
|
||||
|
||||
Reference in New Issue
Block a user