implemented editing of times

This commit is contained in:
2025-08-27 23:54:48 +02:00
parent e1f32c274b
commit 5abfa96dc2
5 changed files with 124 additions and 10 deletions

View File

@@ -77,7 +77,24 @@ public class TimeModule extends BaseHandler implements TimeService {
return sendContent(ex,time);
}
@Override
@Override
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);
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"));
} catch (UmbrellaException e){
return send(ex,e);
}
}
@Override
public boolean doPost(Path path, HttpExchange ex) throws IOException {
addCors(ex);
try {
@@ -101,6 +118,14 @@ public class TimeModule extends BaseHandler implements TimeService {
.max(Comparator.comparing(Time::start));
}
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!");
var json = json(ex);
timeDb.save(time.patch(json));
return sendContent(ex,time);
}
private boolean trackTask(UmbrellaUser user, Path path, HttpExchange ex) throws IOException {
if (path.empty()) throw missingFieldException(TASK_ID);
Task task;