working on patching vcards

This commit is contained in:
2025-10-08 16:02:40 +02:00
parent 3df91f1b9d
commit 2f3a29d606
7 changed files with 98 additions and 7 deletions

View File

@@ -3,8 +3,13 @@ package de.srsoftware.umbrella.contact;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import de.srsoftware.umbrella.core.model.Contact;
import java.util.Map;
public interface ContactDb {
Map<Long,Contact> listContactsOf(long id) throws UmbrellaException;
Map<Long,Contact> listContactsOf(long userId) throws UmbrellaException;
Contact load(long id, long userId);
Contact save(Contact contact);
}

View File

@@ -50,6 +50,30 @@ public class ContactModule extends BaseHandler implements ContactService {
}
}
@Override
public boolean doPatch(Path path, HttpExchange ex) throws IOException {
addCors(ex);
try {
Optional<Token> token = SessionToken.from(ex).map(Token::of);
var user = userService().loadUser(token);
if (user.isEmpty()) return unauthorized(ex);
var head = path.pop();
try {
var id = Long.parseLong(head);
return patchContact(id, user.get(), ex);
} catch (Exception ignored){
return super.doPatch(path,ex);
}
} catch (UmbrellaException e) {
return send(ex,e);
}
}
private boolean patchContact(long id, UmbrellaUser user, HttpExchange ex) throws IOException {
var contact = contactDb.load(id, user.id()).patch(json(ex));
return sendContent(ex,contactDb.save(contact).toMap());
}
private boolean getContacts(UmbrellaUser user, HttpExchange ex) throws IOException {
return sendContent(ex,mapValues(listContactsOf(user)));
}

View File

@@ -7,6 +7,8 @@ import static de.srsoftware.tools.jdbc.Query.select;
import static de.srsoftware.umbrella.contact.Constants.*;
import static de.srsoftware.umbrella.core.Constants.*;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.databaseException;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.notFound;
import static java.lang.System.Logger.Level.ERROR;
import static java.text.MessageFormat.format;
import de.srsoftware.umbrella.core.BaseDb;
@@ -72,4 +74,23 @@ public class SqliteDb extends BaseDb implements ContactDb{
}
}
@Override
public Contact load(long id, long userId) {
try {
var rs = select(ALL).from(TABLE_CONTACTS).leftJoin(ID,TABLE_CONTACTS_USERS,CONTACT_ID).where(USER_ID,equal(userId)).where(ID,equal(id)).exec(db);
Contact contact = null;
if (rs.next()) contact = Contact.of(rs);
rs.close();
if (contact != null) return contact;
throw notFound("Failed to load contact with id = {0}",id);
} catch (SQLException e) {
throw databaseException("Failed to load contacts of user {0}",userId);
}
}
@Override
public Contact save(Contact contact) {
LOG.log(ERROR,"Save not implemented!");
return contact;
}
}