Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
pieceowater committed Sep 29, 2024
1 parent bd4de0c commit 0d82ea2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 55 deletions.
8 changes: 2 additions & 6 deletions gossiper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
network "github.com/pieceowater-dev/lotof.lib.gossiper/internal/consume/mqp"
environment "github.com/pieceowater-dev/lotof.lib.gossiper/internal/environment"
tools "github.com/pieceowater-dev/lotof.lib.gossiper/internal/tools"
t "github.com/pieceowater-dev/lotof.lib.gossiper/types"
)

// ENVIRONMENT
Expand Down Expand Up @@ -86,18 +87,13 @@ const (
ONE_HUNDRED = tools.ONE_HUNDRED
)

// DefaultFilter is an alias for the Tools.DefaultFilter method.
type DefaultFilter[T any] struct {
tools.DefaultFilter[T]
}

// PaginatedEntity is a wrapper for tools.PaginatedEntity
type PaginatedEntity[T any] struct {
tools.PaginatedEntity[T]
}

// NewFilter creates a new DefaultFilter instance.
func NewFilter[T any]() tools.DefaultFilter[T] {
func NewFilter[T any]() t.DefaultFilter[T] {
return tools.NewDefaultFilter[T]()
}

Expand Down
75 changes: 26 additions & 49 deletions internal/tools/filter.go
Original file line number Diff line number Diff line change
@@ -1,67 +1,44 @@
package gossiper

// FilterSortByEnum defines sorting options.
type FilterSortByEnum string
import t "github.com/pieceowater-dev/lotof.lib.gossiper/types"

const (
// ASC represents ascending sort order.
ASC FilterSortByEnum = "ASC"
ASC t.FilterSortByEnum = "ASC"
// DESC represents descending sort order.
DESC FilterSortByEnum = "DESC"
DESC t.FilterSortByEnum = "DESC"
)

// FilterPaginationLengthEnum defines pagination length options.
type FilterPaginationLengthEnum int

const (
TEN FilterPaginationLengthEnum = 10
FIFTEEN FilterPaginationLengthEnum = 15
TWENTY FilterPaginationLengthEnum = 20
TWENTY_FIVE FilterPaginationLengthEnum = 25
THIRTY FilterPaginationLengthEnum = 30
THIRTY_FIVE FilterPaginationLengthEnum = 35
FORTY FilterPaginationLengthEnum = 40
FORTY_FIVE FilterPaginationLengthEnum = 45
FIFTY FilterPaginationLengthEnum = 50
FIFTY_FIVE FilterPaginationLengthEnum = 55
SIXTY FilterPaginationLengthEnum = 60
SIXTY_FIVE FilterPaginationLengthEnum = 65
SEVENTY FilterPaginationLengthEnum = 70
SEVENTY_FIVE FilterPaginationLengthEnum = 75
EIGHTY FilterPaginationLengthEnum = 80
EIGHTY_FIVE FilterPaginationLengthEnum = 85
NINETY FilterPaginationLengthEnum = 90
NINETY_FIVE FilterPaginationLengthEnum = 95
ONE_HUNDRED FilterPaginationLengthEnum = 100
TEN t.FilterPaginationLengthEnum = 10
FIFTEEN t.FilterPaginationLengthEnum = 15
TWENTY t.FilterPaginationLengthEnum = 20
TWENTY_FIVE t.FilterPaginationLengthEnum = 25
THIRTY t.FilterPaginationLengthEnum = 30
THIRTY_FIVE t.FilterPaginationLengthEnum = 35
FORTY t.FilterPaginationLengthEnum = 40
FORTY_FIVE t.FilterPaginationLengthEnum = 45
FIFTY t.FilterPaginationLengthEnum = 50
FIFTY_FIVE t.FilterPaginationLengthEnum = 55
SIXTY t.FilterPaginationLengthEnum = 60
SIXTY_FIVE t.FilterPaginationLengthEnum = 65
SEVENTY t.FilterPaginationLengthEnum = 70
SEVENTY_FIVE t.FilterPaginationLengthEnum = 75
EIGHTY t.FilterPaginationLengthEnum = 80
EIGHTY_FIVE t.FilterPaginationLengthEnum = 85
NINETY t.FilterPaginationLengthEnum = 90
NINETY_FIVE t.FilterPaginationLengthEnum = 95
ONE_HUNDRED t.FilterPaginationLengthEnum = 100
)

// Sort defines the structure for sorting data based on a field and order.
type Sort[T any] struct {
By FilterSortByEnum `json:"by,omitempty"` // Sort order: ASC or DESC
Field string `json:"field,omitempty"` // The field to sort by
}

// Paginated defines pagination properties for data requests.
type Paginated struct {
Length FilterPaginationLengthEnum `json:"length,omitempty"` // Number of items per page
Page int `json:"page,omitempty"` // Current page number
}

// DefaultFilter defines a filter structure with search, sort, and pagination options.
type DefaultFilter[T any] struct {
Search string `json:"search,omitempty"` // Search query string
Sort Sort[T] `json:"sort,omitempty"` // Sort parameters
Pagination Paginated `json:"pagination,omitempty"` // Pagination parameters
}

// NewDefaultFilter - Constructor for DefaultFilter with default pagination values.
func NewDefaultFilter[T any]() DefaultFilter[T] {
return DefaultFilter[T]{
Sort: Sort[T]{
func NewDefaultFilter[T any]() t.DefaultFilter[T] {
return t.DefaultFilter[T]{
Sort: t.Sort[T]{
Field: "id",
By: DESC,
},
Pagination: Paginated{
Pagination: t.Paginated{
Page: 1,
Length: TEN,
},
Expand Down
26 changes: 26 additions & 0 deletions types/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package gossiper

// FilterPaginationLengthEnum defines pagination length options.
type FilterPaginationLengthEnum int

// FilterSortByEnum defines sorting options.
type FilterSortByEnum string

// Sort defines the structure for sorting data based on a field and order.
type Sort[T any] struct {
By FilterSortByEnum `json:"by,omitempty"` // Sort order: ASC or DESC
Field string `json:"field,omitempty"` // The field to sort by
}

// Paginated defines pagination properties for data requests.
type Paginated struct {
Length FilterPaginationLengthEnum `json:"length,omitempty"` // Number of items per page
Page int `json:"page,omitempty"` // Current page number
}

// DefaultFilter defines a filter structure with search, sort, and pagination options.
type DefaultFilter[T any] struct {
Search string `json:"search,omitempty"` // Search query string
Sort Sort[T] `json:"sort,omitempty"` // Sort parameters
Pagination Paginated `json:"pagination,omitempty"` // Pagination parameters
}

0 comments on commit 0d82ea2

Please sign in to comment.