time tracking and display of elapsed time no working
This commit is contained in:
@@ -5,19 +5,16 @@ import static de.srsoftware.tools.jdbc.Condition.*;
|
||||
import static de.srsoftware.tools.jdbc.Query.*;
|
||||
import static de.srsoftware.tools.jdbc.Query.SelectQuery.ALL;
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.model.Status.OPEN;
|
||||
import static de.srsoftware.umbrella.core.model.Time.State.Complete;
|
||||
import static de.srsoftware.umbrella.time.Constants.*;
|
||||
import static java.lang.System.Logger.Level.ERROR;
|
||||
import static java.text.MessageFormat.format;
|
||||
import static java.time.ZoneOffset.UTC;
|
||||
|
||||
import de.srsoftware.umbrella.core.BaseDb;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.Time;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
@@ -132,7 +129,25 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Override
|
||||
public Time load(long timeId) {
|
||||
try {
|
||||
var query = select(ALL).from(TABLE_TIMES).where(ID,equal(timeId));
|
||||
var rs = query.exec(db);
|
||||
Time time = null;
|
||||
if (rs.next()) time = Time.of(rs);
|
||||
rs.close();
|
||||
rs = select(ALL).from(TABLE_TASK_TIMES).where(TIME_ID,equal(timeId)).exec(db);
|
||||
while (rs.next()) time.taskIds().add(rs.getLong(TASK_ID));
|
||||
rs.close();
|
||||
if (time == null) throw UmbrellaException.notFound("No time found with id = {0}",timeId);
|
||||
return time;
|
||||
} catch (SQLException e) {
|
||||
throw new UmbrellaException("Failed to load times for task list");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Time save(Time track) throws UmbrellaException {
|
||||
try {
|
||||
if (track.id() == 0) { // create new
|
||||
|
||||
@@ -3,8 +3,6 @@ package de.srsoftware.umbrella.time;
|
||||
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.Time;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
|
||||
@@ -13,5 +11,7 @@ public interface TimeDb {
|
||||
|
||||
HashMap<Long,Time> listUserTimes(long userId, boolean showClosed);
|
||||
|
||||
Time load(long timeId);
|
||||
|
||||
Time save(Time track) throws UmbrellaException;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
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 {
|
||||
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 {
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
} 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 {
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user