admin: Use gin-gonic as router
This commit is contained in:
parent
83468ad723
commit
8b3fbdb64a
32 changed files with 2785 additions and 1635 deletions
|
@ -2,62 +2,152 @@ package api
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"path"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"srs.epita.fr/fic-server/libfic"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func init() {
|
||||
router.GET("/api/teams/:tid/issue.json", apiHandler(teamHandler(
|
||||
func(team *fic.Team, _ []byte) (interface{}, error) {
|
||||
return team.MyIssueFile()
|
||||
})))
|
||||
|
||||
func declareClaimsRoutes(router *gin.RouterGroup) {
|
||||
// Tasks
|
||||
router.GET("/api/claims", apiHandler(getClaims))
|
||||
router.POST("/api/claims", apiHandler(newClaim))
|
||||
router.DELETE("/api/claims", apiHandler(clearClaims))
|
||||
router.GET("/api/teams/:tid/claims", apiHandler(teamHandler(getTeamClaims)))
|
||||
router.GET("/api/exercices/:eid/claims", apiHandler(exerciceHandler(getExerciceClaims)))
|
||||
router.GET("/api/themes/:thid/exercices/:eid/claims", apiHandler(exerciceHandler(getExerciceClaims)))
|
||||
router.GET("/claims", getClaims)
|
||||
router.POST("/claims", newClaim)
|
||||
router.DELETE("/claims", clearClaims)
|
||||
|
||||
router.GET("/api/claims/:cid", apiHandler(claimHandler(showClaim)))
|
||||
router.PUT("/api/claims/:cid", apiHandler(claimHandler(updateClaim)))
|
||||
router.POST("/api/claims/:cid", apiHandler(claimHandler(addClaimDescription)))
|
||||
router.DELETE("/api/claims/:cid", apiHandler(claimHandler(deleteClaim)))
|
||||
apiClaimsRoutes := router.Group("/claims/:cid")
|
||||
apiClaimsRoutes.Use(ClaimHandler)
|
||||
apiClaimsRoutes.GET("", showClaim)
|
||||
apiClaimsRoutes.PUT("", updateClaim)
|
||||
apiClaimsRoutes.POST("", addClaimDescription)
|
||||
apiClaimsRoutes.DELETE("", deleteClaim)
|
||||
|
||||
router.GET("/api/claims/:cid/last_update", apiHandler(claimHandler(getClaimLastUpdate)))
|
||||
router.PUT("/api/claims/:cid/descriptions", apiHandler(claimHandler(updateClaimDescription)))
|
||||
apiClaimsRoutes.GET("/last_update", getClaimLastUpdate)
|
||||
apiClaimsRoutes.PUT("/descriptions", updateClaimDescription)
|
||||
|
||||
// Assignees
|
||||
router.GET("/api/claims-assignees", apiHandler(getAssignees))
|
||||
router.POST("/api/claims-assignees", apiHandler(newAssignee))
|
||||
router.GET("/claims-assignees", getAssignees)
|
||||
router.POST("/claims-assignees", newAssignee)
|
||||
|
||||
router.GET("/api/claims-assignees/:aid", apiHandler(claimAssigneeHandler(showClaimAssignee)))
|
||||
router.PUT("/api/claims-assignees/:aid", apiHandler(claimAssigneeHandler(updateClaimAssignee)))
|
||||
router.DELETE("/api/claims-assignees/:aid", apiHandler(claimAssigneeHandler(deleteClaimAssignee)))
|
||||
apiClaimAssigneesRoutes := router.Group("/claims-assignees/:aid")
|
||||
apiClaimAssigneesRoutes.Use(ClaimAssigneeHandler)
|
||||
router.GET("/claims-assignees/:aid", showClaimAssignee)
|
||||
router.PUT("/claims-assignees/:aid", updateClaimAssignee)
|
||||
router.DELETE("/claims-assignees/:aid", deleteClaimAssignee)
|
||||
}
|
||||
|
||||
func getClaims(_ httprouter.Params, _ []byte) (interface{}, error) {
|
||||
return fic.GetClaims()
|
||||
func declareExerciceClaimsRoutes(router *gin.RouterGroup) {
|
||||
router.GET("/claims", getExerciceClaims)
|
||||
}
|
||||
|
||||
func getTeamClaims(team *fic.Team, _ []byte) (interface{}, error) {
|
||||
return team.GetClaims()
|
||||
func declareTeamClaimsRoutes(router *gin.RouterGroup) {
|
||||
router.GET("/api/teams/:tid/issue.json", func(c *gin.Context) {
|
||||
team := c.MustGet("team").(*fic.Team)
|
||||
|
||||
issues, err := team.MyIssueFile()
|
||||
if err != nil {
|
||||
log.Printf("Unable to MyIssueFile(tid=%d): %s", team.Id, err.Error())
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to generate issues.json."})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, issues)
|
||||
})
|
||||
|
||||
router.GET("/claims", getTeamClaims)
|
||||
}
|
||||
|
||||
func getExerciceClaims(exercice *fic.Exercice, _ []byte) (interface{}, error) {
|
||||
return exercice.GetClaims()
|
||||
func ClaimHandler(c *gin.Context) {
|
||||
cid, err := strconv.ParseInt(string(c.Params.ByName("cid")), 10, 64)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Invalid claim identifier"})
|
||||
return
|
||||
}
|
||||
|
||||
claim, err := fic.GetClaim(cid)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Requested claim not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.Set("claim", claim)
|
||||
|
||||
c.Next()
|
||||
}
|
||||
|
||||
func getClaimLastUpdate(claim *fic.Claim, _ []byte) (interface{}, error) {
|
||||
return claim.GetLastUpdate()
|
||||
func ClaimAssigneeHandler(c *gin.Context) {
|
||||
aid, err := strconv.ParseInt(string(c.Params.ByName("aid")), 10, 64)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Invalid claim assignee identifier"})
|
||||
return
|
||||
}
|
||||
|
||||
assignee, err := fic.GetAssignee(aid)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Requested claim-assignee not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.Set("claim-assignee", assignee)
|
||||
|
||||
c.Next()
|
||||
}
|
||||
|
||||
func getClaims(c *gin.Context) {
|
||||
claims, err := fic.GetClaims()
|
||||
|
||||
if err != nil {
|
||||
log.Println("Unable to getClaims:", err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during claims retrieval."})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, claims)
|
||||
}
|
||||
|
||||
func getTeamClaims(c *gin.Context) {
|
||||
team := c.MustGet("team").(*fic.Team)
|
||||
|
||||
claims, err := team.GetClaims()
|
||||
if err != nil {
|
||||
log.Printf("Unable to GetClaims(tid=%d): %s", team.Id, err.Error())
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to retrieve claim list."})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, claims)
|
||||
}
|
||||
|
||||
func getExerciceClaims(c *gin.Context) {
|
||||
exercice := c.MustGet("exercice").(*fic.Exercice)
|
||||
|
||||
claims, err := exercice.GetClaims()
|
||||
if err != nil {
|
||||
log.Printf("Unable to GetClaims(eid=%d): %s", exercice.Id, err.Error())
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to retrieve claim list."})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, claims)
|
||||
}
|
||||
|
||||
func getClaimLastUpdate(c *gin.Context) {
|
||||
claim := c.MustGet("claim").(*fic.Claim)
|
||||
|
||||
v, err := claim.GetLastUpdate()
|
||||
if err != nil {
|
||||
log.Printf("Unable to GetLastUpdate: %s", err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during claim last update retrieval."})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, v)
|
||||
}
|
||||
|
||||
type ClaimExported struct {
|
||||
|
@ -76,20 +166,26 @@ type ClaimExported struct {
|
|||
Descriptions []*fic.ClaimDescription `json:"descriptions"`
|
||||
}
|
||||
|
||||
func showClaim(claim *fic.Claim, _ []byte) (interface{}, error) {
|
||||
func showClaim(c *gin.Context) {
|
||||
claim := c.MustGet("claim").(*fic.Claim)
|
||||
|
||||
var e ClaimExported
|
||||
var err error
|
||||
if e.Team, err = claim.GetTeam(); err != nil {
|
||||
return nil, fmt.Errorf("Unable to find associated team: %w", err)
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Sprintf("Unable to find associated team: %s", err.Error())})
|
||||
return
|
||||
}
|
||||
if e.Exercice, err = claim.GetExercice(); err != nil {
|
||||
return nil, fmt.Errorf("Unable to find associated exercice: %w", err)
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Sprintf("Unable to find associated exercice: %s", err.Error())})
|
||||
return
|
||||
}
|
||||
if e.Assignee, err = claim.GetAssignee(); err != nil {
|
||||
return nil, fmt.Errorf("Unable to find associated assignee: %w", err)
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Sprintf("Unable to find associated assignee: %s", err.Error())})
|
||||
return
|
||||
}
|
||||
if e.Descriptions, err = claim.GetDescriptions(); err != nil {
|
||||
return nil, fmt.Errorf("Unable to find claim's descriptions: %w", err)
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Sprintf("Unable to find claim's descriptions: %s", err.Error())})
|
||||
return
|
||||
}
|
||||
|
||||
e.LastUpdate = e.Creation
|
||||
|
@ -107,7 +203,8 @@ func showClaim(claim *fic.Claim, _ []byte) (interface{}, error) {
|
|||
e.Creation = claim.Creation
|
||||
e.State = claim.State
|
||||
e.Priority = claim.Priority
|
||||
return e, nil
|
||||
|
||||
c.JSON(http.StatusOK, e)
|
||||
}
|
||||
|
||||
type ClaimUploaded struct {
|
||||
|
@ -115,20 +212,24 @@ type ClaimUploaded struct {
|
|||
Whoami *int64 `json:"whoami"`
|
||||
}
|
||||
|
||||
func newClaim(_ httprouter.Params, body []byte) (interface{}, error) {
|
||||
func newClaim(c *gin.Context) {
|
||||
var uc ClaimUploaded
|
||||
if err := json.Unmarshal(body, &uc); err != nil {
|
||||
return nil, fmt.Errorf("Unable to decode JSON: %w", err)
|
||||
err := c.ShouldBindJSON(&uc)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if uc.Subject == "" {
|
||||
return nil, errors.New("Claim's subject cannot be empty.")
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Claim's subject cannot be empty."})
|
||||
return
|
||||
}
|
||||
|
||||
var t *fic.Team
|
||||
if uc.IdTeam != nil {
|
||||
if team, err := fic.GetTeam(*uc.IdTeam); err != nil {
|
||||
return nil, fmt.Errorf("Unable to get associated team: %w", err)
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Sprintf("Unable to get associated team: %s", err.Error())})
|
||||
return
|
||||
} else {
|
||||
t = team
|
||||
}
|
||||
|
@ -139,7 +240,8 @@ func newClaim(_ httprouter.Params, body []byte) (interface{}, error) {
|
|||
var e *fic.Exercice
|
||||
if uc.IdExercice != nil {
|
||||
if exercice, err := fic.GetExercice(*uc.IdExercice); err != nil {
|
||||
return nil, fmt.Errorf("Unable to get associated exercice: %w", err)
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Sprintf("Unable to get associated exercice: %s", err.Error())})
|
||||
return
|
||||
} else {
|
||||
e = exercice
|
||||
}
|
||||
|
@ -150,7 +252,8 @@ func newClaim(_ httprouter.Params, body []byte) (interface{}, error) {
|
|||
var a *fic.ClaimAssignee
|
||||
if uc.IdAssignee != nil {
|
||||
if assignee, err := fic.GetAssignee(*uc.IdAssignee); err != nil {
|
||||
return nil, fmt.Errorf("Unable to get associated assignee: %w", err)
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Sprintf("Unable to get associated assignee: %s", err.Error())})
|
||||
return
|
||||
} else {
|
||||
a = assignee
|
||||
}
|
||||
|
@ -162,11 +265,25 @@ func newClaim(_ httprouter.Params, body []byte) (interface{}, error) {
|
|||
uc.Priority = "medium"
|
||||
}
|
||||
|
||||
return fic.NewClaim(uc.Subject, t, e, a, uc.Priority)
|
||||
claim, err := fic.NewClaim(uc.Subject, t, e, a, uc.Priority)
|
||||
if err != nil {
|
||||
log.Println("Unable to newClaim:", err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to register new claim"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, claim)
|
||||
}
|
||||
|
||||
func clearClaims(_ httprouter.Params, _ []byte) (interface{}, error) {
|
||||
return fic.ClearClaims()
|
||||
func clearClaims(c *gin.Context) {
|
||||
nb, err := fic.ClearClaims()
|
||||
if err != nil {
|
||||
log.Printf("Unable to clearClaims: %s", err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during claims clearing."})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, nb)
|
||||
}
|
||||
|
||||
func generateTeamIssuesFile(team fic.Team) error {
|
||||
|
@ -180,122 +297,189 @@ func generateTeamIssuesFile(team fic.Team) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func addClaimDescription(claim *fic.Claim, body []byte) (interface{}, error) {
|
||||
func addClaimDescription(c *gin.Context) {
|
||||
claim := c.MustGet("claim").(*fic.Claim)
|
||||
|
||||
var ud fic.ClaimDescription
|
||||
if err := json.Unmarshal(body, &ud); err != nil {
|
||||
return nil, fmt.Errorf("Unable to decode JSON: %w", err)
|
||||
err := c.ShouldBindJSON(&ud)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if assignee, err := fic.GetAssignee(ud.IdAssignee); err != nil {
|
||||
return nil, fmt.Errorf("Unable to get associated assignee: %w", err)
|
||||
} else if description, err := claim.AddDescription(ud.Content, assignee, ud.Publish); err != nil {
|
||||
return nil, fmt.Errorf("Unable to add description: %w", err)
|
||||
} else {
|
||||
if team, _ := claim.GetTeam(); team != nil {
|
||||
err = generateTeamIssuesFile(*team)
|
||||
assignee, err := fic.GetAssignee(ud.IdAssignee)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Sprintf("Unable to get associated assignee: %s", err.Error())})
|
||||
return
|
||||
}
|
||||
|
||||
description, err := claim.AddDescription(ud.Content, assignee, ud.Publish)
|
||||
if err != nil {
|
||||
log.Println("Unable to addClaimDescription:", err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to add description"})
|
||||
return
|
||||
}
|
||||
|
||||
if team, _ := claim.GetTeam(); team != nil {
|
||||
err = generateTeamIssuesFile(*team)
|
||||
if err != nil {
|
||||
log.Println("Unable to generateTeamIssuesFile after addClaimDescription:", err.Error())
|
||||
}
|
||||
|
||||
return description, err
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, description)
|
||||
}
|
||||
|
||||
func updateClaimDescription(claim *fic.Claim, body []byte) (interface{}, error) {
|
||||
func updateClaimDescription(c *gin.Context) {
|
||||
claim := c.MustGet("claim").(*fic.Claim)
|
||||
|
||||
var ud fic.ClaimDescription
|
||||
if err := json.Unmarshal(body, &ud); err != nil {
|
||||
return nil, fmt.Errorf("Unable to decode JSON: %w", err)
|
||||
err := c.ShouldBindJSON(&ud)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := ud.Update(); err != nil {
|
||||
return nil, fmt.Errorf("Unable to update description: %w", err)
|
||||
} else {
|
||||
if team, _ := claim.GetTeam(); team != nil {
|
||||
err = generateTeamIssuesFile(*team)
|
||||
}
|
||||
|
||||
return ud, err
|
||||
log.Println("Unable to updateClaimDescription:", err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "An error occurs during claim description updating."})
|
||||
return
|
||||
}
|
||||
if team, _ := claim.GetTeam(); team != nil {
|
||||
err = generateTeamIssuesFile(*team)
|
||||
if err != nil {
|
||||
log.Println("Unable to generateTeamIssuesFile:", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, ud)
|
||||
}
|
||||
|
||||
func updateClaim(claim *fic.Claim, body []byte) (interface{}, error) {
|
||||
func updateClaim(c *gin.Context) {
|
||||
claim := c.MustGet("claim").(*fic.Claim)
|
||||
|
||||
var uc ClaimUploaded
|
||||
if err := json.Unmarshal(body, &uc); err != nil {
|
||||
return nil, fmt.Errorf("Unable to decode JSON: %w", err)
|
||||
err := c.ShouldBindJSON(&uc)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
uc.Id = claim.Id
|
||||
|
||||
if _, err := uc.Update(); err != nil {
|
||||
return nil, fmt.Errorf("Unable to update claim: %w", err)
|
||||
} else {
|
||||
if claim.State != uc.State {
|
||||
if uc.Whoami != nil {
|
||||
if assignee, err := fic.GetAssignee(*uc.Whoami); err == nil {
|
||||
claim.AddDescription(fmt.Sprintf("%s a changé l'état de la tâche vers %q (était %q).", assignee.Name, uc.State, claim.State), assignee, true)
|
||||
}
|
||||
_, err = uc.Update()
|
||||
if err != nil {
|
||||
log.Printf("Unable to updateClaim: %s", err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during claim update."})
|
||||
return
|
||||
}
|
||||
|
||||
if claim.State != uc.State {
|
||||
if uc.Whoami != nil {
|
||||
if assignee, err := fic.GetAssignee(*uc.Whoami); err == nil {
|
||||
claim.AddDescription(fmt.Sprintf("%s a changé l'état de la tâche vers %q (était %q).", assignee.Name, uc.State, claim.State), assignee, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if claim.IdAssignee != uc.IdAssignee {
|
||||
if uc.Whoami != nil {
|
||||
if whoami, err := fic.GetAssignee(*uc.Whoami); err == nil {
|
||||
if uc.IdAssignee != nil {
|
||||
if assignee, err := fic.GetAssignee(*uc.IdAssignee); err == nil {
|
||||
if assignee.Id != whoami.Id {
|
||||
claim.AddDescription(fmt.Sprintf("%s a assigné la tâche à %s.", whoami.Name, assignee.Name), whoami, false)
|
||||
} else {
|
||||
claim.AddDescription(fmt.Sprintf("%s s'est assigné la tâche.", assignee.Name), whoami, false)
|
||||
}
|
||||
if claim.IdAssignee != uc.IdAssignee {
|
||||
if uc.Whoami != nil {
|
||||
if whoami, err := fic.GetAssignee(*uc.Whoami); err == nil {
|
||||
if uc.IdAssignee != nil {
|
||||
if assignee, err := fic.GetAssignee(*uc.IdAssignee); err == nil {
|
||||
if assignee.Id != whoami.Id {
|
||||
claim.AddDescription(fmt.Sprintf("%s a assigné la tâche à %s.", whoami.Name, assignee.Name), whoami, false)
|
||||
} else {
|
||||
claim.AddDescription(fmt.Sprintf("%s s'est assigné la tâche.", assignee.Name), whoami, false)
|
||||
}
|
||||
} else {
|
||||
claim.AddDescription(fmt.Sprintf("%s a retiré l'attribution de la tâche.", whoami.Name), whoami, false)
|
||||
}
|
||||
} else {
|
||||
claim.AddDescription(fmt.Sprintf("%s a retiré l'attribution de la tâche.", whoami.Name), whoami, false)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if team, _ := claim.GetTeam(); team != nil {
|
||||
err = generateTeamIssuesFile(*team)
|
||||
}
|
||||
if team, _ := claim.GetTeam(); team != nil {
|
||||
err = generateTeamIssuesFile(*team)
|
||||
}
|
||||
|
||||
return uc, err
|
||||
c.JSON(http.StatusOK, uc)
|
||||
}
|
||||
|
||||
func deleteClaim(c *gin.Context) {
|
||||
claim := c.MustGet("claim").(*fic.Claim)
|
||||
|
||||
if nb, err := claim.Delete(); err != nil {
|
||||
log.Println("Unable to deleteClaim:", err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during claim deletion."})
|
||||
return
|
||||
} else {
|
||||
c.JSON(http.StatusOK, nb)
|
||||
}
|
||||
}
|
||||
|
||||
func deleteClaim(claim *fic.Claim, _ []byte) (interface{}, error) {
|
||||
return claim.Delete()
|
||||
}
|
||||
|
||||
func getAssignees(_ httprouter.Params, _ []byte) (interface{}, error) {
|
||||
return fic.GetAssignees()
|
||||
}
|
||||
|
||||
func showClaimAssignee(assignee *fic.ClaimAssignee, _ []byte) (interface{}, error) {
|
||||
return assignee, nil
|
||||
}
|
||||
func newAssignee(_ httprouter.Params, body []byte) (interface{}, error) {
|
||||
var ua fic.ClaimAssignee
|
||||
if err := json.Unmarshal(body, &ua); err != nil {
|
||||
return nil, fmt.Errorf("Unable to decode JSON: %w", err)
|
||||
func getAssignees(c *gin.Context) {
|
||||
assignees, err := fic.GetAssignees()
|
||||
if err != nil {
|
||||
log.Println("Unable to getAssignees:", err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during assignees retrieval."})
|
||||
return
|
||||
}
|
||||
|
||||
return fic.NewClaimAssignee(ua.Name)
|
||||
c.JSON(http.StatusOK, assignees)
|
||||
}
|
||||
|
||||
func updateClaimAssignee(assignee *fic.ClaimAssignee, body []byte) (interface{}, error) {
|
||||
func showClaimAssignee(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, c.MustGet("claim-assignee").(*fic.ClaimAssignee))
|
||||
}
|
||||
func newAssignee(c *gin.Context) {
|
||||
var ua fic.ClaimAssignee
|
||||
if err := json.Unmarshal(body, &ua); err != nil {
|
||||
return nil, fmt.Errorf("Unable to decode JSON: %w", err)
|
||||
err := c.ShouldBindJSON(&ua)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
assignee, err := fic.NewClaimAssignee(ua.Name)
|
||||
if err != nil {
|
||||
log.Println("Unable to newAssignee:", err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during assignee creation."})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, assignee)
|
||||
}
|
||||
|
||||
func updateClaimAssignee(c *gin.Context) {
|
||||
assignee := c.MustGet("claim-assignee").(*fic.ClaimAssignee)
|
||||
|
||||
var ua fic.ClaimAssignee
|
||||
err := c.ShouldBindJSON(&ua)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
ua.Id = assignee.Id
|
||||
|
||||
if _, err := ua.Update(); err != nil {
|
||||
return nil, fmt.Errorf("Unable to update claim assignee: %w", err)
|
||||
} else {
|
||||
return ua, nil
|
||||
log.Println("Unable to updateClaimAssignee:", err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during claim assignee update."})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, ua)
|
||||
}
|
||||
|
||||
func deleteClaimAssignee(assignee *fic.ClaimAssignee, _ []byte) (interface{}, error) {
|
||||
return assignee.Delete()
|
||||
func deleteClaimAssignee(c *gin.Context) {
|
||||
assignee := c.MustGet("claim-assignee").(*fic.ClaimAssignee)
|
||||
|
||||
if _, err := assignee.Delete(); err != nil {
|
||||
log.Println("Unable to deleteClaimAssignee:", err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("An error occurs during claim assignee deletion: %s", err.Error())})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, true)
|
||||
}
|
||||
|
|
Reference in a new issue