implemented storing of new vcards, editing of names
This commit is contained in:
@@ -3,6 +3,7 @@ package de.srsoftware.umbrella.contact;
|
||||
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.Contact;
|
||||
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@@ -12,4 +13,6 @@ public interface ContactDb {
|
||||
Contact load(long id, long userId);
|
||||
|
||||
Contact save(Contact contact);
|
||||
|
||||
void setOwner(long userId, Contact contact);
|
||||
}
|
||||
|
||||
@@ -69,11 +69,24 @@ public class ContactModule extends BaseHandler implements ContactService {
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
@Override
|
||||
public boolean doPost(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();
|
||||
if (head == null) {
|
||||
return postNewContact(user.get(),ex);
|
||||
}
|
||||
return super.doPatch(path,ex);
|
||||
} catch (UmbrellaException e) {
|
||||
return send(ex,e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private boolean getContacts(UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||
return sendContent(ex,mapValues(listContactsOf(user)));
|
||||
}
|
||||
@@ -82,4 +95,16 @@ public class ContactModule extends BaseHandler implements ContactService {
|
||||
public Map<Long,Contact> listContactsOf(UmbrellaUser user) throws UmbrellaException {
|
||||
return contactDb.listContactsOf(user.id());
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
private boolean postNewContact(UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||
var contact = new Contact(0,body(ex));
|
||||
contact = contactDb.save(contact);
|
||||
contactDb.setOwner(user.id(),contact);
|
||||
return sendContent(ex,contact);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,4 +110,13 @@ public class SqliteDb extends BaseDb implements ContactDb{
|
||||
}
|
||||
return contact;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOwner(long userId, Contact contact) {
|
||||
try {
|
||||
Query.replaceInto(TABLE_CONTACTS_USERS,USER_ID,CONTACT_ID,ASSIGNED).values(userId,contact.id(),false).execute(db).close();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException("Failed to assign contact {0} to user {1]",contact.id(),userId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user