-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.go
86 lines (73 loc) · 2.79 KB
/
entity.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
package repositorysdk
import (
"github.com/google/uuid"
gosdk "github.com/thinc-org/newbie-gosdk"
"gorm.io/gorm"
"time"
)
// Base is a struct that holds common fields for database tables, including the ID, creation and update timestamps,
// and soft deletion timestamp.
type Base struct {
ID *uuid.UUID `json:"id" gorm:"primary_key"`
CreatedAt time.Time `json:"created_at" gorm:"type:timestamp;autoCreateTime:nano"`
UpdatedAt time.Time `json:"updated_at" gorm:"type:timestamp;autoUpdateTime:nano"`
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index;type:timestamp"`
}
// BeforeCreate is a GORM callback that generates a new UUID for the ID field before creating a new record in the database.
func (b *Base) BeforeCreate(_ *gorm.DB) error {
if b.ID == nil {
b.ID = gosdk.UUIDAdr(uuid.New())
}
return nil
}
// BaseHardDelete is a struct that holds common fields for database tables, including the ID and creation and update timestamps,
// but excludes soft deletion timestamp.
type BaseHardDelete struct {
ID *uuid.UUID `json:"id" gorm:"primary_key"`
CreatedAt time.Time `json:"created_at" gorm:"type:timestamp;autoCreateTime:nano"`
UpdatedAt time.Time `json:"updated_at" gorm:"type:timestamp;autoUpdateTime:nano"`
}
// BeforeCreate is a GORM callback that generates a new UUID for the ID field before creating a new record in the database.
func (b *BaseHardDelete) BeforeCreate(_ *gorm.DB) error {
if b.ID == nil {
b.ID = gosdk.UUIDAdr(uuid.New())
}
return nil
}
// PaginationMetadata is a struct that holds pagination metadata including the number of items per page, the current page,
// the total number of items, and the total number of pages.
type PaginationMetadata struct {
ItemsPerPage int
ItemCount int
TotalItem int
CurrentPage int
TotalPage int
}
// GetOffset is a method that calculates the offset for the current page based on the number of items per page.
func (p *PaginationMetadata) GetOffset() int {
return (p.GetCurrentPage() - 1) * p.GetItemPerPage()
}
// GetItemPerPage is a method that returns the number of items per page, ensuring that the value is within a certain range.
func (p *PaginationMetadata) GetItemPerPage() int {
if p.ItemsPerPage < 10 {
p.ItemsPerPage = 10
}
if p.ItemsPerPage > 100 {
p.ItemsPerPage = 100
}
return p.ItemsPerPage
}
// GetCurrentPage is a method that returns the current page, ensuring that the value is at least 1.
func (p *PaginationMetadata) GetCurrentPage() int {
if p.CurrentPage < 1 {
p.CurrentPage = 1
}
return p.CurrentPage
}
// CalItemPerPage is a method that calculates the number of items per page based on the total number of items and the
// desired number of items per page.
func (p *PaginationMetadata) CalItemPerPage() {
if p.ItemCount < p.ItemsPerPage {
p.ItemsPerPage = p.ItemCount
}
}