New form to change grade
This commit is contained in:
parent
b604e98f64
commit
7642a23947
3 changed files with 80 additions and 2 deletions
28
works.go
28
works.go
|
@ -251,6 +251,26 @@ func declareAPIAdminWorksRoutes(router *gin.RouterGroup) {
|
|||
|
||||
gradesRoutes := worksRoutes.Group("/grades/:gid")
|
||||
gradesRoutes.Use(gradeHandler)
|
||||
gradesRoutes.PUT("", func(c *gin.Context) {
|
||||
current := c.MustGet("grade").(*WorkGrade)
|
||||
|
||||
var new WorkGrade
|
||||
if err := c.ShouldBindJSON(&new); err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
new.Id = current.Id
|
||||
|
||||
grade, err := new.Update()
|
||||
if err != nil {
|
||||
log.Println("Unable to Update grade:", err)
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during grade update."})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, grade)
|
||||
})
|
||||
gradesRoutes.DELETE("", func(c *gin.Context) {
|
||||
g := c.MustGet("grade").(*WorkGrade)
|
||||
|
||||
|
@ -654,6 +674,14 @@ type WorkGrade struct {
|
|||
Comment string `json:"comment,omitempty"`
|
||||
}
|
||||
|
||||
func (g *WorkGrade) Update() (*WorkGrade, error) {
|
||||
if _, err := DBExec("UPDATE user_work_grades SET id_user = ?, id_work = ?, date = ?, grade = ?, comment = ? WHERE id_gradation = ?", g.IdUser, g.IdWork, g.Date, g.Grade, g.Comment, g.Id); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return g, err
|
||||
}
|
||||
}
|
||||
|
||||
func (g WorkGrade) Delete() (int64, error) {
|
||||
if res, err := DBExec("DELETE FROM user_work_grades WHERE id_gradation = ?", g.Id); err != nil {
|
||||
return 0, err
|
||||
|
|
Reference in a new issue