-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_exists.go
83 lines (76 loc) · 2.59 KB
/
check_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
package lightweight_api
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/ssst0n3/awesome_libs"
"github.com/ssst0n3/awesome_libs/awesome_error"
"github.com/ssst0n3/awesome_libs/awesome_reflect"
"strconv"
)
func (r *Resource) CheckResourceExistsByIdAutoParseParam(c *gin.Context) (bool, int64, error) {
paramId := c.Param("id")
id, err := strconv.ParseInt(paramId, 10, 64)
if err != nil {
awesome_error.CheckErr(err)
return false, id, err
}
exists, err := r.CheckResourceExistsById(uint(id))
return exists, id, err
}
func (r *Resource) CheckResourceExistsById(id uint) (exists bool, err error) {
var count int64
model := awesome_reflect.EmptyPointerOfModel(r.Model)
err = DB.Find(model, id).Count(&count).Error
if err != nil {
awesome_error.CheckErr(err)
return
}
exists = count > 0
return
}
func (r *Resource) CheckResourceExistsByGuid(guidColName string, guidValue interface{}) (exists bool) {
var count int64
err := DB.Table(r.TableName).Where(map[string]interface{}{guidColName: guidValue}).Count(&count).Error
if err != nil {
awesome_error.CheckErr(err)
return
}
exists = count > 0
return
}
func (r *Resource) CheckResourceExistsByModelPtrWithGuid(modelPtr interface{}, GuidFieldJsonTag string) bool {
awesome_reflect.MustPointer(modelPtr)
if GuidFieldJsonTag == "" {
// please make sure by developer
panic(GuidTagMustNotBeEmpty)
}
guidFiled, find := awesome_reflect.FieldByJsonTag(awesome_reflect.Value(modelPtr), GuidFieldJsonTag)
if !find {
// please make sure by developer
panic(fmt.Sprintf(FieldCannotFind, GuidFieldJsonTag))
}
guidValue := guidFiled.Interface()
exists := r.CheckResourceExistsByGuid(GuidFieldJsonTag, guidValue)
return exists
}
func (r *Resource) CheckResourceExistsExceptSelfByGuid(guidColName string, guidValue interface{}, id uint) bool {
var count int64
whereQuery := awesome_libs.Format("{.guid}=? AND id <>? ", awesome_libs.Dict{"guid": guidColName})
DB.Table(r.TableName).Where(whereQuery, guidValue, id).Count(&count)
return count > 0
}
func (r *Resource) CheckResourceExistsExceptSelfByModelPtrWithGuid(modelPtr interface{}, GuidFieldJsonTag string, id uint) bool {
awesome_reflect.MustPointer(modelPtr)
if GuidFieldJsonTag == "" {
// please make sure by developer
panic(GuidTagMustNotBeEmpty)
}
guidFiled, find := awesome_reflect.FieldByJsonTag(awesome_reflect.Value(modelPtr), GuidFieldJsonTag)
if !find {
// please make sure by developer
panic(fmt.Sprintf(FieldCannotFind, GuidFieldJsonTag))
}
guidValue := guidFiled.Interface()
exists := r.CheckResourceExistsExceptSelfByGuid(GuidFieldJsonTag, guidValue, id)
return exists
}