96 lines
1.7 KiB
Go
96 lines
1.7 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"gitea.tecamino.com/paadi/memberDB/models"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func (a *APIHandler) AddNewResponsible(c *gin.Context) {
|
|
if !a.databaseOpened(c) {
|
|
return
|
|
}
|
|
|
|
var responsible models.Person
|
|
err := c.BindJSON(&responsible)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
err = a.DbHandler.AddNewResponsible(responsible)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"message": "responsible added",
|
|
})
|
|
}
|
|
|
|
func (a *APIHandler) GetResponsibleById(c *gin.Context) {
|
|
if !a.databaseOpened(c) {
|
|
return
|
|
}
|
|
|
|
var i int
|
|
var err error
|
|
|
|
id := c.Query("id")
|
|
if id != "" {
|
|
i, err = strconv.Atoi(id)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
}
|
|
|
|
members, err := a.DbHandler.GetResponsible(i)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, members)
|
|
}
|
|
|
|
func (a *APIHandler) DeleteResponsible(c *gin.Context) {
|
|
if !a.databaseOpened(c) {
|
|
return
|
|
}
|
|
var err error
|
|
|
|
var request struct {
|
|
Ids []int `json:"ids"`
|
|
}
|
|
err = c.BindJSON(&request)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
err = a.DbHandler.DeleteResponsible(request.Ids...)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"message": "responsible(s) deleted",
|
|
})
|
|
}
|