|
|
|
|
@ -3,7 +3,7 @@ package de.srsoftware.umbrella.time;
@@ -3,7 +3,7 @@ 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.Paths.*; |
|
|
|
|
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.*; |
|
|
|
|
@ -21,9 +21,7 @@ import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
@@ -21,9 +21,7 @@ import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
|
|
|
|
import de.srsoftware.umbrella.core.model.*; |
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.time.LocalDateTime; |
|
|
|
|
import java.time.ZoneOffset; |
|
|
|
|
import java.util.*; |
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
public class TimeModule extends BaseHandler implements TimeService { |
|
|
|
|
|
|
|
|
|
@ -58,14 +56,27 @@ public class TimeModule extends BaseHandler implements TimeService {
@@ -58,14 +56,27 @@ public class TimeModule extends BaseHandler implements TimeService {
|
|
|
|
|
var head = path.pop(); |
|
|
|
|
return switch (head) { |
|
|
|
|
case TRACK_TASK -> trackTask(user.get(),path,ex); |
|
|
|
|
case null -> getUserTimes(user.get(),ex); |
|
|
|
|
default -> super.doGet(path,ex); |
|
|
|
|
case STARTED -> getStartedTime(user.get(),ex); |
|
|
|
|
case null -> getUserTimes(user.get(),ex); |
|
|
|
|
default -> { |
|
|
|
|
try { |
|
|
|
|
long timeId = Long.parseLong(head); |
|
|
|
|
if (STOP.equals(path.pop())) yield getStoppedTask(user.get(),timeId,ex); |
|
|
|
|
} catch (Exception ignored) {} |
|
|
|
|
yield super.doGet(path,ex); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} catch (UmbrellaException e){ |
|
|
|
|
return send(ex,e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean getStoppedTask(UmbrellaUser user, long timeId, HttpExchange ex) throws IOException { |
|
|
|
|
var time = timeDb.load(timeId); |
|
|
|
|
timeDb.save(time.stop(LocalDateTime.now())); |
|
|
|
|
return sendContent(ex,time); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public boolean doPost(Path path, HttpExchange ex) throws IOException { |
|
|
|
|
addCors(ex); |
|
|
|
|
@ -83,6 +94,13 @@ public class TimeModule extends BaseHandler implements TimeService {
@@ -83,6 +94,13 @@ public class TimeModule extends BaseHandler implements TimeService {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Optional<Time> getStartedTime(UmbrellaUser user){ |
|
|
|
|
return timeDb.listUserTimes(user.id(), false).values() |
|
|
|
|
.stream() |
|
|
|
|
.filter(time -> time.state() == Started) |
|
|
|
|
.max(Comparator.comparing(Time::start)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean trackTask(UmbrellaUser user, Path path, HttpExchange ex) throws IOException { |
|
|
|
|
if (path.empty()) throw missingFieldException(TASK_ID); |
|
|
|
|
Task task; |
|
|
|
|
@ -93,12 +111,9 @@ public class TimeModule extends BaseHandler implements TimeService {
@@ -93,12 +111,9 @@ public class TimeModule extends BaseHandler implements TimeService {
|
|
|
|
|
} catch (NumberFormatException e) { |
|
|
|
|
throw invalidFieldException(TASK_ID,"long value"); |
|
|
|
|
} |
|
|
|
|
var now = LocalDateTime.now().withSecond(0).withNano(0); |
|
|
|
|
var now = LocalDateTime.now().withNano(0); |
|
|
|
|
|
|
|
|
|
var opt = timeDb.listUserTimes(user.id(), false).values() |
|
|
|
|
.stream() |
|
|
|
|
.filter(time -> time.state() == Started) |
|
|
|
|
.max(Comparator.comparing(Time::start)); |
|
|
|
|
var opt = getStartedTime(user); |
|
|
|
|
if (opt.isPresent()){ |
|
|
|
|
var startedTime = opt.get(); |
|
|
|
|
|
|
|
|
|
@ -114,6 +129,14 @@ public class TimeModule extends BaseHandler implements TimeService {
@@ -114,6 +129,14 @@ public class TimeModule extends BaseHandler implements TimeService {
|
|
|
|
|
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()); |
|
|
|
|
} |
|
|
|
|
return send(ex,UmbrellaException.notFound("no started time")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean getUserTimes(UmbrellaUser user, HttpExchange ex) throws IOException { |
|
|
|
|
Set<Long> taskIds = new HashSet<>(); |
|
|
|
|
Map<Long, Project> projects = projectService().listUserProjects(user.id(), true); |
|
|
|
|
|