Files
nCovTrack-Backend/handler/management.go
fallen-angle 9e3638885d finish
2022-05-16 19:55:59 +08:00

235 lines
6.9 KiB
Go

package handler
import (
"nCovTrack-Backend/global"
"nCovTrack-Backend/service/management"
"nCovTrack-Backend/utils"
"strconv"
"github.com/gin-gonic/gin"
)
//PullContactHandler pull contact from faker
// @Tags Management
// @Produce json
// @Summary pull contact from faker
// @Success 200 {object} utils.GinResponse
// @Router /management/faker/{patientId} [get]
// @Param Token header string true "Token"
// @Param patientId path int true "Patient Id"
func PullContactHandler(c *gin.Context) {
claims := utils.ClaimsFromHeader(c)
if claims.Role != global.ROLE_ID_MAP["ADMIN"] {
Forbidden(c)
return
}
patientId, _ := strconv.Atoi(c.Param("patientId"))
rowsAffected := management.PullFromFaker(claims, patientId)
if rowsAffected == -1 {
DataNotFound(c, map[string]interface{}{"patientId": patientId})
return
}
utils.Succ(c, map[string]interface{}{"pullAmount": rowsAffected})
}
//GetObservationHandler get observation's info
// @Tags Management
// @Produce json
// @Summary get observation's info
// @Success 200 {object} utils.GinResponse{data=models.BackObservation}
// @Router /management/observation/{id} [get]
// @Param token header string true "token"
// @Param id path int true "id"
func GetObservationHandler(c *gin.Context) {
claims := utils.ClaimsFromHeader(c)
allowRows := []int{global.ROLE_ID_MAP["ADMIN"], global.ROLE_ID_MAP["WORKER"], global.ROLE_ID_MAP["VOLUNTEER"]}
if !utils.Contains(allowRows, claims.Role) {
Forbidden(c)
return
}
id, _ := strconv.Atoi(c.Param("id"))
utils.Succ(c, management.GetObservation(id))
}
//ListObservationsHandler list observations by query
// @Tags Management
// @Produce json
// @Summary list observations by query
// @Success 200 {object} utils.GinResponse{data=[]models.ListObservation}
// @Router /management/observation/ [get]
// @Param Token header string true "token"
// @Param queries query models.BackObservation true "queries"
func ListObservationsHandler(c *gin.Context) {
claims := utils.ClaimsFromHeader(c)
allowRows := []int{global.ROLE_ID_MAP["ADMIN"], global.ROLE_ID_MAP["WORKER"], global.ROLE_ID_MAP["VOLUNTEER"]}
if !utils.Contains(allowRows, claims.Role) {
Forbidden(c)
return
}
jsonMap := bindQuery(c)
utils.Succ(c, management.ListObservation(jsonMap))
}
//TreeifyObservationHandler get transform chain by user
// @Tags Management
// @Produce json
// @Summary get transform chain by user
// @Success 200 {object} utils.GinResponse{data=models.TreeObservation}
// @Router /management/observation/tree/{id}/{direction} [get]
// @Param Token header string true "token"
// @Param id path int true "id"
// @Param direction path string true "direction" Enums(up, down, all)
func TreeifyObservationHandler(c *gin.Context) {
claims := utils.ClaimsFromHeader(c)
allowRows := []int{global.ROLE_ID_MAP["ADMIN"], global.ROLE_ID_MAP["WORKER"], global.ROLE_ID_MAP["VOLUNTEER"]}
if !utils.Contains(allowRows, claims.Role) {
Forbidden(c)
return
}
id, _ := strconv.Atoi(c.Param("id"))
direction := c.Param("direction")
tree := management.TreeifyObservations(id, direction)
utils.Succ(c, tree)
}
//InsertObservationHandler insert observation
// @Tags Management
// @Accept json
// @Produce json
// @Summary insert observation
// @Success 200 {object} utils.GinResponse
// @Router /management/observation/ [post]
// @Param Observation body models.BackObservation true "observation"
// @Param Token header string true "token"
func InsertObservationHandler(c *gin.Context) {
claims := utils.ClaimsFromHeader(c)
allowRows := []int{global.ROLE_ID_MAP["ADMIN"], global.ROLE_ID_MAP["WORKER"]}
if !utils.Contains(allowRows, claims.Role) {
Forbidden(c)
return
}
observaion := bindJson(c)
delete(observaion, "id")
ok := management.InsertObservation(claims, observaion)
if ok {
utils.Succ(c, nil)
} else {
OperationFailed(c)
}
}
//UpdateObservationHandler update observation
// @Tags Management
// @Accept json
// @Produce json
// @Summary update observation
// @Success 200 {object} utils.GinResponse
// @Router /management/observation/ [put]
// @Param Observation body models.BackObservation true "observation"
// @Param Token header string true "token"
func UpdateObservationHandler(c *gin.Context) {
claims := utils.ClaimsFromHeader(c)
allowRows := []int{global.ROLE_ID_MAP["ADMIN"], global.ROLE_ID_MAP["WORKER"]}
if !utils.Contains(allowRows, claims.Role) {
Forbidden(c)
return
}
observation := bindJson(c)
ok := management.UpdateObservation(claims, observation)
if ok {
utils.Succ(c, nil)
} else {
OperationFailed(c)
}
}
//ListLocationHandler list locations
// @Tags Management
// @Produce json
// @Summary list locations
// @Success 200 {object} utils.GinResponse{data=models.BackLocation}
// @Router /management/location/ [get]
// @Param queries query models.BackLocation true "queries"
// @Param Token header string true "token"
func ListLocationHandler(c *gin.Context) {
claims := utils.ClaimsFromHeader(c)
allowRows := []int{global.ROLE_ID_MAP["ADMIN"], global.ROLE_ID_MAP["WORKER"], global.ROLE_ID_MAP["VOLUNTEER"]}
if !utils.Contains(allowRows, claims.Role) {
Forbidden(c)
return
}
queryMap := bindQuery(c)
utils.Succ(c, management.ListLocation(queryMap))
}
//InsertLocationHandler insert location
// @Tags Management
// @Accept json
// @Produce json
// @Summary insert location
// @Success 200 {object} utils.GinResponse
// @Router /management/location/ [post]
// @Param Location body models.BackLocation true "location"
// @Param Token header string true "token"
func InsertLocationHandler(c *gin.Context) {
claims := utils.ClaimsFromHeader(c)
if claims.Role != global.ROLE_ID_MAP["ADMIN"] {
Forbidden(c)
return
}
locationMap := bindJson(c)
ok := management.InsertLocation(claims, locationMap)
if ok {
utils.Succ(c, nil)
} else {
OperationFailed(c)
}
}
//DeleteLocationHandler delete location
// @Tags Management
// @Produce json
// @Summary delete location
// @Success 200 {object} utils.GinResponse
// @Router /management/location/{id} [delete]
// @Param Token header string true "token"
// @Param Id path int true "Id"
func DeleteLocationHandler(c *gin.Context) {
claims := utils.ClaimsFromHeader(c)
if claims.Role != global.ROLE_ID_MAP["ADMIN"] {
Forbidden(c)
return
}
id, _ := strconv.Atoi(c.Param("id"))
ok := management.DeleteLocation(id)
if ok {
utils.Succ(c, nil)
} else {
OperationFailed(c)
}
}
//InsertPcrHandler insert pcr record
// @Tags Management
// @Produce json
// @Summary insert pcr record
// @Success 200 {object} utils.GinResponse
// @Router /management/pcr/ [post]
// @Param Pcr body models.BackPcr true "Pcr"
// @Param Token header string true "token"
func InsertPcrHandler(c *gin.Context) {
claims := utils.ClaimsFromHeader(c)
allowRows := []int{global.ROLE_ID_MAP["ADMIN"], global.ROLE_ID_MAP["WORKER"]}
if !utils.Contains(allowRows, claims.Role) {
Forbidden(c)
return
}
jsonMap := bindJson(c)
ok := management.InsertPcr(claims, jsonMap)
if ok {
utils.Succ(c, nil)
} else {
OperationFailed(c)
}
}