Skip to content

Commit

Permalink
Implement ordering and detailed filtering for products and sports events
Browse files Browse the repository at this point in the history
The update delivers enhanced functionality for ecommerce filters where users can now filter by attributes such as sport, year, package type, and order. Further improvements include a specialized SportsProducts function that retrieves a list of sports events linked with products and enhancements to the app, ecommerce, and event models, providing unique record identification. Various errors checks have been included, and logging errors improved.
  • Loading branch information
iesreza committed Feb 27, 2024
1 parent de04d60 commit 6a48405
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions lib/db/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package schema

import (
"fmt"
"github.com/getevo/evo/v2/lib/db/schema/table"
"gorm.io/gorm"
"gorm.io/gorm/schema"
"path/filepath"
Expand All @@ -11,24 +12,29 @@ import (
var Models []Model

type Model struct {
Sample any `json:"sample"`
Value reflect.Value `json:"-"`
Type reflect.Type `json:"-"`
Kind reflect.Kind `json:"-"`
Table string `json:"table"`
Name string `json:"name"`
Package string `json:"package"`
PackagePath string `json:"package_path"`
PrimaryKey []string `json:"primary_key"`
Schema *schema.Schema `json:"-"`
Statement *gorm.Statement `json:"-"`
Sample any `json:"sample"`
Value reflect.Value `json:"-"`
Type reflect.Type `json:"-"`
Kind reflect.Kind `json:"-"`
Table string `json:"table"`
Name string `json:"name"`
Package string `json:"package"`
PackagePath string `json:"package_path"`
PrimaryKey []string `json:"primary_key"`
Joins map[string][]string `json:"joins"`
Schema *schema.Schema `json:"-"`
Statement *gorm.Statement `json:"-"`
}

func (m Model) Join(joins ...*Model) ([]string, []string, error) {
var where []string
var tables = []string{m.Table}
for _, join := range joins {
tables = append(tables, join.Table)
if v, ok := m.Joins[join.Table]; ok {
where = append(where, quote(m.Table)+"."+quote(v[0])+" = "+quote(join.Table)+"."+quote(v[1]))
continue
}
if _, ok := join.Schema.FieldsByDBName[m.PrimaryKey[0]]; ok {
where = append(where, quote(m.Table)+"."+quote(m.PrimaryKey[0])+" = "+quote(join.Table)+"."+quote(m.PrimaryKey[0]))
continue
Expand All @@ -47,8 +53,13 @@ func quote(s string) string {
return "`" + s + "`"
}

var database = ""

func UseModel(db *gorm.DB, values ...any) {
migrations = append(migrations, values...)
if database == "" {
db.Raw("SELECT DATABASE();").Scan(&database)
}
for index, _ := range values {
ref := reflect.ValueOf(values[index])
if ref.Kind() != reflect.Struct {
Expand All @@ -69,6 +80,13 @@ func UseModel(db *gorm.DB, values ...any) {
model.PrimaryKey = stmt.Schema.PrimaryFieldDBNames
model.Statement = stmt
model.Table = stmt.Table
model.Joins = make(map[string][]string)

var constraints []table.Constraint
db.Where(table.Constraint{Database: database}).Find(&constraints)
for _, constraint := range constraints {
model.Joins[constraint.ReferencedTable] = []string{constraint.Column, constraint.ReferencedColumn}
}

Models = append(Models, model)
}
Expand Down

0 comments on commit 6a48405

Please sign in to comment.