|
|
|
@ -22,6 +22,8 @@ import de.srsoftware.umbrella.core.exceptions.UmbrellaException; |
|
|
|
import de.srsoftware.umbrella.core.model.*; |
|
|
|
import de.srsoftware.umbrella.core.model.*; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.util.*; |
|
|
|
import java.util.*; |
|
|
|
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
import org.json.JSONArray; |
|
|
|
|
|
|
|
|
|
|
|
public class TimeModule extends BaseHandler implements TimeService { |
|
|
|
public class TimeModule extends BaseHandler implements TimeService { |
|
|
|
|
|
|
|
|
|
|
|
@ -34,6 +36,9 @@ public class TimeModule extends BaseHandler implements TimeService { |
|
|
|
ModuleRegistry.add(this); |
|
|
|
ModuleRegistry.add(this); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean accessible(Time time, UmbrellaUser user) { |
|
|
|
|
|
|
|
return time.userId() == user.id(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean deleteTime(UmbrellaUser user, long timeId, HttpExchange ex) throws IOException { |
|
|
|
private boolean deleteTime(UmbrellaUser user, long timeId, HttpExchange ex) throws IOException { |
|
|
|
var time = timeDb.load(timeId); |
|
|
|
var time = timeDb.load(timeId); |
|
|
|
@ -85,11 +90,17 @@ public class TimeModule extends BaseHandler implements TimeService { |
|
|
|
Optional<Token> token = SessionToken.from(ex).map(Token::of); |
|
|
|
Optional<Token> token = SessionToken.from(ex).map(Token::of); |
|
|
|
var user = userService().loadUser(token); |
|
|
|
var user = userService().loadUser(token); |
|
|
|
if (user.isEmpty()) return unauthorized(ex); |
|
|
|
if (user.isEmpty()) return unauthorized(ex); |
|
|
|
|
|
|
|
|
|
|
|
var head = path.pop(); |
|
|
|
var head = path.pop(); |
|
|
|
var timeId = Long.parseLong(head); |
|
|
|
|
|
|
|
return patchTime(user.get(),timeId,ex); |
|
|
|
if (head == null){ |
|
|
|
} catch (NumberFormatException e){ |
|
|
|
return patchTimes(user.get(),ex); |
|
|
|
return send(ex,invalidFieldException(TIME_ID,"long value")); |
|
|
|
} else try { |
|
|
|
|
|
|
|
var timeId = Long.parseLong(head); |
|
|
|
|
|
|
|
return patchTime(user.get(), timeId, ex); |
|
|
|
|
|
|
|
} catch (NumberFormatException e) { |
|
|
|
|
|
|
|
return send(ex, invalidFieldException(TIME_ID, "long value")); |
|
|
|
|
|
|
|
} |
|
|
|
} catch (UmbrellaException e){ |
|
|
|
} catch (UmbrellaException e){ |
|
|
|
return send(ex,e); |
|
|
|
return send(ex,e); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -241,12 +252,26 @@ public class TimeModule extends BaseHandler implements TimeService { |
|
|
|
|
|
|
|
|
|
|
|
private boolean patchTime(UmbrellaUser user, long timeId, HttpExchange ex) throws IOException { |
|
|
|
private boolean patchTime(UmbrellaUser user, long timeId, HttpExchange ex) throws IOException { |
|
|
|
var time = timeDb.load(timeId); |
|
|
|
var time = timeDb.load(timeId); |
|
|
|
if (time.userId() != user.id()) throw forbidden("You are not allowed to alter this time!"); |
|
|
|
if (!accessible(time, user)) throw forbidden("You are not allowed to alter this time!"); |
|
|
|
var json = json(ex); |
|
|
|
var json = json(ex); |
|
|
|
timeDb.save(time.patch(json)); |
|
|
|
timeDb.save(time.patch(json)); |
|
|
|
return sendContent(ex,time); |
|
|
|
return sendContent(ex,time); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean patchTimes(UmbrellaUser user, HttpExchange ex) throws IOException { |
|
|
|
|
|
|
|
var json = json(ex); |
|
|
|
|
|
|
|
if (!json.has(IDS) || !(json.get(IDS) instanceof JSONArray ids)) throw missingFieldException(IDS); |
|
|
|
|
|
|
|
var times = new HashSet<Time>(); |
|
|
|
|
|
|
|
for (var o : ids.toList()){ |
|
|
|
|
|
|
|
if (!(o instanceof Number id)) throw unprocessable("IDS list contains {0}, which is not a valid ID!",o); |
|
|
|
|
|
|
|
var time = timeDb.load(id.longValue()); |
|
|
|
|
|
|
|
if (!accessible(time,user)) throw forbidden("Time list contains id ({0}) of time ({1}) which you are not allowed to edit!",o,time.subject()); |
|
|
|
|
|
|
|
times.add(time); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for (var time : times) timeDb.save(time.patch(json)); |
|
|
|
|
|
|
|
return sendContent(ex, times.stream().collect(Collectors.toMap(Time::id,Time::toMap))); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean postSearch(HttpExchange ex, UmbrellaUser user) throws IOException { |
|
|
|
private boolean postSearch(HttpExchange ex, UmbrellaUser user) throws IOException { |
|
|
|
var json = json(ex); |
|
|
|
var json = json(ex); |
|
|
|
if (!(json.has(KEY) && json.get(KEY) instanceof String key)) throw missingFieldException(KEY); |
|
|
|
if (!(json.has(KEY) && json.get(KEY) instanceof String key)) throw missingFieldException(KEY); |
|
|
|
|