|
|
|
@ -74,9 +74,9 @@ public class MariaDB implements Database {
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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) { |
|
|
|
|