implemented buld status update for times
This commit is contained in:
@@ -7,6 +7,7 @@ public class Constants {
|
||||
public static final String CHILDREN = "children";
|
||||
public static final String CONFIG_DATABASE = "umbrella.modules.time.database";
|
||||
public static final String DOCUMENTS = "documents";
|
||||
public static final String IDS = "ids";
|
||||
public static final String JOIN = "join";
|
||||
public static final String PROJECTS = "projects";
|
||||
public static final String TABLE_TASK_TIMES = "task_times";
|
||||
|
||||
@@ -22,6 +22,8 @@ import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.*;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import org.json.JSONArray;
|
||||
|
||||
public class TimeModule extends BaseHandler implements TimeService {
|
||||
|
||||
@@ -34,6 +36,9 @@ public class TimeModule extends BaseHandler implements TimeService {
|
||||
ModuleRegistry.add(this);
|
||||
}
|
||||
|
||||
private boolean accessible(Time time, UmbrellaUser user) {
|
||||
return time.userId() == user.id();
|
||||
}
|
||||
|
||||
private boolean deleteTime(UmbrellaUser user, long timeId, HttpExchange ex) throws IOException {
|
||||
var time = timeDb.load(timeId);
|
||||
@@ -85,11 +90,17 @@ public class TimeModule extends BaseHandler implements TimeService {
|
||||
Optional<Token> token = SessionToken.from(ex).map(Token::of);
|
||||
var user = userService().loadUser(token);
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
|
||||
var head = path.pop();
|
||||
var timeId = Long.parseLong(head);
|
||||
return patchTime(user.get(),timeId,ex);
|
||||
} catch (NumberFormatException e){
|
||||
return send(ex,invalidFieldException(TIME_ID,"long value"));
|
||||
|
||||
if (head == null){
|
||||
return patchTimes(user.get(),ex);
|
||||
} else try {
|
||||
var timeId = Long.parseLong(head);
|
||||
return patchTime(user.get(), timeId, ex);
|
||||
} catch (NumberFormatException e) {
|
||||
return send(ex, invalidFieldException(TIME_ID, "long value"));
|
||||
}
|
||||
} catch (UmbrellaException e){
|
||||
return send(ex,e);
|
||||
}
|
||||
@@ -241,12 +252,26 @@ public class TimeModule extends BaseHandler implements TimeService {
|
||||
|
||||
private boolean patchTime(UmbrellaUser user, long timeId, HttpExchange ex) throws IOException {
|
||||
var time = timeDb.load(timeId);
|
||||
if (time.userId() != user.id()) throw forbidden("You are not allowed to alter this time!");
|
||||
if (!accessible(time, user)) throw forbidden("You are not allowed to alter this time!");
|
||||
var json = json(ex);
|
||||
timeDb.save(time.patch(json));
|
||||
return sendContent(ex,time);
|
||||
}
|
||||
|
||||
private boolean patchTimes(UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||
var json = json(ex);
|
||||
if (!json.has(IDS) || !(json.get(IDS) instanceof JSONArray ids)) throw missingFieldException(IDS);
|
||||
var times = new HashSet<Time>();
|
||||
for (var o : ids.toList()){
|
||||
if (!(o instanceof Number id)) throw unprocessable("IDS list contains {0}, which is not a valid ID!",o);
|
||||
var time = timeDb.load(id.longValue());
|
||||
if (!accessible(time,user)) throw forbidden("Time list contains id ({0}) of time ({1}) which you are not allowed to edit!",o,time.subject());
|
||||
times.add(time);
|
||||
}
|
||||
for (var time : times) timeDb.save(time.patch(json));
|
||||
return sendContent(ex, times.stream().collect(Collectors.toMap(Time::id,Time::toMap)));
|
||||
}
|
||||
|
||||
private boolean postSearch(HttpExchange ex, UmbrellaUser user) throws IOException {
|
||||
var json = json(ex);
|
||||
if (!(json.has(KEY) && json.get(KEY) instanceof String key)) throw missingFieldException(KEY);
|
||||
|
||||
Reference in New Issue
Block a user