|
|
|
|
@ -4,9 +4,10 @@ package de.srsoftware.umbrella.time;
@@ -4,9 +4,10 @@ package de.srsoftware.umbrella.time;
|
|
|
|
|
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.exceptions.UmbrellaException.forbidden; |
|
|
|
|
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingFieldException; |
|
|
|
|
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*; |
|
|
|
|
import static de.srsoftware.umbrella.core.model.Time.State.Started; |
|
|
|
|
import static de.srsoftware.umbrella.time.Constants.*; |
|
|
|
|
import static java.util.stream.Collectors.toMap; |
|
|
|
|
import static java.util.stream.Collectors.toSet; |
|
|
|
|
|
|
|
|
|
import com.sun.net.httpserver.HttpExchange; |
|
|
|
|
@ -21,6 +22,7 @@ import de.srsoftware.umbrella.core.model.*;
@@ -21,6 +22,7 @@ import de.srsoftware.umbrella.core.model.*;
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.time.LocalDateTime; |
|
|
|
|
import java.util.*; |
|
|
|
|
import java.util.function.Function; |
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
public class TimeModule extends BaseHandler implements TimeService { |
|
|
|
|
@ -55,6 +57,7 @@ public class TimeModule extends BaseHandler implements TimeService {
@@ -55,6 +57,7 @@ public class TimeModule extends BaseHandler implements TimeService {
|
|
|
|
|
if (user.isEmpty()) return unauthorized(ex); |
|
|
|
|
var head = path.pop(); |
|
|
|
|
return switch (head) { |
|
|
|
|
case ADD_TASK -> getAddTask(user.get(),path,ex); |
|
|
|
|
case null -> getUserTimes(user.get(),ex); |
|
|
|
|
default -> super.doGet(path,ex); |
|
|
|
|
}; |
|
|
|
|
@ -63,7 +66,7 @@ public class TimeModule extends BaseHandler implements TimeService {
@@ -63,7 +66,7 @@ public class TimeModule extends BaseHandler implements TimeService {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
@Override |
|
|
|
|
public boolean doPost(Path path, HttpExchange ex) throws IOException { |
|
|
|
|
addCors(ex); |
|
|
|
|
try { |
|
|
|
|
@ -80,7 +83,31 @@ public class TimeModule extends BaseHandler implements TimeService {
@@ -80,7 +83,31 @@ public class TimeModule extends BaseHandler implements TimeService {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean getUserTimes(UmbrellaUser user, HttpExchange ex) throws IOException { |
|
|
|
|
private boolean getAddTask(UmbrellaUser user, Path path, HttpExchange ex) throws IOException { |
|
|
|
|
if (path.empty()) throw missingFieldException(TASK_ID); |
|
|
|
|
long taskId; |
|
|
|
|
try { |
|
|
|
|
taskId = Long.parseLong(path.pop()); |
|
|
|
|
} catch (NumberFormatException e) { |
|
|
|
|
throw invalidFieldException(TASK_ID,"long value"); |
|
|
|
|
} |
|
|
|
|
var taskIds = new HashSet<Long>(); |
|
|
|
|
var times = timeDb.listUserTimes(user.id(), false).values().stream() |
|
|
|
|
.filter(time -> time.state() == Started) |
|
|
|
|
.peek(time -> taskIds.addAll(time.taskIds())) |
|
|
|
|
.collect(Collectors.toMap(Time::id, t -> t)); |
|
|
|
|
taskIds.add(taskId); |
|
|
|
|
var tasks = taskService().load(taskIds); |
|
|
|
|
if (times.isEmpty()){ |
|
|
|
|
var task = tasks.get(taskId); |
|
|
|
|
if (task == null) throw UmbrellaException.notFound("Failed to find task with id = {0}",taskId); |
|
|
|
|
var time = timeDb.createNew(user.id(),task.name(),task.description()); |
|
|
|
|
times.put(time.id(),time); |
|
|
|
|
} |
|
|
|
|
return sendContent(ex,Map.of(TIMES,times,TASKS,tasks)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean getUserTimes(UmbrellaUser user, HttpExchange ex) throws IOException { |
|
|
|
|
Set<Long> taskIds = new HashSet<>(); |
|
|
|
|
Map<Long, Project> projects = projectService().listUserProjects(user.id(), true); |
|
|
|
|
for (var pid : projects.keySet()) taskIds.addAll(taskService().listProjectTasks(pid).keySet()); |
|
|
|
|
|