Browse Source

implemented editing of times

feature/join_times
Stephan Richter 2 months ago
parent
commit
5abfa96dc2
  1. 23
      core/src/main/java/de/srsoftware/umbrella/core/model/Time.java
  2. 24
      frontend/src/Components/DateTimeEditor.svelte
  3. 6
      frontend/src/Components/LineEditor.svelte
  4. 52
      frontend/src/routes/time/Index.svelte
  5. 25
      time/src/main/java/de/srsoftware/umbrella/time/TimeModule.java

23
core/src/main/java/de/srsoftware/umbrella/core/model/Time.java

@ -6,20 +6,24 @@ import static de.srsoftware.umbrella.core.Util.*; @@ -6,20 +6,24 @@ import static de.srsoftware.umbrella.core.Util.*;
import static java.time.ZoneOffset.UTC;
import de.srsoftware.tools.Mappable;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import org.json.JSONObject;
public class Time implements Mappable{
private static final DateTimeFormatter DT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
private final HashSet<Long> taskIds = new HashSet<>();
private LocalDateTime end;
private final LocalDateTime start;
private LocalDateTime start;
private long id;
private final long userId;
private final String description, subject;
private String description;
private String subject;
private State state;
public enum State{
@ -49,9 +53,8 @@ public class Time implements Mappable{ @@ -49,9 +53,8 @@ public class Time implements Mappable{
default -> throw new IllegalArgumentException();
};
}
}
public Time(long id, long userId, String subject, String description, LocalDateTime start, LocalDateTime end, State state, Collection<Long> taskIds){
this.id=id;
this.userId = userId;
@ -62,6 +65,7 @@ public class Time implements Mappable{ @@ -62,6 +65,7 @@ public class Time implements Mappable{
this.state = state;
if (taskIds != null) this.taskIds.addAll(taskIds);
}
public String description(){
return description;
}
@ -104,6 +108,15 @@ public class Time implements Mappable{ @@ -104,6 +108,15 @@ public class Time implements Mappable{
);
}
public Time patch(JSONObject json) {
if (json.has(SUBJECT) && json.get(SUBJECT) instanceof String s) subject = s;
if (json.has(DESCRIPTION) && json.get(DESCRIPTION) instanceof String d) description = d;
if (json.has(START_TIME) && json.get(START_TIME) instanceof String st) start = LocalDateTime.parse(st, DT);
if (json.has(END_TIME) && json.get(END_TIME) instanceof String e) end = LocalDateTime.parse(e,DT);
if (end != null && !start.isBefore(end)) throw UmbrellaException.invalidFieldException(END_TIME,"after start_time");
return this;
}
public void setId(long newValue) {
id = newValue;
}

24
frontend/src/Components/DateTimeEditor.svelte

@ -0,0 +1,24 @@ @@ -0,0 +1,24 @@
<script>
let { onSet = (dateTime) => {}, value = ' ' } = $props();
let date = $state(value.split(' ')[0]);
let time = $state(value.split(' ')[1]);
console.log({date:date,time:time,value:value});
function handleSubmit(e){
e.preventDefault();
onSet(`${date} ${time}`);
}
</script>
<style>
button{ display: none }
</style>
<form onsubmit={handleSubmit} >
<input type="date" bind:value={date} />
<input type="time" bind:value={time} />
<button type="submit">ok</button>
</form>

6
frontend/src/Components/LineEditor.svelte

@ -3,14 +3,15 @@ @@ -3,14 +3,15 @@
import { t } from '../translations.svelte.js';
let {
editable = false,
simple = false,
editable = simple,
onclick = evt => { startEdit() },
onSet = newVal => {return true;},
type = 'div',
value = $bindable(null)
} = $props();
let editing = $state(false);
let editing = $state(simple);
let editValue = value;
let start = 0;
@ -64,6 +65,7 @@ @@ -64,6 +65,7 @@
}
activeField.subscribe((val) => resetEdit());
if (simple) startEdit();
</script>
<style>

52
frontend/src/routes/time/Index.svelte

@ -4,9 +4,14 @@ @@ -4,9 +4,14 @@
import { api } from '../../urls.svelte.js';
import { t } from '../../translations.svelte.js';
import DateTimeEditor from '../../Components/DateTimeEditor.svelte';
import LineEditor from '../../Components/LineEditor.svelte';
import MarkdownEditor from '../../Components/MarkdownEditor.svelte';
let error = $state(null);
let router = useTinyRouter();
let times = $state(null);
let detail = $state(null);
async function loadTimes(){
const url = api('time');
@ -22,9 +27,32 @@ @@ -22,9 +27,32 @@
router.navigate(`task/${tid}/view`);
}
async function update(tid,field,newVal){
times[tid][field] = newVal;
detail = null;
const url = api(`time/${tid}`);
const res = await fetch(url,{
credentials:'include',
method:'PATCH',
body:JSON.stringify(times[tid])
});
if (res.ok){
times[tid] = await res.json();
error = null;
return true;
} else {
error = await res.text();
return false;
}
}
onMount(loadTimes);
</script>
<style>
td { vertical-align: top; }
</style>
<h1>{t('timetracking')}</h1>
{#if error}
<span class="error">{error}</span>
@ -32,11 +60,32 @@ @@ -32,11 +60,32 @@
{#if times}
<table class="timetracks">
<thead>
<tr>
<th>{t('start_end')}</th>
<th>{t('duration')}</th>
<th>{t('subject')}</th>
<th>{t('tasks')}</th>
<th>{t('state')}</th>
</tr>
</thead>
<tbody>
{#each Object.entries(times) as [tid,time]}
{#if detail == tid}
<tr>
<td>
<div>
<DateTimeEditor value={time.start_time} onSet={dateTime => update(tid,'start_time',dateTime)} />
<DateTimeEditor value={time.end_time} onSet={dateTime => update(tid,'end_time',dateTime)} />
</div>
</td>
<td colspan="2">
<LineEditor simple={true} value={time.subject} onSet={subject => update(tid,'subject',subject)} />
<MarkdownEditor simple={true} value={time.description} onSet={desc => update(tid,'description',desc)} />
</td>
</tr>
{:else}
<tr onclick={e => {detail = tid}}>
<td class="start_end">
{time.start_time}{#if time.end_time}{time.end_time}{/if}
</td>
@ -56,6 +105,7 @@ @@ -56,6 +105,7 @@
{t("state_"+time.state.name.toLowerCase())}
</td>
</tr>
{/if}
{/each}
</tbody>
</table>

25
time/src/main/java/de/srsoftware/umbrella/time/TimeModule.java

@ -77,6 +77,23 @@ public class TimeModule extends BaseHandler implements TimeService { @@ -77,6 +77,23 @@ public class TimeModule extends BaseHandler implements TimeService {
return sendContent(ex,time);
}
@Override
public boolean doPatch(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 patchTime(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 doPost(Path path, HttpExchange ex) throws IOException {
addCors(ex);
@ -101,6 +118,14 @@ public class TimeModule extends BaseHandler implements TimeService { @@ -101,6 +118,14 @@ public class TimeModule extends BaseHandler implements TimeService {
.max(Comparator.comparing(Time::start));
}
private boolean patchTime(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 alter this time!");
var json = json(ex);
timeDb.save(time.patch(json));
return sendContent(ex,time);
}
private boolean trackTask(UmbrellaUser user, Path path, HttpExchange ex) throws IOException {
if (path.empty()) throw missingFieldException(TASK_ID);
Task task;

Loading…
Cancel
Save