implemented deletion of times
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -10,6 +10,7 @@ import static de.srsoftware.umbrella.time.Constants.*;
|
||||
import static java.lang.System.Logger.Level.ERROR;
|
||||
import static java.text.MessageFormat.format;
|
||||
|
||||
import de.srsoftware.tools.jdbc.Query;
|
||||
import de.srsoftware.umbrella.core.BaseDb;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.Time;
|
||||
@@ -77,7 +78,20 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Override
|
||||
public Long delete(long timeId) {
|
||||
try {
|
||||
db.setAutoCommit(false);
|
||||
Query.delete().from(TABLE_TASK_TIMES).where(TIME_ID,equal(timeId)).execute(db);
|
||||
Query.delete().from(TABLE_TIMES).where(ID,equal(timeId)).execute(db);
|
||||
db.setAutoCommit(false);
|
||||
return timeId;
|
||||
} catch (SQLException e) {
|
||||
throw UmbrellaException.databaseException("Failed to delete time with id = {0}",timeId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public HashMap<Long,Time> listTimes(Collection<Long> taskIds, boolean showClosed) throws UmbrellaException {
|
||||
try {
|
||||
var rs = select(ALL).from(TABLE_TASK_TIMES).where(TASK_ID,in(taskIds.toArray())).exec(db);
|
||||
|
||||
@@ -7,6 +7,8 @@ import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
|
||||
public interface TimeDb {
|
||||
Long delete(long timeId);
|
||||
|
||||
HashMap<Long,Time> listTimes(Collection<Long> taskIds, boolean showClosed) throws UmbrellaException;
|
||||
|
||||
HashMap<Long,Time> listUserTimes(long userId, boolean showClosed);
|
||||
|
||||
@@ -34,6 +34,32 @@ public class TimeModule extends BaseHandler implements TimeService {
|
||||
timeDb = new SqliteDb(connect(dbFile));
|
||||
}
|
||||
|
||||
|
||||
private boolean deleteTime(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 delete this time!");
|
||||
timeDb.delete(timeId);
|
||||
return sendContent(ex,time);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean doDelete(Path path, HttpExchange ex) throws IOException {
|
||||
addCors(ex);
|
||||
try {
|
||||
Optional<Token> token = SessionToken.from(ex).map(Token::of);
|
||||
var user = userService().loadUser(token);
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
var head = path.pop();
|
||||
var timeId = Long.parseLong(head);
|
||||
return deleteTime(user.get(),timeId,ex);
|
||||
} catch (NumberFormatException e){
|
||||
return send(ex,invalidFieldException(TIME_ID,"long value"));
|
||||
} catch (UmbrellaException e){
|
||||
return send(ex,e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doGet(Path path, HttpExchange ex) throws IOException {
|
||||
addCors(ex);
|
||||
|
||||
Reference in New Issue
Block a user