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{
|
||||
|
||||
@@ -5,25 +5,24 @@ import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingFieldException;
|
||||
|
||||
import de.srsoftware.tools.Mappable;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Map;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public record Contact(long id, String vcard) implements Mappable {
|
||||
public static Contact of(ResultSet rs) throws SQLException {
|
||||
return new Contact(rs.getLong(ID),rs.getString(DATA));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> toMap() {
|
||||
return Map.of(ID,id,VCARD,vcard);
|
||||
}
|
||||
|
||||
public Contact patch(JSONObject json) {
|
||||
if (!(json.get(FROM) instanceof String from)) throw missingFieldException(FROM);
|
||||
if (!(json.get(TO) instanceof String to)) throw missingFieldException(TO);
|
||||
return new Contact(id,vcard.replace(from, to));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> toMap() {
|
||||
return Map.of(ID,id,VCARD,vcard);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,12 +5,11 @@ package de.srsoftware.umbrella.documents.model;
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
|
||||
import de.srsoftware.tools.Mappable;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class CustomerSettings implements Mappable {
|
||||
private static final System.Logger LOG = System.getLogger(CustomerSettings.class.getSimpleName());
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<script>
|
||||
import { api } from '../../urls.svelte';
|
||||
import { error, yikes } from '../../warn.svelte';
|
||||
import { name } from '../../vcard.js';
|
||||
import { t } from '../../translations.svelte';
|
||||
|
||||
|
||||
@@ -25,6 +26,26 @@
|
||||
let orgs = $derived(contact.vcard.match(/^ORG.*:.+$/gm));
|
||||
let urls = $derived(contact.vcard.match(/^URL.*:.+$/gm));
|
||||
|
||||
async function drop(){
|
||||
for (let n of names){
|
||||
n = name(n);
|
||||
n = (n.given + ' ' + n.family).trim();
|
||||
if (confirm(t('confirm_delete',{element:n}))) break;
|
||||
return
|
||||
}
|
||||
const url = api(`contact/${contact.id}`);
|
||||
const res = await fetch(url,{
|
||||
credentials:'include',
|
||||
method:'DELETE'
|
||||
});
|
||||
if (res.ok){
|
||||
yikes();
|
||||
contact = null;
|
||||
} else {
|
||||
error(res);
|
||||
}
|
||||
}
|
||||
|
||||
async function patch(from,to){
|
||||
if (from == to) return;
|
||||
const newContact = contact.id == 0;
|
||||
@@ -49,6 +70,7 @@
|
||||
}
|
||||
</script>
|
||||
|
||||
{#if contact}
|
||||
<fieldset class="vcard">
|
||||
<legend>
|
||||
{#if contact.id}
|
||||
@@ -57,6 +79,9 @@
|
||||
{t('new_contact')}
|
||||
{/if}
|
||||
<button class="symbol" onclick={toggleCode}></button>
|
||||
{#if contact.id}
|
||||
<button class="symbol" onclick={drop}></button>
|
||||
{/if}
|
||||
</legend>
|
||||
<table>
|
||||
<thead></thead>
|
||||
@@ -124,4 +149,5 @@
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</fieldset>
|
||||
</fieldset>
|
||||
{/if}
|
||||
Reference in New Issue
Block a user