Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamess-Lucass committed Apr 16, 2024
1 parent d06930b commit a7ec27d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 34 deletions.
34 changes: 27 additions & 7 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@ type Base struct {
Id uuid.UUID `json:"id"`
}

type Address struct {
Postcode string `json:"postcode"`
}

type User struct {
Base

Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
Email string `json:"email"`
UserName string `gorm:"column:display_name" json:"userName"`
PersonSex string `json:"gender"`
Age uint `json:"age"`
Contributor bool `json:"contributor"`
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
Email string `json:"email"`
UserName string `gorm:"column:display_name" json:"userName"`
PersonSex string `json:"gender"`
Age uint `json:"age"`
Contributor bool `json:"contributor"`
Addresses []Address `json:"addresses"`
}

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -474,3 +479,18 @@ func Test_QueryWithFilterAndOrInColumnName(t *testing.T) {

assert.Equal(t, expectedSql, sql)
}

func Test_QueryWithFilterOnPropertyInCollection(t *testing.T) {
query := Query{Filter: "addresses/postcode eq 'asd'"}

sql := DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
res, _, _ := Apply(tx.Model(&User{}), query, nil, nil, &[]User{})
return res.Find(&[]User{})
})

expectedSql := DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
return tx.Model(&User{}).Where("LOWER(contributor) = LOWER('true')").Find(&[]User{})
})

assert.Equal(t, expectedSql, sql)
}
6 changes: 3 additions & 3 deletions examples/fiber/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ go 1.21
require (
github.com/brianvoe/gofakeit/v6 v6.23.2
github.com/gofiber/fiber/v2 v2.49.1
github.com/google/uuid v1.5.0
gorm.io/driver/sqlite v1.5.4
gorm.io/gorm v1.25.5
github.com/google/uuid v1.6.0
gorm.io/driver/sqlite v1.5.5
gorm.io/gorm v1.25.9
)

replace goatquery => ../..
Expand Down
16 changes: 8 additions & 8 deletions examples/fiber/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gofiber/fiber/v2 v2.49.1 h1:0W2DRWevSirc8pJl4o8r8QejDR8TV6ZUCawHxwbIdOk=
github.com/gofiber/fiber/v2 v2.49.1/go.mod h1:nPUeEBUeeYGgwbDm59Gp7vS8MDyScL6ezr/Np9A13WU=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
Expand All @@ -28,8 +28,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.49.0 h1:9FdvCpmxB74LH4dPb7IJ1cOSsluR07XG3I1txXWwJpE=
Expand All @@ -42,7 +42,7 @@ golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/sqlite v1.5.4 h1:IqXwXi8M/ZlPzH/947tn5uik3aYQslP9BVveoax0nV0=
gorm.io/driver/sqlite v1.5.4/go.mod h1:qxAuCol+2r6PannQDpOP1FP6ag3mKi4esLnB/jHed+4=
gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls=
gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
gorm.io/driver/sqlite v1.5.5 h1:7MDMtUZhV065SilG62E0MquljeArQZNfJnjd9i9gx3E=
gorm.io/driver/sqlite v1.5.5/go.mod h1:6NgQ7sQWAIFsPrJJl1lSNSu2TABh0ZZ/zm5fosATavE=
gorm.io/gorm v1.25.9 h1:wct0gxZIELDk8+ZqF/MVnHLkA1rvYlBWUMv2EdsK1g8=
gorm.io/gorm v1.25.9/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
53 changes: 37 additions & 16 deletions examples/fiber/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"errors"
"fmt"
"log"

"github.com/brianvoe/gofakeit/v6"
"github.com/gofiber/fiber/v2"
Expand All @@ -15,7 +16,7 @@ import (
)

type Base struct {
Id uuid.UUID `json:"id"`
Id uuid.UUID `json:"id" gorm:"type:uuid;primary_key"`
}

type User struct {
Expand All @@ -37,8 +38,9 @@ type User struct {
type UserPermission struct {
Base

Name string `json:"name"`
UserId uuid.UUID `json:"-"`
Name string `json:"name"`

UserID uuid.UUID `json:"-"`
}

type Address struct {
Expand All @@ -59,12 +61,14 @@ type UserDto struct {
Age uint `json:"age"`
AddressId uuid.UUID `json:"-"`
Address Address `json:"address"`
Permissions []UserPermission `json:"permissions" gorm:"foreignKey:UserId"`
Permissions []UserPermission `json:"permissions"`
}

var DB *gorm.DB

func main() {
log.Println("starting")

db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{
Logger: logger.Default.LogMode(logger.Info),
})
Expand All @@ -81,7 +85,7 @@ func main() {
if err := db.First(&User{}).Error; errors.Is(err, gorm.ErrRecordNotFound) {
items := []User{}

for i := 1; i < 1_000; i++ {
for i := 1; i < 100; i++ {
person := gofakeit.Person()
userId := uuid.New()

Expand All @@ -96,7 +100,7 @@ func main() {
Age: gofakeit.UintRange(0, 100),
IsDeleted: gofakeit.Bool(),
Address: Address{Base: Base{Id: uuid.New()}, Postcode: person.Address.Zip},
Permissions: []UserPermission{{Base: Base{Id: uuid.New()}, UserId: userId, Name: gofakeit.LoremIpsumWord()}},
Permissions: []UserPermission{{Base: Base{Id: uuid.New()}, UserID: userId, Name: gofakeit.LoremIpsumWord()}},
}

items = append(items, item)
Expand Down Expand Up @@ -128,19 +132,36 @@ func getUsers(c *fiber.Ctx) error {
Filter: c.Query("filter"),
}

var users []UserDto
res, count, err := goatquery.Apply(GetAllUsers(DB), query, nil, nil, &users)
if err != nil {
return c.Status(400).JSON(goatquery.QueryErrorResponse{Status: 400, Message: err.Error()})
}
// var users []UserDto
// res, count, err := goatquery.Apply(GetAllUsers(DB), query, nil, nil, &users)
// if err != nil {
// return c.Status(400).JSON(goatquery.QueryErrorResponse{Status: 400, Message: err.Error()})
// }

if err := res.Find(&users).Error; err != nil {
return c.Status(400).JSON(goatquery.QueryErrorResponse{Status: 400, Message: err.Error()})
}
// if err := res.Find(&users).Error; err != nil {
// return c.Status(400).JSON(goatquery.QueryErrorResponse{Status: 400, Message: err.Error()})
// }

response := goatquery.BuildPagedResponse(users, query, count)
// response := goatquery.BuildPagedResponse(users, query, count)

// log.Println(response)

var user []User
DB.Model(&User{}).Preload("Address").Preload("Permissions").Find(&user)

if query.Filter != "" {
var user []User
DB.Model(&User{}).
Preload("Address").
Preload("Permissions").
Joins("INNER JOIN user_permissions up ON up.user_id = users.id").
Where("up.name = ?", query.Filter).
Find(&user)

return c.JSON(user)
}

return c.JSON(response)
return c.JSON(user)
}

func GetAllUsers(db *gorm.DB) *gorm.DB {
Expand Down

0 comments on commit a7ec27d

Please sign in to comment.