Only option

This commit is contained in:
Eden Kirin
2024-06-24 01:08:39 +02:00
parent 295e915f89
commit bd510d958b
5 changed files with 147 additions and 82 deletions

View File

@ -72,7 +72,7 @@ func doList(db *gorm.DB) {
Alive: &TRUE, Alive: &TRUE,
} }
certs, err := repo.List(filter, nil, nil) certs, err := repo.List(filter, nil)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -92,7 +92,7 @@ func doGet(db *gorm.DB) {
Id: &id, Id: &id,
} }
cert, err := repo.Get(filter) cert, err := repo.Get(filter, nil)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -7,6 +7,10 @@ import (
"gorm.io/gorm/schema" "gorm.io/gorm/schema"
) )
type GetOptions struct {
Only *[]string
}
type GetMethod[T schema.Tabler] struct { type GetMethod[T schema.Tabler] struct {
DbConn *gorm.DB DbConn *gorm.DB
} }
@ -15,7 +19,7 @@ func (m *GetMethod[T]) Init(dbConn *gorm.DB) {
m.DbConn = dbConn m.DbConn = dbConn
} }
func (m GetMethod[T]) Get(filter interface{}) (*T, error) { func (m GetMethod[T]) Get(filter interface{}, options *GetOptions) (*T, error) {
var ( var (
model T model T
) )
@ -25,6 +29,10 @@ func (m GetMethod[T]) Get(filter interface{}) (*T, error) {
return nil, err return nil, err
} }
if options != nil {
query = applyOptionOnly(query, options.Only)
}
result := query.First(&model) result := query.First(&model)
if result.Error != nil { if result.Error != nil {
return nil, result.Error return nil, result.Error

View File

@ -1,28 +1,16 @@
package repository package repository
import ( import (
"fmt"
"repo-pattern/app/repository/smartfilter" "repo-pattern/app/repository/smartfilter"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/schema" "gorm.io/gorm/schema"
) )
type Pagination struct { type ListOptions struct {
Offset int Only *[]string
Limit int Ordering *[]Order
} Pagination *Pagination
type OrderDirection string
const (
OrderASC OrderDirection = "ASC"
OrderDESC OrderDirection = "DESC"
)
type Order struct {
Field string
Direction OrderDirection
} }
type ListMethod[T schema.Tabler] struct { type ListMethod[T schema.Tabler] struct {
@ -33,36 +21,7 @@ func (m *ListMethod[T]) Init(dbConn *gorm.DB) {
m.DbConn = dbConn m.DbConn = dbConn
} }
func applyOrdering(query *gorm.DB, ordering *[]Order) *gorm.DB { func (m ListMethod[T]) List(filter interface{}, options *ListOptions) (*[]T, error) {
if ordering == nil || len(*ordering) == 0 {
return query
}
for _, order := range *ordering {
if order.Direction == OrderASC {
query = query.Order(order.Field)
} else {
query = query.Order(fmt.Sprintf("%s %s", order.Field, order.Direction))
}
}
return query
}
func applyPagination(query *gorm.DB, pagination *Pagination) *gorm.DB {
if pagination == nil {
return query
}
if pagination.Limit != 0 {
query = query.Limit(pagination.Limit)
}
if pagination.Offset != 0 {
query = query.Offset(pagination.Offset)
}
return query
}
func (m ListMethod[T]) List(filter interface{}, ordering *[]Order, pagination *Pagination) (*[]T, error) {
var ( var (
model T model T
models []T models []T
@ -73,8 +32,11 @@ func (m ListMethod[T]) List(filter interface{}, ordering *[]Order, pagination *P
return nil, err return nil, err
} }
query = applyOrdering(query, ordering) if options != nil {
query = applyPagination(query, pagination) query = applyOptionOnly(query, options.Only)
query = applyOptionOrdering(query, options.Ordering)
query = applyOptionPagination(query, options.Pagination)
}
query.Find(&models) query.Find(&models)
return &models, nil return &models, nil

61
app/repository/options.go Normal file
View File

@ -0,0 +1,61 @@
package repository
import (
"fmt"
"gorm.io/gorm"
)
type Pagination struct {
Offset int
Limit int
}
type OrderDirection string
const (
OrderASC OrderDirection = "ASC"
OrderDESC OrderDirection = "DESC"
)
type Order struct {
Field string
Direction OrderDirection
}
func applyOptionOnly(query *gorm.DB, only *[]string) *gorm.DB {
if only == nil || len(*only) == 0 {
return query
}
query = query.Select(*only)
return query
}
func applyOptionOrdering(query *gorm.DB, ordering *[]Order) *gorm.DB {
if ordering == nil || len(*ordering) == 0 {
return query
}
for _, order := range *ordering {
if len(order.Direction) == 0 || order.Direction == OrderASC {
query = query.Order(order.Field)
} else {
query = query.Order(fmt.Sprintf("%s %s", order.Field, order.Direction))
}
}
return query
}
func applyOptionPagination(query *gorm.DB, pagination *Pagination) *gorm.DB {
if pagination == nil {
return query
}
if pagination.Limit != 0 {
query = query.Limit(pagination.Limit)
}
if pagination.Offset != 0 {
query = query.Offset(pagination.Offset)
}
return query
}

View File

@ -56,21 +56,23 @@ func TestListMethod(t *testing.T) {
repo.Init(db) repo.Init(db)
filter := MyModelFilter{} filter := MyModelFilter{}
ordering := []Order{ options := ListOptions{
{ Ordering: &[]Order{
Field: "id", {
Direction: OrderASC, Field: "id",
}, Direction: OrderASC,
{ },
Field: "count", {
Direction: OrderDESC, Field: "count",
Direction: OrderDESC,
},
}, },
} }
sql := "SELECT * FROM my_models ORDER BY id,count DESC" sql := "SELECT * FROM my_models ORDER BY id,count DESC"
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))) mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql)))
_, err := repo.List(filter, &ordering, nil) _, err := repo.List(filter, &options)
assert.Nil(t, err) assert.Nil(t, err)
if err := mock.ExpectationsWereMet(); err != nil { if err := mock.ExpectationsWereMet(); err != nil {
@ -86,16 +88,17 @@ func TestListMethod(t *testing.T) {
repo.Init(db) repo.Init(db)
filter := MyModelFilter{} filter := MyModelFilter{}
pagination := Pagination{ options := ListOptions{
Limit: 111, Pagination: &Pagination{
Offset: 0, Limit: 111,
},
} }
sql := "SELECT * FROM my_models LIMIT $1" sql := "SELECT * FROM my_models LIMIT $1"
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))). mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
WithArgs(pagination.Limit) WithArgs(options.Pagination.Limit)
_, err := repo.List(filter, nil, &pagination) _, err := repo.List(filter, &options)
assert.Nil(t, err) assert.Nil(t, err)
if err := mock.ExpectationsWereMet(); err != nil { if err := mock.ExpectationsWereMet(); err != nil {
@ -111,16 +114,17 @@ func TestListMethod(t *testing.T) {
repo.Init(db) repo.Init(db)
filter := MyModelFilter{} filter := MyModelFilter{}
pagination := Pagination{ options := ListOptions{
Limit: 0, Pagination: &Pagination{
Offset: 222, Offset: 222,
},
} }
sql := "SELECT * FROM my_models OFFSET $1" sql := "SELECT * FROM my_models OFFSET $1"
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))). mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
WithArgs(pagination.Offset) WithArgs(options.Pagination.Offset)
_, err := repo.List(filter, nil, &pagination) _, err := repo.List(filter, &options)
assert.Nil(t, err) assert.Nil(t, err)
if err := mock.ExpectationsWereMet(); err != nil { if err := mock.ExpectationsWereMet(); err != nil {
@ -136,16 +140,18 @@ func TestListMethod(t *testing.T) {
repo.Init(db) repo.Init(db)
filter := MyModelFilter{} filter := MyModelFilter{}
pagination := Pagination{ options := ListOptions{
Limit: 111, Pagination: &Pagination{
Offset: 222, Limit: 111,
Offset: 222,
},
} }
sql := "SELECT * FROM my_models LIMIT $1 OFFSET $2" sql := "SELECT * FROM my_models LIMIT $1 OFFSET $2"
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))). mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
WithArgs(pagination.Limit, pagination.Offset) WithArgs(options.Pagination.Limit, options.Pagination.Offset)
_, err := repo.List(filter, nil, &pagination) _, err := repo.List(filter, &options)
assert.Nil(t, err) assert.Nil(t, err)
if err := mock.ExpectationsWereMet(); err != nil { if err := mock.ExpectationsWereMet(); err != nil {
@ -169,7 +175,7 @@ func TestListMethod(t *testing.T) {
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))). mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
WithArgs(id) WithArgs(id)
_, err := repo.List(filter, nil, nil) _, err := repo.List(filter, nil)
assert.Nil(t, err) assert.Nil(t, err)
if err := mock.ExpectationsWereMet(); err != nil { if err := mock.ExpectationsWereMet(); err != nil {
@ -197,7 +203,7 @@ func TestListMethod(t *testing.T) {
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))). mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
WithArgs(id, value, count) WithArgs(id, value, count)
_, err := repo.List(filter, nil, nil) _, err := repo.List(filter, nil)
assert.Nil(t, err) assert.Nil(t, err)
if err := mock.ExpectationsWereMet(); err != nil { if err := mock.ExpectationsWereMet(); err != nil {
@ -220,16 +226,44 @@ func TestListMethod(t *testing.T) {
Value: &value, Value: &value,
Count: &count, Count: &count,
} }
pagination := Pagination{ options := ListOptions{
Offset: 111, Pagination: &Pagination{
Limit: 222, Offset: 111,
Limit: 222,
},
} }
sql := "SELECT * FROM my_models WHERE my_models.id = $1 AND my_models.value = $2 AND my_models.count > $3 LIMIT $4 OFFSET $5" sql := "SELECT * FROM my_models WHERE my_models.id = $1 AND my_models.value = $2 AND my_models.count > $3 LIMIT $4 OFFSET $5"
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))). mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
WithArgs(id, value, count, pagination.Limit, pagination.Offset) WithArgs(id, value, count, options.Pagination.Limit, options.Pagination.Offset)
_, err := repo.List(filter, nil, &pagination) _, err := repo.List(filter, &options)
assert.Nil(t, err)
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
})
t.Run("Only id and count", func(t *testing.T) {
sqldb, db, mock := NewMockDB()
defer sqldb.Close()
repo := RepoBase[MyModel]{}
repo.Init(db)
filter := MyModelFilter{}
options := ListOptions{
Only: &[]string{
"id",
"count",
},
}
sql := "SELECT id,count FROM my_models"
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql)))
_, err := repo.List(filter, &options)
assert.Nil(t, err) assert.Nil(t, err)
if err := mock.ExpectationsWereMet(); err != nil { if err := mock.ExpectationsWereMet(); err != nil {