forked from eddycjy/go-gin-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag.go
executable file
·114 lines (92 loc) · 2.35 KB
/
tag.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package models
import (
"github.com/jinzhu/gorm"
)
type Tag struct {
Model
Name string `json:"name"`
CreatedBy string `json:"created_by"`
ModifiedBy string `json:"modified_by"`
State int `json:"state"`
}
// ExistTagByName checks if there is a tag with the same name
func ExistTagByName(name string) (bool, error) {
var tag Tag
err := db.Select("id").Where("name = ? AND deleted_on = ? ", name, 0).First(&tag).Error
if err != nil && err != gorm.ErrRecordNotFound {
return false, err
}
if tag.ID > 0 {
return true, nil
}
return false, nil
}
// AddTag Add a Tag
func AddTag(name string, state int, createdBy string) error {
tag := Tag{
Name: name,
State: state,
CreatedBy: createdBy,
}
if err := db.Create(&tag).Error; err != nil {
return err
}
return nil
}
// GetTags gets a list of tags based on paging and constraints
func GetTags(pageNum int, pageSize int, maps interface{}) ([]Tag, error) {
var (
tags []Tag
err error
)
if pageSize > 0 && pageNum > 0 {
err = db.Where(maps).Find(&tags).Offset(pageNum).Limit(pageSize).Error
} else {
err = db.Where(maps).Find(&tags).Error
}
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}
return tags, nil
}
// GetTagTotal counts the total number of tags based on the constraint
func GetTagTotal(maps interface{}) (int, error) {
var count int
if err := db.Model(&Tag{}).Where(maps).Count(&count).Error; err != nil {
return 0, err
}
return count, nil
}
// ExistTagByID determines whether a Tag exists based on the ID
func ExistTagByID(id int) (bool, error) {
var tag Tag
err := db.Select("id").Where("id = ? AND deleted_on = ? ", id, 0).First(&tag).Error
if err != nil && err != gorm.ErrRecordNotFound {
return false, err
}
if tag.ID > 0 {
return true, nil
}
return false, nil
}
// DeleteTag delete a tag
func DeleteTag(id int) error {
if err := db.Where("id = ?", id).Delete(&Tag{}).Error; err != nil {
return err
}
return nil
}
// EditTag modify a single tag
func EditTag(id int, data interface{}) error {
if err := db.Model(&Tag{}).Where("id = ? AND deleted_on = ? ", id, 0).Updates(data).Error; err != nil {
return err
}
return nil
}
// CleanAllTag clear all tag
func CleanAllTag() (bool, error) {
if err := db.Unscoped().Where("deleted_on != ? ", 0).Delete(&Tag{}).Error; err != nil {
return false, err
}
return true, nil
}