Add a route to update NeedHelp entries
This commit is contained in:
parent
6c1ca2a23b
commit
bb7c4f32e4
44
help.go
44
help.go
@ -1,8 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@ -19,6 +21,29 @@ func declareAPIAdminHelpRoutes(router *gin.RouterGroup) {
|
||||
|
||||
c.JSON(http.StatusOK, nhs)
|
||||
})
|
||||
|
||||
needhelpsRoutes := router.Group("/help/:hid")
|
||||
needhelpsRoutes.Use(needHelpHandler)
|
||||
|
||||
needhelpsRoutes.PUT("", func(c *gin.Context) {
|
||||
current := c.MustGet("needhelp").(*NeedHelp)
|
||||
|
||||
var new NeedHelp
|
||||
if err := c.ShouldBindJSON(&new); err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
new.Id = current.Id
|
||||
|
||||
if err := new.Update(); err != nil {
|
||||
log.Println("Unable to Update needhelp:", err)
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("An error occurs during needhelp entry updation: %s", err.Error())})
|
||||
return
|
||||
} else {
|
||||
c.JSON(http.StatusOK, new)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func declareAPIAuthHelpRoutes(router *gin.RouterGroup) {
|
||||
@ -36,6 +61,19 @@ func declareAPIAuthHelpRoutes(router *gin.RouterGroup) {
|
||||
})
|
||||
}
|
||||
|
||||
func needHelpHandler(c *gin.Context) {
|
||||
if hid, err := strconv.Atoi(string(c.Param("hid"))); err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Bad need help identifier."})
|
||||
return
|
||||
} else if nh, err := getNeedHelp(hid); err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Need help entry not found."})
|
||||
return
|
||||
} else {
|
||||
c.Set("needhelp", nh)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
type NeedHelp struct {
|
||||
Id int64 `json:"id"`
|
||||
IdUser int64 `json:"id_user"`
|
||||
@ -65,6 +103,12 @@ func getNeedHelps() (nh []NeedHelp, err error) {
|
||||
}
|
||||
}
|
||||
|
||||
func getNeedHelp(id int) (n *NeedHelp, err error) {
|
||||
n = new(NeedHelp)
|
||||
err = DBQueryRow("SELECT id_need_help, id_user, date, comment, date_treated FROM user_need_help WHERE id_need_help=?", id).Scan(&n.Id, &n.IdUser, &n.Date, &n.Comment, &n.DateTreated)
|
||||
return
|
||||
}
|
||||
|
||||
func (u *User) NewNeedHelp() (NeedHelp, error) {
|
||||
if res, err := DBExec("INSERT INTO user_need_help (id_user, comment) VALUES (?, ?)", u.Id, ""); err != nil {
|
||||
return NeedHelp{}, err
|
||||
|
Reference in New Issue
Block a user