mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00

- rename common package to database - move all code not related to database to new helper package - add more test in database package to improve code coverage - add a new (own) package for AMQP client
69 lines
1.5 KiB
Go
69 lines
1.5 KiB
Go
package helper
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/jinzhu/gorm"
|
|
"net/http"
|
|
)
|
|
|
|
func DBError(c *gin.Context, err error) bool {
|
|
if err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
NotFoundError(c, "Record not Found in DB: "+err.Error())
|
|
} else {
|
|
InternalServerError(c, "Error on DB Query or transaction: "+err.Error())
|
|
}
|
|
return true // Error
|
|
}
|
|
return false // No error
|
|
}
|
|
|
|
func BadRequestError(c *gin.Context, err string) {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"success": false,
|
|
"message": fmt.Sprintf("%v", err),
|
|
})
|
|
}
|
|
|
|
func UnprocessableEntityError(c *gin.Context, err string) {
|
|
c.JSON(http.StatusUnprocessableEntity, gin.H{
|
|
"success": false,
|
|
"message": fmt.Sprintf("%v", err),
|
|
})
|
|
}
|
|
|
|
func InternalServerError(c *gin.Context, err string) {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"success": false,
|
|
"message": fmt.Sprintf("%v", err),
|
|
})
|
|
}
|
|
|
|
func UnauthorizedError(c *gin.Context, err string) {
|
|
c.JSON(http.StatusUnauthorized, gin.H{
|
|
"success": false,
|
|
"message": fmt.Sprintf("%v", err),
|
|
})
|
|
}
|
|
|
|
func UnauthorizedAbort(c *gin.Context, err string) {
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
|
|
"succes": false,
|
|
"message": fmt.Sprintf("%v", err),
|
|
})
|
|
}
|
|
|
|
func NotFoundError(c *gin.Context, err string) {
|
|
c.JSON(http.StatusNotFound, gin.H{
|
|
"success": false,
|
|
"message": fmt.Sprintf("%v", err),
|
|
})
|
|
}
|
|
|
|
func ForbiddenError(c *gin.Context, err string) {
|
|
c.JSON(http.StatusForbidden, gin.H{
|
|
"success": false,
|
|
"message": fmt.Sprintf("%v", err),
|
|
})
|
|
}
|