-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_controller.go
More file actions
38 lines (30 loc) · 923 Bytes
/
base_controller.go
File metadata and controls
38 lines (30 loc) · 923 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package gin_crud
import (
"github.com/gin-gonic/gin"
"github.com/zubroide/gorm-crud"
"net/http"
)
const StatusOk = "ok"
const StatusError = "error"
type BaseControllerInterface interface {
}
type BaseController struct {
Logger gorm_crud.LoggerInterface
}
func NewBaseController(logger gorm_crud.LoggerInterface) *BaseController {
return &BaseController{Logger: logger}
}
func (c BaseController) ReplySuccess(context *gin.Context, data interface{}) {
c.Response(context, gin.H{"data": data, "status": StatusOk}, http.StatusOK)
}
func (c BaseController) ReplyError(context *gin.Context, message string, code int) {
c.Response(context, gin.H{"message": message, "status": StatusError}, code)
}
func (c BaseController) Response(context *gin.Context, obj interface{}, code int) {
switch context.GetHeader("Accept") {
case "application/xml":
context.XML(code, obj)
default:
context.JSON(code, obj)
}
}