Skip to content

Commit e6861bd

Browse files
committed
Move domain package from codnect.io/procyon to codnect.io/procyon-data
1 parent 4bd028e commit e6861bd

7 files changed

Lines changed: 487 additions & 0 deletions

File tree

domain/page/page.go

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package page
2+
3+
import (
4+
"codnect.io/procyon-data/domain/sort"
5+
"math"
6+
)
7+
8+
type Paginated interface {
9+
TotalPages() int
10+
TotalElements() int
11+
HasNext() bool
12+
Number() int
13+
Size() int
14+
NumberOfElements() int
15+
HasPrevious() bool
16+
IsFirst() bool
17+
IsLast() bool
18+
NextPageable() Pageable
19+
PreviousPageable() Pageable
20+
HasContent() bool
21+
Content() any
22+
Pageable() Pageable
23+
Sort() sort.Sort
24+
}
25+
26+
type Page[T any] interface {
27+
Paginated
28+
29+
Items() []T
30+
}
31+
32+
type page[T any] struct {
33+
content []T
34+
pageable Pageable
35+
total int
36+
}
37+
38+
func newPage[T any](content []T, pageable Pageable, total int) *page[T] {
39+
if content == nil {
40+
panic("content must not be nil")
41+
}
42+
43+
if pageable == nil {
44+
panic("pageable must not be nil")
45+
}
46+
47+
if len(content) != 0 && pageable.Offset()+pageable.PageSize() > total {
48+
total = pageable.Offset() + len(content)
49+
}
50+
51+
return &page[T]{
52+
content: content,
53+
pageable: pageable,
54+
total: total,
55+
}
56+
}
57+
58+
func (p *page[T]) TotalPages() int {
59+
if p.Size() == 0 {
60+
return 1
61+
}
62+
63+
return int(math.Ceil(float64(p.total) / float64(p.Size())))
64+
}
65+
66+
func (p *page[T]) TotalElements() int {
67+
return p.total
68+
}
69+
70+
func (p *page[T]) HasNext() bool {
71+
return p.Number()+1 < p.TotalPages()
72+
}
73+
74+
func (p *page[T]) Number() int {
75+
if p.pageable.IsPaged() {
76+
return p.pageable.PageNumber()
77+
}
78+
79+
return 0
80+
}
81+
82+
func (p *page[T]) Size() int {
83+
if p.pageable.IsPaged() {
84+
return p.pageable.PageSize()
85+
}
86+
87+
return len(p.content)
88+
}
89+
90+
func (p *page[T]) NumberOfElements() int {
91+
return len(p.content)
92+
}
93+
94+
func (p *page[T]) HasPrevious() bool {
95+
return p.Number() > 0
96+
}
97+
98+
func (p *page[T]) IsFirst() bool {
99+
return !p.HasPrevious()
100+
}
101+
102+
func (p *page[T]) IsLast() bool {
103+
return !p.HasNext()
104+
}
105+
106+
func (p *page[T]) NextPageable() Pageable {
107+
if p.HasNext() {
108+
return p.pageable.Next()
109+
}
110+
111+
return unpagedRequest
112+
}
113+
114+
func (p *page[T]) PreviousPageable() Pageable {
115+
if p.HasPrevious() {
116+
return p.pageable.PreviousOrFirst()
117+
}
118+
119+
return unpagedRequest
120+
}
121+
122+
func (p *page[T]) HasContent() bool {
123+
return len(p.content) != 0
124+
}
125+
126+
func (p *page[T]) Content() any {
127+
return p.content
128+
}
129+
130+
func (p *page[T]) Items() []T {
131+
copyOfContent := make([]T, len(p.content))
132+
copy(copyOfContent, p.content)
133+
return copyOfContent
134+
}
135+
136+
func (p *page[T]) Pageable() Pageable {
137+
return p.pageable
138+
}
139+
140+
func (p *page[T]) Sort() sort.Sort {
141+
return p.pageable.Sort()
142+
}
143+
144+
func Empty[T any]() Page[T] {
145+
return New(make([]T, 0), unpagedRequest, 0)
146+
}
147+
148+
func New[T any](content []T, pageable Pageable, total int) Page[T] {
149+
return newPage(content, pageable, total)
150+
}
151+
152+
func WithContent[T any](content []T) Page[T] {
153+
return New[T](content, unpagedRequest, len(content))
154+
}

domain/page/pageable.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package page
2+
3+
import (
4+
"codnect.io/procyon-data/domain/sort"
5+
)
6+
7+
type Pageable interface {
8+
PageNumber() int
9+
PageSize() int
10+
Offset() int
11+
Sort() sort.Sort
12+
IsPaged() bool
13+
IsUnpaged() bool
14+
First() Pageable
15+
HasPrevious() bool
16+
Next() Pageable
17+
PreviousOrFirst() Pageable
18+
}
19+
20+
func Unpaged() Pageable {
21+
return unpagedRequest
22+
}
23+
24+
func Of(pageNumber, pageSize int) Pageable {
25+
return newRequest(pageNumber, pageSize, true)
26+
}
27+
28+
func OfSize(pageSize int) Pageable {
29+
return newRequest(0, pageSize, true)
30+
}

domain/page/request.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package page
2+
3+
import (
4+
"codnect.io/procyon-data/domain/sort"
5+
)
6+
7+
var (
8+
unpagedRequest = newRequest(0, 0, false)
9+
)
10+
11+
type request struct {
12+
page int
13+
size int
14+
sort sort.Sort
15+
isPaged bool
16+
}
17+
18+
func newRequest(pageNumber, pageSize int, isPaged bool) *request {
19+
return &request{
20+
page: pageNumber,
21+
size: pageSize,
22+
isPaged: isPaged,
23+
}
24+
}
25+
26+
func (r *request) PageNumber() int {
27+
if !r.isPaged {
28+
panic("unsupported operation")
29+
}
30+
31+
return r.page
32+
}
33+
34+
func (r *request) PageSize() int {
35+
if !r.isPaged {
36+
panic("unsupported operation")
37+
}
38+
39+
return r.size
40+
}
41+
42+
func (r *request) Offset() int {
43+
if !r.isPaged {
44+
panic("unsupported operation")
45+
}
46+
47+
return r.page * r.size
48+
}
49+
50+
func (r *request) Sort() sort.Sort {
51+
/* todo fix sort method */
52+
return r.sort
53+
}
54+
55+
func (r *request) IsPaged() bool {
56+
return r.isPaged
57+
}
58+
59+
func (r *request) IsUnpaged() bool {
60+
return !r.isPaged
61+
}
62+
63+
func (r *request) HasPrevious() bool {
64+
if !r.isPaged {
65+
return false
66+
}
67+
68+
return r.page > 0
69+
}
70+
71+
func (r *request) PreviousOrFirst() Pageable {
72+
if !r.isPaged {
73+
return r
74+
}
75+
76+
if r.HasPrevious() {
77+
return r.Previous()
78+
}
79+
80+
return r.First()
81+
}
82+
83+
func (r *request) Previous() Pageable {
84+
if r.PageNumber() == 0 {
85+
return r
86+
}
87+
88+
return newRequest(r.PageNumber()-1, r.PageSize(), r.IsPaged())
89+
}
90+
91+
func (r *request) First() Pageable {
92+
if !r.isPaged {
93+
return r
94+
}
95+
96+
return newRequest(0, r.PageSize(), r.IsPaged())
97+
}
98+
99+
func (r *request) Next() Pageable {
100+
if !r.isPaged {
101+
return r
102+
}
103+
104+
return newRequest(r.PageNumber()+1, r.PageSize(), r.IsPaged())
105+
}

domain/sort/order/direction.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package order
2+
3+
type Direction int
4+
5+
const (
6+
Ascending Direction = iota + 1
7+
Descending
8+
Default = Ascending
9+
)

0 commit comments

Comments
 (0)