add filters for each table

This commit is contained in:
Chris Cromer 2023-02-01 17:56:56 -03:00 committed by Martin Araneda
parent dbad5ed64b
commit bb735afe33
11 changed files with 166 additions and 10 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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
}