added database code: attachments now also added to appointment lists
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.4")
|
||||
implementation("de.srsoftware:tools.jdbc:1.1.5")
|
||||
implementation("de.srsoftware:tools.optionals:1.0.0")
|
||||
implementation("de.srsoftware:tools.util:1.3.0")
|
||||
}
|
||||
|
||||
@@ -74,9 +74,9 @@ public class MariaDB implements Database {
|
||||
keys.close();
|
||||
if (saved == null) return error("Insert query did not return appointment id!");
|
||||
|
||||
addAttachments(saved);
|
||||
addLinks(saved);
|
||||
addTags(saved);
|
||||
writeAttachments(saved);
|
||||
writeLinks(saved);
|
||||
writeTags(saved);
|
||||
|
||||
return Payload.of(saved);
|
||||
} catch (SQLException e) {
|
||||
@@ -85,7 +85,7 @@ public class MariaDB implements Database {
|
||||
}
|
||||
}
|
||||
|
||||
private void addAttachments(Appointment saved) throws SQLException { // link to attachments
|
||||
private void writeAttachments(Appointment saved) throws SQLException { // link to attachments
|
||||
var attachments = saved.attachments();
|
||||
InsertQuery assignQuery = null;
|
||||
for (var attachment : attachments) {
|
||||
@@ -96,7 +96,7 @@ public class MariaDB implements Database {
|
||||
if (assignQuery != null) assignQuery.ignoreDuplicates().execute(connection);
|
||||
}
|
||||
|
||||
private void addLinks(Appointment saved) throws SQLException { // link to links
|
||||
private void writeLinks(Appointment saved) throws SQLException { // link to links
|
||||
var links = saved.links();
|
||||
InsertQuery assignQuery = null;
|
||||
for (var link : links) {
|
||||
@@ -107,7 +107,7 @@ public class MariaDB implements Database {
|
||||
if (assignQuery != null) assignQuery.ignoreDuplicates().execute(connection);
|
||||
}
|
||||
|
||||
private void addTags(Appointment saved) throws SQLException {
|
||||
private void writeTags(Appointment saved) throws SQLException {
|
||||
var tags = saved.tags();
|
||||
InsertQuery assignQuery = null;
|
||||
for (var tag : tags) {
|
||||
@@ -178,12 +178,41 @@ public class MariaDB implements Database {
|
||||
var list = new ArrayList<Appointment>();
|
||||
while (results.next()) createAppointmentOf(results).optional().ifPresent(list::add);
|
||||
results.close();
|
||||
addAttachments(list);
|
||||
return Payload.of(list);
|
||||
} catch (SQLException e) {
|
||||
return SqlError.of(e, "Failed to fetch appointments from database!");
|
||||
}
|
||||
}
|
||||
|
||||
private void addAttachments(ArrayList<Appointment> list) {
|
||||
var map = new HashMap<Long,BaseAppointment>();
|
||||
|
||||
list.stream().filter(app -> app instanceof BaseAppointment)
|
||||
.map(BaseAppointment.class::cast)
|
||||
.forEach(app -> map.put(app.id(),app));
|
||||
var keys = map.keySet().toArray();
|
||||
try {
|
||||
var rs = select(ALL).from(APPOINTMENT_ATTACHMENTS).leftJoin(UID,URLS,UID).where(AID,in(keys)).exec(connection);
|
||||
while (rs.next()){
|
||||
var aid = rs.getLong(AID);
|
||||
var app = map.get(aid);
|
||||
if (app == null) continue;
|
||||
var mime = rs.getString(MIME);
|
||||
var uri = rs.getString(URL);
|
||||
try {
|
||||
var url = URI.create(uri).toURL();
|
||||
app.add(new Attachment(url,mime));
|
||||
} catch (MalformedURLException e) {
|
||||
LOG.log(WARNING,"Faild to create URL object from %s",uri);
|
||||
}
|
||||
}
|
||||
rs.close();
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Appointment> loadEvent(long id) {
|
||||
try {
|
||||
@@ -330,11 +359,11 @@ public class MariaDB implements Database {
|
||||
.apply(event.title(), event.description(), start, end, location, coords);
|
||||
|
||||
delete().from(APPOINTMENT_TAGS).where(AID, equal(id)).execute(connection);
|
||||
addTags(event);
|
||||
writeTags(event);
|
||||
delete().from(APPOINTMENT_ATTACHMENTS).where(AID, equal(id)).execute(connection);
|
||||
addAttachments(event);
|
||||
writeAttachments(event);
|
||||
delete().from(APPOINTMENT_URLS).where(AID, equal(id)).execute(connection);
|
||||
addLinks(event);
|
||||
writeLinks(event);
|
||||
|
||||
return Payload.of(event);
|
||||
} catch (SQLException sqle) {
|
||||
|
||||
Reference in New Issue
Block a user