implemented editing of document in GUI + respective handlers in backend
This commit is contained in:
@@ -5,6 +5,7 @@ import static de.srsoftware.tools.MimeType.MIME_FORM_URL;
|
||||
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.Paths.LIST;
|
||||
import static de.srsoftware.umbrella.core.ResponseCode.HTTP_UNPROCESSABLE;
|
||||
import static de.srsoftware.umbrella.core.Util.request;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.forbidden;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingFieldException;
|
||||
@@ -111,6 +112,23 @@ public class DocumentApi extends BaseHandler {
|
||||
return sendEmptyResponse(HTTP_OK,addCors(ex));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doPatch(Path path, HttpExchange ex) throws IOException {
|
||||
addCors(ex);
|
||||
try {
|
||||
Optional<Token> token = SessionToken.from(ex).map(Token::of);
|
||||
var user = users.loadUser(token);
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
var head = path.pop();
|
||||
var docId = Long.parseLong(head);
|
||||
return patchDocument(docId,user.get(),ex);
|
||||
} catch (NumberFormatException n){
|
||||
return sendContent(ex,HTTP_UNPROCESSABLE,"Invalid document id");
|
||||
} catch (UmbrellaException e) {
|
||||
return send(ex,e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doPost(Path path, HttpExchange ex) throws IOException {
|
||||
addCors(ex);
|
||||
@@ -187,6 +205,20 @@ public class DocumentApi extends BaseHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean patchDocument(long docId, UmbrellaUser user, HttpExchange ex) throws UmbrellaException, IOException {
|
||||
var doc = db.loadDoc(docId);
|
||||
var companyId = doc.companyId();
|
||||
if (!companies.membership(companyId,user.id())) throw forbidden("You are mot a member of company {0}",doc.companyId());
|
||||
var json = json(ex);
|
||||
for (var key : json.keySet()){
|
||||
var value = json.get(key);
|
||||
LOG.log(WARNING,"{0} : {1}",key,value);
|
||||
}
|
||||
doc.patch(json);
|
||||
db.save(doc);
|
||||
return ok(ex);
|
||||
}
|
||||
|
||||
private boolean postDocument(HttpExchange ex, UmbrellaUser user) throws IOException, UmbrellaException {
|
||||
var json = json(ex);
|
||||
if (!(json.has(SENDER) && json.get(SENDER) instanceof JSONObject senderData)) throw missingFieldException(SENDER);
|
||||
|
||||
@@ -438,10 +438,10 @@ CREATE TABLE IF NOT EXISTS {0} ( {1} VARCHAR(255) PRIMARY KEY, {2} VARCHAR(255)
|
||||
var sender = doc.sender();
|
||||
var custom = doc.customer();
|
||||
update(TABLE_DOCUMENTS)
|
||||
.set(DATE, FIELD_DELIVERY_DATE,FIELD_FOOTER,FIELD_HEAD, NUMBER, STATE, SENDER,FIELD_TAX_NUMBER,FIELD_BANK_ACCOUNT,FIELD_COURT,FIELD_CUSTOMER,FIELD_CUSTOMER_EMAIL,FIELD_CUSTOMER_NUMBER,FIELD_CUSTOMER_TAX_NUMBER)
|
||||
.set(DATE, FIELD_DELIVERY_DATE,FIELD_FOOTER,FIELD_HEAD, NUMBER, STATE, SENDER,FIELD_TAX_NUMBER,FIELD_BANK_ACCOUNT,FIELD_COURT,FIELD_CUSTOMER,FIELD_CUSTOMER_EMAIL,FIELD_CUSTOMER_NUMBER,FIELD_CUSTOMER_TAX_NUMBER,FIELD_TEMPLATE_ID)
|
||||
.where(ID,equal(doc.id()))
|
||||
.prepare(db)
|
||||
.apply(timestamp,doc.delivery(),doc.footer(),doc.head(),doc.number(),doc.state().code(),sender.name(),sender.taxNumber(),sender.bankAccount(),sender.court(),custom.name(),custom.email(),custom.id(),custom.taxNumber())
|
||||
.apply(timestamp,doc.delivery(),doc.footer(),doc.head(),doc.number(),doc.state().code(),sender.name(),sender.taxNumber(),sender.bankAccount(),sender.court(),custom.name(),custom.email(),custom.id(),custom.taxNumber(),doc.template().id())
|
||||
.close();
|
||||
sender.clean();
|
||||
custom.clean();
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
package de.srsoftware.umbrella.documents.model;
|
||||
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.ResponseCode.HTTP_UNPROCESSABLE;
|
||||
import static de.srsoftware.umbrella.core.Util.markdown;
|
||||
import static de.srsoftware.umbrella.documents.Constants.*;
|
||||
import static de.srsoftware.umbrella.documents.Constants.FIELD_CUSTOMER;
|
||||
@@ -11,6 +12,8 @@ import de.srsoftware.tools.Mappable;
|
||||
import java.time.LocalDate;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
|
||||
@@ -54,7 +57,7 @@ public final class Document implements Mappable {
|
||||
private final Type type;
|
||||
private LocalDate date;
|
||||
private State state;
|
||||
private final Template template;
|
||||
private Template template;
|
||||
private final Sender sender;
|
||||
private final Customer customer;
|
||||
private final PositionList positions;
|
||||
@@ -182,18 +185,21 @@ public final class Document implements Mappable {
|
||||
return number;
|
||||
}
|
||||
|
||||
public void patch(JSONObject json) {
|
||||
public void patch(JSONObject json) throws UmbrellaException {
|
||||
for (var key : json.keySet()){
|
||||
switch (key){
|
||||
case FIELD_CUSTOMER: if (json.get(key) instanceof JSONObject nested) customer.patch(nested); break;
|
||||
case FIELD_CURRENCY: currency = json.getString(key); break;
|
||||
case DATE: date = LocalDate.parse(json.getString(key)); break;
|
||||
case DATE: date = LocalDate.parse(json.getString(key)); break;
|
||||
case FIELD_DELIVERY: delivery = json.getString(key); break;
|
||||
case FIELD_FOOTER: footer = json.getString(key); break;
|
||||
case FIELD_HEAD: head = json.getString(key); break;
|
||||
case NUMBER: number = json.getString(key); break;
|
||||
case SENDER: if (json.get(key) instanceof JSONObject nested) sender.patch(nested); break;
|
||||
default: key = null;
|
||||
case NUMBER: number = json.getString(key); break;
|
||||
case SENDER: if (json.get(key) instanceof JSONObject nested) sender.patch(nested); break;
|
||||
case STATE: state = State.of(json.getInt(key)).orElseThrow(() -> new UmbrellaException(HTTP_UNPROCESSABLE,"Invalid state")); break;
|
||||
case FIELD_POS: if (json.get(key) instanceof JSONObject nested) positions.patch(nested); break;
|
||||
case FIELD_TEMPLATE_ID: if (json.get(key) instanceof Number num) template = new Template(num.longValue(),companyId,null,null); break;
|
||||
default: key = null;
|
||||
}
|
||||
if (key != null) dirtyFields.add(key);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user