completed new contact creation, implemented contact deletion
This commit is contained in:
@@ -3,11 +3,11 @@ 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;
|
||||
|
||||
public interface ContactDb {
|
||||
void drop(Contact contact);
|
||||
|
||||
Map<Long,Contact> listContactsOf(long userId) throws UmbrellaException;
|
||||
|
||||
Contact load(long id, long userId);
|
||||
|
||||
@@ -33,6 +33,30 @@ public class ContactModule extends BaseHandler implements ContactService {
|
||||
ModuleRegistry.add(this);
|
||||
}
|
||||
|
||||
private boolean deleteContact(long id, UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||
var contact = contactDb.load(id, user.id());
|
||||
contactDb.drop(contact);
|
||||
return sendEmptyResponse(200,ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doDelete(Path path, HttpExchange ex) throws IOException {
|
||||
addCors(ex);
|
||||
try {
|
||||
var user = userService().refreshSession(ex);
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
var head = path.pop();
|
||||
try {
|
||||
var id = Long.parseLong(head);
|
||||
return deleteContact(id, user.get(), ex);
|
||||
} catch (Exception ignored){
|
||||
return super.doPatch(path,ex);
|
||||
}
|
||||
} catch (UmbrellaException e) {
|
||||
return send(ex,e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doGet(Path path, HttpExchange ex) throws IOException {
|
||||
addCors(ex);
|
||||
@@ -54,8 +78,7 @@ public class ContactModule extends BaseHandler implements ContactService {
|
||||
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);
|
||||
var user = userService().refreshSession(ex);
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
var head = path.pop();
|
||||
try {
|
||||
|
||||
@@ -9,7 +9,6 @@ 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.tools.jdbc.Query;
|
||||
@@ -59,6 +58,17 @@ public class SqliteDb extends BaseDb implements ContactDb{
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drop(Contact contact) {
|
||||
try {
|
||||
db.setAutoCommit(false);
|
||||
Query.delete().from(TABLE_CONTACTS).where(ID,equal(contact.id())).execute(db);
|
||||
Query.delete().from(TABLE_CONTACTS_USERS).where(CONTACT_ID,equal(contact.id())).execute(db);
|
||||
db.setAutoCommit(true);
|
||||
} catch (SQLException e){
|
||||
throw databaseException("Failed to remove contact {0}",contact.id());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Long,Contact> listContactsOf(long userId) throws UmbrellaException{
|
||||
|
||||
Reference in New Issue
Block a user