qa: New route to assign all exercices

This commit is contained in:
nemunaire 2024-09-18 12:32:11 +02:00
parent 1f295c3411
commit 2f6c7ecd8b
2 changed files with 93 additions and 3 deletions

View File

@ -1,8 +1,11 @@
package api
import (
"fmt"
"log"
"net/http"
"strconv"
"strings"
"srs.epita.fr/fic-server/libfic"
@ -17,6 +20,7 @@ func declareTodoRoutes(router *gin.RouterGroup) {
}
func declareTodoManagerRoutes(router *gin.RouterGroup) {
router.POST("/qa_assign_work", assignWork)
router.POST("/qa_my_exercices.json", addQAView)
router.POST("/qa_work.json", createQATodo)
@ -235,3 +239,55 @@ func deleteQATodo(c *gin.Context) {
c.Status(http.StatusOK)
}
}
type QAAssignWork struct {
Turns int `json:"turns"`
Start int `json:"start"`
}
func assignWork(c *gin.Context) {
var uaw QAAssignWork
if err := c.ShouldBindJSON(&uaw); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
if uaw.Turns == 0 {
uaw.Turns = 1
}
teams, err := fic.GetTeams()
if err != nil {
log.Println("Unable to GetTeams: ", err.Error())
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to list teams: %s", err.Error())})
return
}
// Remove assistant team
for tid := len(teams) - 1; tid >= 0; tid-- {
team := teams[tid]
if strings.Contains(strings.ToLower(team.Name), "assistants") {
teams = append(teams[:tid], teams[tid+1:]...)
}
}
exercices, err := fic.GetExercices()
if err != nil {
log.Println("Unable to GetExercices: ", err.Error())
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to list exercices: %s", err.Error())})
return
}
for i := 0; i < uaw.Turns; i++ {
for eid, ex := range exercices {
team := teams[(uaw.Start+eid+uaw.Turns*i)%len(teams)]
_, err := team.NewQATodo(ex.Id)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
}
}
c.JSON(http.StatusOK, "true")
}

View File

@ -4,7 +4,9 @@
import { teams } from '$lib/stores/teams';
import {
Button,
Container,
Input,
Table,
} from '@sveltestrap/sveltestrap';
@ -16,12 +18,44 @@
function show(id) {
goto("teams/" + id)
}
let start = 0;
let turns = 3;
async function assignExercices() {
const res = await fetch(`api/qa_assign_work`, {
method: 'POST',
headers: {'Accept': 'application/json'},
body: JSON.stringify({
start,
turns,
}),
})
if (res.status == 200) {
teams.refresh();
} else {
throw new Error((await res.json()).errmsg);
}
}
</script>
<Container class="mt-2 mb-5">
<h2>
Équipes
</h2>
<div class="d-flex justify-content-between">
<h2>
Équipes
</h2>
<div class="input-group">
<Input type="number" bind:value={start} />
<Input type="number" bind:value={turns} />
<Button
color="light"
size="sm"
on:click={assignExercices}
>
Assigner des exercices
</Button>
</div>
</div>
<p>
<input type="search" class="form-control" placeholder="Filtrer" bind:value={query} autofocus>