mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-21 17:21:09 +01:00
refactor: enhance controller parameter handling and error responses
- Refactored export and filter controller methods to utilize structured parameter types for improved clarity and maintainability. - Consolidated error handling and response generation, ensuring consistent API responses across methods. - Updated function signatures to eliminate unnecessary context parameters, enhancing type safety and readability. - Improved handling of query parameters and conditions, streamlining the logic for better performance and maintainability.
This commit is contained in:
@@ -1,88 +1,88 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/crawlab-team/crawlab/core/constants"
|
||||
"github.com/crawlab-team/crawlab/core/export"
|
||||
"github.com/crawlab-team/crawlab/core/interfaces"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/juju/errors"
|
||||
)
|
||||
|
||||
func PostExport(c *gin.Context) {
|
||||
exportType := c.Param("type")
|
||||
exportTarget := c.Query("target")
|
||||
exportFilter, _ := GetFilter(c)
|
||||
type PostExportParams struct {
|
||||
Type string `path:"type" validate:"required"`
|
||||
Target string `query:"target" validate:"required"`
|
||||
Filter interfaces.Filter `query:"filter"`
|
||||
}
|
||||
|
||||
func PostExport(_ *gin.Context, params *PostExportParams) (response *Response[string], err error) {
|
||||
var exportId string
|
||||
var err error
|
||||
switch exportType {
|
||||
switch params.Type {
|
||||
case constants.ExportTypeCsv:
|
||||
exportId, err = export.GetCsvService().Export(exportType, exportTarget, exportFilter)
|
||||
exportId, err = export.GetCsvService().Export(params.Type, params.Target, params.Filter)
|
||||
case constants.ExportTypeJson:
|
||||
exportId, err = export.GetJsonService().Export(exportType, exportTarget, exportFilter)
|
||||
exportId, err = export.GetJsonService().Export(params.Type, params.Target, params.Filter)
|
||||
default:
|
||||
HandleErrorBadRequest(c, errors.New(fmt.Sprintf("invalid export type: %s", exportType)))
|
||||
return
|
||||
return GetErrorResponse[string](errors.BadRequestf("invalid export type: %s", params.Type))
|
||||
}
|
||||
if err != nil {
|
||||
HandleErrorInternalServerError(c, err)
|
||||
return
|
||||
return GetErrorResponse[string](err)
|
||||
}
|
||||
|
||||
HandleSuccessWithData(c, exportId)
|
||||
return GetDataResponse(exportId)
|
||||
}
|
||||
|
||||
func GetExport(c *gin.Context) {
|
||||
exportType := c.Param("type")
|
||||
exportId := c.Param("id")
|
||||
|
||||
var exp interfaces.Export
|
||||
var err error
|
||||
switch exportType {
|
||||
case constants.ExportTypeCsv:
|
||||
exp, err = export.GetCsvService().GetExport(exportId)
|
||||
case constants.ExportTypeJson:
|
||||
exp, err = export.GetJsonService().GetExport(exportId)
|
||||
default:
|
||||
HandleErrorBadRequest(c, errors.New(fmt.Sprintf("invalid export type: %s", exportType)))
|
||||
}
|
||||
if err != nil {
|
||||
HandleErrorInternalServerError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
HandleSuccessWithData(c, exp)
|
||||
type GetExportParams struct {
|
||||
Type string `path:"type" validate:"required"`
|
||||
Id string `path:"id" validate:"required"`
|
||||
}
|
||||
|
||||
func GetExportDownload(c *gin.Context) {
|
||||
exportType := c.Param("type")
|
||||
exportId := c.Param("id")
|
||||
|
||||
func GetExport(_ *gin.Context, params *GetExportParams) (response *Response[interfaces.Export], err error) {
|
||||
var exp interfaces.Export
|
||||
var err error
|
||||
switch exportType {
|
||||
switch params.Type {
|
||||
case constants.ExportTypeCsv:
|
||||
exp, err = export.GetCsvService().GetExport(exportId)
|
||||
exp, err = export.GetCsvService().GetExport(params.Id)
|
||||
case constants.ExportTypeJson:
|
||||
exp, err = export.GetJsonService().GetExport(exportId)
|
||||
exp, err = export.GetJsonService().GetExport(params.Id)
|
||||
default:
|
||||
HandleErrorBadRequest(c, errors.New(fmt.Sprintf("invalid export type: %s", exportType)))
|
||||
return
|
||||
return GetErrorResponse[interfaces.Export](errors.BadRequestf("invalid export type: %s", params.Type))
|
||||
}
|
||||
if err != nil {
|
||||
HandleErrorInternalServerError(c, err)
|
||||
return
|
||||
return GetErrorResponse[interfaces.Export](err)
|
||||
}
|
||||
|
||||
switch exportType {
|
||||
return GetDataResponse(exp)
|
||||
}
|
||||
|
||||
type GetExportDownloadParams struct {
|
||||
Type string `path:"type" validate:"required"`
|
||||
Id string `path:"id" validate:"required"`
|
||||
}
|
||||
|
||||
func GetExportDownload(c *gin.Context, params *GetExportDownloadParams) (err error) {
|
||||
var exp interfaces.Export
|
||||
switch params.Type {
|
||||
case constants.ExportTypeCsv:
|
||||
exp, err = export.GetCsvService().GetExport(params.Id)
|
||||
case constants.ExportTypeJson:
|
||||
exp, err = export.GetJsonService().GetExport(params.Id)
|
||||
default:
|
||||
return errors.BadRequestf("invalid export type: %s", params.Type)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch params.Type {
|
||||
case constants.ExportTypeCsv:
|
||||
c.Header("Content-Type", "text/csv")
|
||||
case constants.ExportTypeJson:
|
||||
c.Header("Content-Type", "text/plain")
|
||||
default:
|
||||
HandleErrorBadRequest(c, errors.New(fmt.Sprintf("invalid export type: %s", exportType)))
|
||||
return errors.BadRequestf("invalid export type: %s", params.Type)
|
||||
}
|
||||
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", exp.GetDownloadPath()))
|
||||
c.File(exp.GetDownloadPath())
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -4,23 +4,34 @@ import (
|
||||
"github.com/crawlab-team/crawlab/core/entity"
|
||||
"github.com/crawlab-team/crawlab/core/mongo"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/juju/errors"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
mongo2 "go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
func GetFilterColFieldOptions(c *gin.Context) {
|
||||
colName := c.Param("col")
|
||||
value := c.Param("value")
|
||||
type GetFilterColFieldOptionsParams struct {
|
||||
Col string `path:"col" validate:"required"`
|
||||
Value string `path:"value"`
|
||||
Label string `path:"label"`
|
||||
Conditions string `query:"conditions" description:"Filter conditions. Format: [{\"key\":\"name\",\"op\":\"eq\",\"value\":\"test\"}]"`
|
||||
}
|
||||
|
||||
func GetFilterColFieldOptions(_ *gin.Context, params *GetFilterColFieldOptionsParams) (response *Response[[]entity.FilterSelectOption], err error) {
|
||||
value := params.Value
|
||||
if value == "" {
|
||||
value = "_id"
|
||||
}
|
||||
label := c.Param("label")
|
||||
label := params.Label
|
||||
if label == "" {
|
||||
label = "name"
|
||||
}
|
||||
query := MustGetFilterQuery(c)
|
||||
|
||||
pipelines := mongo2.Pipeline{}
|
||||
if query != nil {
|
||||
if params.Conditions != "" {
|
||||
query, err := GetFilterFromConditionString(params.Conditions)
|
||||
if err != nil {
|
||||
return GetErrorResponse[[]entity.FilterSelectOption](errors.Trace(err))
|
||||
}
|
||||
pipelines = append(pipelines, bson.D{{"$match", query}})
|
||||
}
|
||||
pipelines = append(
|
||||
@@ -60,10 +71,11 @@ func GetFilterColFieldOptions(c *gin.Context) {
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
var options []entity.FilterSelectOption
|
||||
if err := mongo.GetMongoCol(colName).Aggregate(pipelines, nil).All(&options); err != nil {
|
||||
HandleErrorInternalServerError(c, err)
|
||||
return
|
||||
if err := mongo.GetMongoCol(params.Col).Aggregate(pipelines, nil).All(&options); err != nil {
|
||||
return GetErrorResponse[[]entity.FilterSelectOption](errors.Trace(err))
|
||||
}
|
||||
HandleSuccessWithData(c, options)
|
||||
|
||||
return GetDataResponse(options)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user