|
|
|
|
@ -105,6 +105,7 @@ public class TimeModule extends BaseHandler implements TimeService {
@@ -105,6 +105,7 @@ public class TimeModule extends BaseHandler implements TimeService {
|
|
|
|
|
return switch (head) { |
|
|
|
|
case JOIN -> joinTimes(ex,user.get()); |
|
|
|
|
case LIST -> listTimes(ex,user.get()); |
|
|
|
|
case SEARCH -> postSearch(ex, user.get()); |
|
|
|
|
case TRACK_TASK -> trackTask(user.get(),path,ex); |
|
|
|
|
default -> { |
|
|
|
|
try { |
|
|
|
|
@ -180,48 +181,6 @@ public class TimeModule extends BaseHandler implements TimeService {
@@ -180,48 +181,6 @@ public class TimeModule extends BaseHandler implements TimeService {
|
|
|
|
|
return sendContent(ex,time); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
try { |
|
|
|
|
var taskId = Long.parseLong(path.pop()); |
|
|
|
|
task = taskService().load(List.of(taskId)).get(taskId); |
|
|
|
|
if (task == null) throw UmbrellaException.notFound("Failed to load task with id = {0}",taskId); |
|
|
|
|
} catch (NumberFormatException e) { |
|
|
|
|
throw invalidFieldException(TASK_ID,"long value"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
long now; |
|
|
|
|
try { |
|
|
|
|
now = Long.parseLong(body(ex)); |
|
|
|
|
} catch (NumberFormatException e) { |
|
|
|
|
throw unprocessable("request body does not contain a timestamp!"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var opt = getStartedTime(user); |
|
|
|
|
if (opt.isPresent()){ |
|
|
|
|
var startedTime = opt.get(); |
|
|
|
|
|
|
|
|
|
if (startedTime.taskIds().contains(task.id())) { |
|
|
|
|
// if the time started last already belongs to the task, there is nothing left to do
|
|
|
|
|
return sendContent(ex,startedTime); |
|
|
|
|
} |
|
|
|
|
timeDb.save(startedTime.stop(now)); |
|
|
|
|
} |
|
|
|
|
var track = new Time(0,user.id(),task.name(),task.description(),now,null,Started,List.of(task.id())); |
|
|
|
|
timeDb.save(track); |
|
|
|
|
|
|
|
|
|
return sendContent(ex,track); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean getStartedTime(UmbrellaUser user, HttpExchange ex) throws IOException { |
|
|
|
|
var startedTime = getStartedTime(user); |
|
|
|
|
if (startedTime.isPresent()) return sendContent(ex,startedTime.get()); |
|
|
|
|
@ -272,4 +231,55 @@ public class TimeModule extends BaseHandler implements TimeService {
@@ -272,4 +231,55 @@ 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!"); |
|
|
|
|
var json = json(ex); |
|
|
|
|
timeDb.save(time.patch(json)); |
|
|
|
|
return sendContent(ex,time); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
var keys = Arrays.asList(key.split(" ")); |
|
|
|
|
var fulltext = json.has(FULLTEXT) && json.get(FULLTEXT) instanceof Boolean val && val; |
|
|
|
|
var notes = timeDb.find(user.id(),keys,fulltext); |
|
|
|
|
return sendContent(ex,mapValues(notes)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean trackTask(UmbrellaUser user, Path path, HttpExchange ex) throws IOException { |
|
|
|
|
if (path.empty()) throw missingFieldException(TASK_ID); |
|
|
|
|
Task task; |
|
|
|
|
try { |
|
|
|
|
var taskId = Long.parseLong(path.pop()); |
|
|
|
|
task = taskService().load(List.of(taskId)).get(taskId); |
|
|
|
|
if (task == null) throw UmbrellaException.notFound("Failed to load task with id = {0}",taskId); |
|
|
|
|
} catch (NumberFormatException e) { |
|
|
|
|
throw invalidFieldException(TASK_ID,"long value"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
long now; |
|
|
|
|
try { |
|
|
|
|
now = Long.parseLong(body(ex)); |
|
|
|
|
} catch (NumberFormatException e) { |
|
|
|
|
throw unprocessable("request body does not contain a timestamp!"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var opt = getStartedTime(user); |
|
|
|
|
if (opt.isPresent()){ |
|
|
|
|
var startedTime = opt.get(); |
|
|
|
|
|
|
|
|
|
if (startedTime.taskIds().contains(task.id())) { |
|
|
|
|
// if the time started last already belongs to the task, there is nothing left to do
|
|
|
|
|
return sendContent(ex,startedTime); |
|
|
|
|
} |
|
|
|
|
timeDb.save(startedTime.stop(now)); |
|
|
|
|
} |
|
|
|
|
var track = new Time(0,user.id(),task.name(),task.description(),now,null,Started,List.of(task.id())); |
|
|
|
|
timeDb.save(track); |
|
|
|
|
|
|
|
|
|
return sendContent(ex,track); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|