add ability to agree with results closes #96

This commit is contained in:
Andre Henriques 2024-04-16 19:03:30 +01:00
parent f165e9e744
commit 00ddb91a22
4 changed files with 76 additions and 6 deletions

29
logic/tasks/agreement.go Normal file
View File

@ -0,0 +1,29 @@
package tasks
import (
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types"
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/tasks/utils"
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils"
)
func handleRequests(x *Handle) {
type AgreementRequest struct {
Id string `json:"id" validate:"required"`
Agreement int `json:"agreement" validate:"required"`
}
PostAuthJson(x, "/task/agreement", User_Normal, func(c *Context, dat *AgreementRequest) *Error {
var task Task
err := GetDBOnce(c, &task, "tasks where id=$1", dat.Id)
if err == ModelNotFoundError {
return c.JsonBadRequest("Model not found")
} else if err != nil {
return c.E500M("Failed to get task data", err)
}
err = task.SetAgreement(c, TaskAgreement(dat.Agreement))
if err != nil {
return c.E500M("Failed to update task data", err)
}
return c.SendJSON(JustId{Id: dat.Id})
})
}

View File

@ -4,8 +4,8 @@ import (
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils"
)
func HandleTasks (handle *Handle) {
handleUpload(handle)
handleList(handle)
func HandleTasks(handle *Handle) {
handleUpload(handle)
handleList(handle)
handleRequests(handle)
}

View File

@ -41,10 +41,23 @@ const (
TASK_TYPE_RETRAINING
)
type TaskAgreement int
const (
TASK_AGREE_DISAGRE TaskAgreement = -1 + iota
TASK_AGREE_NONE
TASK_AGREE_AGREE
)
func (t Task) UpdateStatus(base BasePack, status TaskStatus, message string) (err error) {
return UpdateTaskStatus(base, t.Id, status, message)
}
func (t Task) SetAgreement(base BasePack, agreement TaskAgreement) (err error) {
_, err = base.GetDb().Exec("update tasks set user_confirmed=$1 where id=$2", agreement, t.Id)
return
}
/**
* Call the UpdateStatus function and logs on the case of failure!
* This varient does not return any error message

View File

@ -32,10 +32,11 @@
</script>
<script lang="ts">
import { post } from 'src/lib/requests.svelte';
import { post, showMessage } from 'src/lib/requests.svelte';
import type { Model } from './+page.svelte';
import MessageSimple from 'src/lib/MessageSimple.svelte';
let { model } = $props<{ model: Model; uploadCounter?: number }>();
let { model }: { model: Model } = $props();
let page = $state(0);
let showNext = $state(false);
@ -59,10 +60,27 @@
getList();
}
});
let userPreceptionMessages: MessageSimple;
// This returns a function that performs the call and does not do the call it self
function userPreception(task: string, agree: number) {
return async function () {
try {
await post('task/agreement', {
id: task,
agreement: agree
});
getList();
} catch (e) {
showMessage(e, userPreception);
}
};
}
</script>
<div>
<h2>Tasks</h2>
<MessageSimple bind:this={userPreceptionMessages} />
<table>
<thead>
<tr>
@ -116,6 +134,16 @@
{:else}
TODO {task.user_confirmed}
{/if}
{#if task.user_confirmed == -1}
<button type="button" on:click={userPreception(task.id, 1)}>
<span class="bi bi-check"></span>
</button>
{/if}
{#if task.user_confirmed == 1}
<button class="danger" type="button" on:click={userPreception(task.id, -1)}>
<span class="bi bi-x-lg"></span>
</button>
{/if}
{:else}
-
{/if}