admin: Really expose route to update team history

Related-to: a35aa7be70
This commit is contained in:
Pierre-Olivier Mercier 2024-03-12 10:21:13 +01:00
parent a0bc832910
commit c7fc18bfb4
2 changed files with 17 additions and 7 deletions

View File

@ -1,7 +1,6 @@
package api package api
import ( import (
"encoding/json"
"fmt" "fmt"
"log" "log"
"math/rand" "math/rand"
@ -146,6 +145,7 @@ func declareTeamsRoutes(router *gin.RouterGroup) {
c.JSON(http.StatusOK, history) c.JSON(http.StatusOK, history)
}) })
apiTeamsRoutes.PATCH("/history.json", updateHistory)
apiTeamsRoutes.DELETE("/history.json", delHistory) apiTeamsRoutes.DELETE("/history.json", delHistory)
apiTeamsPublicRoutes.GET("/tries", func(c *gin.Context) { apiTeamsPublicRoutes.GET("/tries", func(c *gin.Context) {
team := c.MustGet("team").(*fic.Team) team := c.MustGet("team").(*fic.Team)
@ -486,10 +486,14 @@ type uploadedHistory struct {
Coefficient float32 Coefficient float32
} }
func updateHistory(team *fic.Team, body []byte) (interface{}, error) { func updateHistory(c *gin.Context) {
team := c.MustGet("team").(*fic.Team)
var uh uploadedHistory var uh uploadedHistory
if err := json.Unmarshal(body, &uh); err != nil { err := c.ShouldBindJSON(&uh)
return nil, err if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
} }
var givenId int64 var givenId int64
@ -499,7 +503,13 @@ func updateHistory(team *fic.Team, body []byte) (interface{}, error) {
givenId = *uh.Primary givenId = *uh.Primary
} }
return team.UpdateHistoryCoeff(uh.Kind, uh.Time, givenId, uh.Coefficient) _, err = team.UpdateHistoryCoeff(uh.Kind, uh.Time, givenId, uh.Coefficient)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to update this history line: %s", err.Error())})
return
}
c.JSON(http.StatusOK, true)
} }
func delHistory(c *gin.Context) { func delHistory(c *gin.Context) {

View File

@ -55,7 +55,7 @@ func (t *Team) GetHistory() ([]map[string]interface{}, error) {
} }
// UpdateHistoryCoeff updates the coefficient for a given entry. // UpdateHistoryCoeff updates the coefficient for a given entry.
func (t *Team) UpdateHistoryCoeff(kind string, h time.Time, givenId int64, newCoeff float32) (interface{}, error) { func (t *Team) UpdateHistoryCoeff(kind string, h time.Time, givenId int64, newCoeff float32) (int64, error) {
if kind == "hint" { if kind == "hint" {
if res, err := DBExec("UPDATE team_hints SET coefficient = ? WHERE id_team = ? AND time = ? AND id_hint = ?", newCoeff, t.Id, h, givenId); err != nil { if res, err := DBExec("UPDATE team_hints SET coefficient = ? WHERE id_team = ? AND time = ? AND id_hint = ?", newCoeff, t.Id, h, givenId); err != nil {
return 0, err return 0, err
@ -81,7 +81,7 @@ func (t *Team) UpdateHistoryCoeff(kind string, h time.Time, givenId int64, newCo
return nb, err return nb, err
} }
} else { } else {
return nil, nil return 0, nil
} }
} }