Skip to content

Commit

Permalink
Merge pull request #3 from juaismar/juaismar/refactor/tableList
Browse files Browse the repository at this point in the history
Params refactor
  • Loading branch information
juaismar authored Mar 17, 2021
2 parents 89aaecc + 3fb3e5a commit 69da795
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 150 deletions.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ func (c *User) Pagination() {
// parameter represents the DataTables column identifier. In this case simple
// indexes but can be a string
// Formatter is a function to customize the value of field , can be nil.
columns := make(map[int]SSP.Data)
columns[0] = SSP.Data{Db: "name", Dt: 0, Formatter: nil}
columns[1] = SSP.Data{Db: "role", Dt: 1, Formatter: nil}
columns[2] = SSP.Data{Db: "email", Dt: 2, Formatter: nil}
columns := []SSP.Data{
SSP.Data{Db: "name", Dt: 0, Formatter: nil},
SSP.Data{Db: "role", Dt: 1, Formatter: nil},
SSP.Data{Db: "email", Dt: 2, Formatter: nil},
}
// Send the data to the client
// "users" is the name of the table
Expand All @@ -60,7 +61,7 @@ func (c *User) Pagination() {

-This is an example of data formatting.
```
columns[3] = SSP.Data{Db: "registered", Dt: 3, Formatter: func(
SSP.Data{Db: "registered", Dt: 3, Formatter: func(
data interface{}, row map[string]interface{}) (interface{}, error) {
//data is the value id column, row is a map whit the values of all columns
if data != nil {
Expand All @@ -72,11 +73,12 @@ columns[3] = SSP.Data{Db: "registered", Dt: 3, Formatter: func(

-This is a complex example.
```
import ("github.com/juaismar/go-gormss")
import ("github.com/juaismar/go-gormssp")
func (c *User) Pagination() {
columns := make(map[int]SSP.Data)
columns[0] = SSP.Data{Db: "id", Dt: "id", Formatter: nil}
columns := []SSP.Data{
SSP.Data{Db: "id", Dt: "id", Formatter: nil},
}
//whereResult is a WHERE condition to apply to the result set
//whereAll is a WHERE condition to apply to all queries
Expand Down
16 changes: 8 additions & 8 deletions SSP.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type Controller interface {
// Simple is a main method, externally called
func Simple(c Controller, conn *gorm.DB,
table string,
columns map[int]Data) (responseJSON MessageDataTable, err error) {
columns []Data) (responseJSON MessageDataTable, err error) {

dialect = conn.Dialector.Name()

Expand Down Expand Up @@ -82,7 +82,7 @@ func Simple(c Controller, conn *gorm.DB,
}

// Complex is a main method, externally called
func Complex(c Controller, conn *gorm.DB, table string, columns map[int]Data,
func Complex(c Controller, conn *gorm.DB, table string, columns []Data,
whereResult []string,
whereAll []string,
whereJoin map[string]string) (responseJSON MessageDataTable, err error) {
Expand Down Expand Up @@ -153,7 +153,7 @@ func Complex(c Controller, conn *gorm.DB, table string, columns map[int]Data,
return
}

func dataOutput(columns map[int]Data, rows *sql.Rows) ([]interface{}, error) {
func dataOutput(columns []Data, rows *sql.Rows) ([]interface{}, error) {
out := make([]interface{}, 0)

for rows.Next() {
Expand All @@ -168,7 +168,7 @@ func dataOutput(columns map[int]Data, rows *sql.Rows) ([]interface{}, error) {
column := columns[j]
var dt string
if column.Dt == nil {
return nil, fmt.Errorf("Bad map id, column[%v] dont exist", j)
return nil, fmt.Errorf("Dt cannot be nil in column[%v]", j)
}

vType := reflect.TypeOf(column.Dt)
Expand Down Expand Up @@ -249,7 +249,7 @@ func setJoins(joins map[string]string) func(db *gorm.DB) *gorm.DB {
}

//database func
func filterGlobal(c Controller, columns map[int]Data, columnsType []*sql.ColumnType) func(db *gorm.DB) *gorm.DB {
func filterGlobal(c Controller, columns []Data, columnsType []*sql.ColumnType) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {

globalSearch := ""
Expand Down Expand Up @@ -293,7 +293,7 @@ func filterGlobal(c Controller, columns map[int]Data, columnsType []*sql.ColumnT
}
}

func filterIndividual(c Controller, columns map[int]Data, columnsType []*sql.ColumnType) func(db *gorm.DB) *gorm.DB {
func filterIndividual(c Controller, columns []Data, columnsType []*sql.ColumnType) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
// Individual column filtering
columnSearch := ""
Expand Down Expand Up @@ -338,7 +338,7 @@ func filterIndividual(c Controller, columns map[int]Data, columnsType []*sql.Col
}

//Refactor this
func order(c Controller, columns map[int]Data) func(db *gorm.DB) *gorm.DB {
func order(c Controller, columns []Data) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {

if c.GetString("order[0][column]") != "" {
Expand Down Expand Up @@ -410,7 +410,7 @@ func limit(c Controller) func(db *gorm.DB) *gorm.DB {
}
}

func search(column map[int]Data, keyColumnsI string) int {
func search(column []Data, keyColumnsI string) int {
var i int
for i = 0; i < len(column); i++ {
data := column[i]
Expand Down
Loading

0 comments on commit 69da795

Please sign in to comment.