-
Notifications
You must be signed in to change notification settings - Fork 0
/
must_exists.go
99 lines (90 loc) · 3.05 KB
/
must_exists.go
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package lightweight_api
import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"reflect"
)
func (r *Resource) MustResourceExistsGetModelByIdAutoParseParam(c *gin.Context) (id int64, model interface{}, err error) {
id, err = r.MustResourceExistsByIdAutoParseParam(c)
if err != nil {
return
}
//model, err = Conn.OrmShowObjectByIdUsingReflectRet(r.TableName, id, r.User)
//if err != nil {
// HandleInternalServerError(c, err)
// return
//}
//m := awesome_reflect.EmptyPointerOfModel(r.User)
m := reflect.New(reflect.TypeOf(r.Model))
DB.Table(r.TableName).First(m.Interface(), id)
model = m.Elem().Interface()
return
}
func (r *Resource) MustResourceExistsByIdAutoParseParam(c *gin.Context) (int64, error) {
exists, id, err := r.CheckResourceExistsByIdAutoParseParam(c)
if err != nil {
HandleInternalServerError(c, err)
return id, err
}
if !exists {
err := errors.New(fmt.Sprintf(ResourceMustExists, r.Name))
HandleStatusBadRequestError(c, err)
return id, err
}
return id, nil
}
func (r *Resource) MustResourceExistsById(c *gin.Context, id uint) error {
exists, err := r.CheckResourceExistsById(id)
if err != nil {
HandleStatusBadRequestError(c, err)
return err
}
if !exists {
err := errors.New(fmt.Sprintf(ResourceMustExists, r.Name))
HandleStatusBadRequestError(c, err)
return err
}
return nil
}
func (r *Resource) MustResourceExistsByGuid(c *gin.Context, guidColName string, guidValue interface{}) error {
if !r.CheckResourceExistsByGuid(guidColName, guidValue) {
err := errors.New(fmt.Sprintf(ResourceMustExists, r.Name))
HandleStatusBadRequestError(c, err)
return err
}
return nil
}
func (r *Resource) MustResourceNotExistsByGuid(c *gin.Context, guidColName string, guidValue interface{}) error {
if r.CheckResourceExistsByGuid(guidColName, guidValue) {
//err := errors.New(fmt.Sprintf(ResourceAlreadyExists, r.Name, guidColName, guidValue))
err := errors.New(fmt.Sprintf(GuidFieldMustNotExists, guidColName))
HandleStatusBadRequestError(c, err)
return err
}
return nil
}
func (r *Resource) MustResourceNotExistsByModelPtrWithGuid(c *gin.Context, modelPtr interface{}, GuidFieldJsonTag string) error {
if r.CheckResourceExistsByModelPtrWithGuid(modelPtr, GuidFieldJsonTag) {
err := errors.New(fmt.Sprintf(GuidFieldMustNotExists, GuidFieldJsonTag))
HandleStatusBadRequestError(c, err)
return err
}
return nil
}
func (r *Resource) MustResourceNotExistsExceptSelfByGuid(c *gin.Context, guidColName string, guidValue interface{}, id uint) error {
if r.CheckResourceExistsExceptSelfByGuid(guidColName, guidValue, id) {
err := errors.New(fmt.Sprintf(ResourceMustNotExistsExceptSelf, r.Name))
HandleStatusBadRequestError(c, err)
return err
}
return nil
}
func (r *Resource) MustResourceNotExistsExceptSelfByModelPtrWithGuid(c *gin.Context, modelPtr interface{}, GuidFieldJsonTag string, id uint) error {
if r.CheckResourceExistsExceptSelfByModelPtrWithGuid(modelPtr, GuidFieldJsonTag, id) {
err := errors.New(fmt.Sprintf(ResourceMustNotExistsExceptSelf, r.Name))
HandleStatusBadRequestError(c, err)
return err
}
return nil
}