refactored timetracking to only use client-supplied times
This commit is contained in:
@@ -152,14 +152,14 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
try {
|
||||
if (track.id() == 0) { // create new
|
||||
var rs = insertInto(TABLE_TIMES, USER_ID, SUBJECT, DESCRIPTION, START_TIME, END_TIME, STATE)
|
||||
.values(track.userId(), track.subject(), track.description(), track.startSecond(), track.endSecond(), track.state().code())
|
||||
.values(track.userId(), track.subject(), track.description(), track.start(), track.end(), track.state().code())
|
||||
.execute(db)
|
||||
.getGeneratedKeys();
|
||||
if (rs.next()) track.setId(rs.getLong(1));
|
||||
rs.close();
|
||||
} else { // update
|
||||
replaceInto(TABLE_TIMES, ID, USER_ID, SUBJECT, DESCRIPTION, START_TIME, END_TIME, STATE)
|
||||
.values(track.id(), track.userId(), track.subject(), track.description(), track.startSecond(), track.endSecond(), track.state().code())
|
||||
.values(track.id(), track.userId(), track.subject(), track.description(), track.start(), track.end(), track.state().code())
|
||||
.execute(db).close();
|
||||
}
|
||||
var query = replaceInto(TABLE_TASK_TIMES,TIME_ID,TASK_ID);
|
||||
|
||||
@@ -21,25 +21,13 @@ 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.*;
|
||||
|
||||
public class TimeModule extends BaseHandler implements TimeService {
|
||||
|
||||
private class ExtendedTime extends Time{
|
||||
|
||||
|
||||
private final Collection<Task> tasks;
|
||||
|
||||
public ExtendedTime(long id, long userId, String subject, String description, LocalDateTime start, LocalDateTime end, State state, Collection<Task> tasks) {
|
||||
super(id, userId, subject, description, start, end, state, tasks.stream().map(Task::id).toList());
|
||||
this.tasks = tasks;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private final TimeDb timeDb;
|
||||
|
||||
|
||||
public TimeModule(ModuleRegistry registry, Configuration config) throws UmbrellaException {
|
||||
super(registry);
|
||||
var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingFieldException(CONFIG_DATABASE));
|
||||
@@ -55,16 +43,9 @@ public class TimeModule extends BaseHandler implements TimeService {
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
var head = path.pop();
|
||||
return switch (head) {
|
||||
case TRACK_TASK -> trackTask(user.get(),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);
|
||||
}
|
||||
default -> super.doGet(path,ex);
|
||||
};
|
||||
} catch (UmbrellaException e){
|
||||
return send(ex,e);
|
||||
@@ -72,8 +53,14 @@ public class TimeModule extends BaseHandler implements TimeService {
|
||||
}
|
||||
|
||||
private boolean getStoppedTask(UmbrellaUser user, long timeId, HttpExchange ex) throws IOException {
|
||||
long now;
|
||||
try {
|
||||
now = Long.parseLong(body(ex));
|
||||
} catch (NumberFormatException e) {
|
||||
throw unprocessable("request body does not contain a timestamp!");
|
||||
}
|
||||
var time = timeDb.load(timeId);
|
||||
timeDb.save(time.stop(LocalDateTime.now()));
|
||||
timeDb.save(time.stop(now));
|
||||
return sendContent(ex,time);
|
||||
}
|
||||
|
||||
@@ -104,7 +91,14 @@ public class TimeModule extends BaseHandler implements TimeService {
|
||||
var head = path.pop();
|
||||
return switch (head) {
|
||||
case LIST -> listTimes(ex,user.get());
|
||||
default -> super.doPost(path,ex);
|
||||
case TRACK_TASK -> trackTask(user.get(),path,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);
|
||||
@@ -136,7 +130,13 @@ public class TimeModule extends BaseHandler implements TimeService {
|
||||
} catch (NumberFormatException e) {
|
||||
throw invalidFieldException(TASK_ID,"long value");
|
||||
}
|
||||
var now = LocalDateTime.now().withNano(0);
|
||||
|
||||
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()){
|
||||
@@ -156,9 +156,7 @@ public class TimeModule extends BaseHandler implements TimeService {
|
||||
|
||||
private boolean getStartedTime(UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||
var startedTime = getStartedTime(user);
|
||||
if (startedTime.isPresent()){
|
||||
return sendContent(ex,startedTime.get());
|
||||
}
|
||||
if (startedTime.isPresent()) return sendContent(ex,startedTime.get());
|
||||
return send(ex,UmbrellaException.notFound("no started time"));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user