From 7cd197ee3a8c3c41e47fa86e6519b0471572ccbf Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Wed, 1 Feb 2023 17:56:56 -0300 Subject: [PATCH] add filters for each table --- backend/controllers/frame.go | 16 +++++++++++++++- backend/controllers/game.go | 24 +++++++++++++++++++++++- backend/controllers/godot_version.go | 20 +++++++++++++++++++- backend/controllers/level.go | 12 +++++++++++- backend/controllers/object.go | 18 +++++++++++++++++- backend/controllers/object_name.go | 12 +++++++++++- backend/controllers/object_state.go | 12 +++++++++++- backend/controllers/os.go | 12 +++++++++++- backend/controllers/player.go | 14 +++++++++++++- backend/controllers/user.go | 15 ++++++++++++++- backend/utils/database_filters.go | 21 +++++++++++++++++++++ 11 files changed, 166 insertions(+), 10 deletions(-) create mode 100644 backend/utils/database_filters.go diff --git a/backend/controllers/frame.go b/backend/controllers/frame.go index aef2730..d12a349 100644 --- a/backend/controllers/frame.go +++ b/backend/controllers/frame.go @@ -45,7 +45,21 @@ func ListFrame(writer http.ResponseWriter, request *http.Request, params httprou offset = int(math.Max(float64(0), float64(offset))) } - result := gdb.Model(&models.Frame{}).Order("ID asc").Limit(limit).Offset(offset).Find(&frame) + filters := []string{ + "game_id", + "coins", + "points", + "fps", + "elapsed_time", + } + + whereClause, err := utils.GenerateWhereFilter(filters, queryParams) + if err != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) + return + } + + result := gdb.Model(&models.Frame{}).Where(whereClause).Order("ID asc").Limit(limit).Offset(offset).Find(&frame) if result.Error != nil { utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error()) return diff --git a/backend/controllers/game.go b/backend/controllers/game.go index c41cfb3..b9509d8 100644 --- a/backend/controllers/game.go +++ b/backend/controllers/game.go @@ -47,7 +47,29 @@ func ListGame(writer http.ResponseWriter, request *http.Request, params httprout offset = int(math.Max(float64(0), float64(offset))) } - result := gdb.Model(&models.Game{}).Order("ID asc").Limit(limit).Offset(offset).Find(&games) + filters := []string{ + "player_id", + "level_id", + "os_id", + "godot_version_id", + "processor_count", + "screen_count", + "screen_dpi", + "screen_size", + "machine_id", + "locale", + "game_version", + "won", + "timestamp", + } + + whereClause, err := utils.GenerateWhereFilter(filters, queryParams) + if err != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) + return + } + + result := gdb.Model(&models.Game{}).Where(whereClause).Order("ID asc").Limit(limit).Offset(offset).Find(&games) if result.Error != nil { utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error()) return diff --git a/backend/controllers/godot_version.go b/backend/controllers/godot_version.go index 9c5e9c7..e7a721f 100644 --- a/backend/controllers/godot_version.go +++ b/backend/controllers/godot_version.go @@ -45,7 +45,25 @@ func ListGodotVersion(writer http.ResponseWriter, request *http.Request, params offset = int(math.Max(float64(0), float64(offset))) } - result := gdb.Model(&models.GodotVersion{}).Order("ID asc").Limit(limit).Offset(offset).Find(&godotVersion) + filters := []string{ + "major", + "minor", + "patch", + "hex", + "status", + "build", + "year", + "hash", + "string", + } + + whereClause, err := utils.GenerateWhereFilter(filters, queryParams) + if err != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) + return + } + + result := gdb.Model(&models.GodotVersion{}).Where(whereClause).Order("ID asc").Limit(limit).Offset(offset).Find(&godotVersion) if result.Error != nil { utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error()) return diff --git a/backend/controllers/level.go b/backend/controllers/level.go index 52bdf2c..3c552e2 100644 --- a/backend/controllers/level.go +++ b/backend/controllers/level.go @@ -45,7 +45,17 @@ func ListLevel(writer http.ResponseWriter, request *http.Request, params httprou offset = int(math.Max(float64(0), float64(offset))) } - result := gdb.Model(&models.Level{}).Order("ID asc").Limit(limit).Offset(offset).Find(&level) + filters := []string{ + "name", + } + + whereClause, err := utils.GenerateWhereFilter(filters, queryParams) + if err != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) + return + } + + result := gdb.Model(&models.Level{}).Where(whereClause).Order("ID asc").Limit(limit).Offset(offset).Find(&level) if result.Error != nil { utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error()) return diff --git a/backend/controllers/object.go b/backend/controllers/object.go index 69f746b..52fce3c 100644 --- a/backend/controllers/object.go +++ b/backend/controllers/object.go @@ -45,7 +45,23 @@ func ListObject(writer http.ResponseWriter, request *http.Request, params httpro offset = int(math.Max(float64(0), float64(offset))) } - result := gdb.Model(&models.Object{}).Order("ID asc").Limit(limit).Offset(offset).Find(&object) + filters := []string{ + "frame_id", + "object_name_id", + "object_state_id", + "position_x", + "position_y", + "velocity_x", + "velocity_y", + } + + whereClause, err := utils.GenerateWhereFilter(filters, queryParams) + if err != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) + return + } + + result := gdb.Model(&models.Object{}).Where(whereClause).Order("ID asc").Limit(limit).Offset(offset).Find(&object) if result.Error != nil { utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error()) return diff --git a/backend/controllers/object_name.go b/backend/controllers/object_name.go index b767147..c55b2f8 100644 --- a/backend/controllers/object_name.go +++ b/backend/controllers/object_name.go @@ -45,7 +45,17 @@ func ListObjectName(writer http.ResponseWriter, request *http.Request, params ht offset = int(math.Max(float64(0), float64(offset))) } - result := gdb.Model(&models.ObjectName{}).Order("ID asc").Limit(limit).Offset(offset).Find(&objectName) + filters := []string{ + "name", + } + + whereClause, err := utils.GenerateWhereFilter(filters, queryParams) + if err != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) + return + } + + result := gdb.Model(&models.ObjectName{}).Where(whereClause).Order("ID asc").Limit(limit).Offset(offset).Find(&objectName) if result.Error != nil { utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error()) return diff --git a/backend/controllers/object_state.go b/backend/controllers/object_state.go index d42ce65..2e3fe68 100644 --- a/backend/controllers/object_state.go +++ b/backend/controllers/object_state.go @@ -45,7 +45,17 @@ func ListObjectState(writer http.ResponseWriter, request *http.Request, params h offset = int(math.Max(float64(0), float64(offset))) } - result := gdb.Model(&models.ObjectState{}).Order("ID asc").Limit(limit).Offset(offset).Find(&objectState) + filters := []string{ + "name", + } + + whereClause, err := utils.GenerateWhereFilter(filters, queryParams) + if err != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) + return + } + + result := gdb.Model(&models.ObjectState{}).Where(whereClause).Order("ID asc").Limit(limit).Offset(offset).Find(&objectState) if result.Error != nil { utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error()) return diff --git a/backend/controllers/os.go b/backend/controllers/os.go index 64ed4a3..ab7b202 100644 --- a/backend/controllers/os.go +++ b/backend/controllers/os.go @@ -45,7 +45,17 @@ func ListOS(writer http.ResponseWriter, request *http.Request, params httprouter offset = int(math.Max(float64(0), float64(offset))) } - result := gdb.Model(&models.OS{}).Order("ID asc").Limit(limit).Offset(offset).Find(&os) + filters := []string{ + "name", + } + + whereClause, err := utils.GenerateWhereFilter(filters, queryParams) + if err != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) + return + } + + result := gdb.Model(&models.OS{}).Where(whereClause).Order("ID asc").Limit(limit).Offset(offset).Find(&os) if result.Error != nil { utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error()) return diff --git a/backend/controllers/player.go b/backend/controllers/player.go index 6288559..82d74be 100644 --- a/backend/controllers/player.go +++ b/backend/controllers/player.go @@ -45,7 +45,19 @@ func ListPlayer(writer http.ResponseWriter, request *http.Request, params httpro offset = int(math.Max(float64(0), float64(offset))) } - result := gdb.Model(&models.Player{}).Order("ID asc").Limit(limit).Offset(offset).Find(&player) + filters := []string{ + "rut", + "name", + "email", + } + + whereClause, err := utils.GenerateWhereFilter(filters, queryParams) + if err != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) + return + } + + result := gdb.Model(&models.Player{}).Where(whereClause).Order("ID asc").Limit(limit).Offset(offset).Find(&player) if result.Error != nil { utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error()) return diff --git a/backend/controllers/user.go b/backend/controllers/user.go index 5fa0ab1..079dcf0 100644 --- a/backend/controllers/user.go +++ b/backend/controllers/user.go @@ -45,7 +45,20 @@ func ListUser(writer http.ResponseWriter, request *http.Request, params httprout offset = int(math.Max(float64(0), float64(offset))) } - result := gdb.Model(&models.User{}).Order("ID asc").Limit(limit).Offset(offset).Find(&users) + filters := []string{ + "name", + "username", + "email", + "password", + } + + whereClause, err := utils.GenerateWhereFilter(filters, queryParams) + if err != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) + return + } + + result := gdb.Model(&models.User{}).Where(whereClause).Order("ID asc").Limit(limit).Offset(offset).Find(&users) if result.Error != nil { utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error()) return diff --git a/backend/utils/database_filters.go b/backend/utils/database_filters.go new file mode 100644 index 0000000..4426159 --- /dev/null +++ b/backend/utils/database_filters.go @@ -0,0 +1,21 @@ +package utils + +import ( + "net/url" +) + +func GenerateWhereFilter(filters []string, queryParams url.Values) (map[string]interface{}, error) { + whereClause := make(map[string]interface{}) + + for _, filter := range filters { + if queryParams.Get(filter) != "" { + var err error + whereClause[filter] = queryParams.Get(filter) + if err != nil { + return nil, err + } + } + } + + return whereClause, nil +}