mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-26 17:49:15 +01:00
refactor: standardize response types across controllers
- Updated multiple controller methods to return VoidResponse instead of generic Response[any]. - Consolidated error handling to utilize GetErrorVoidResponse for consistent error responses. - Enhanced parameter handling in export and file management functions for improved clarity and maintainability. - Refactored health check and login/logout methods to align with new response structure. - Improved overall consistency in response formatting across various endpoints.
This commit is contained in:
@@ -36,7 +36,7 @@ func (svc *CsvService) GenerateId() (exportId string, err error) {
|
||||
return exportId, nil
|
||||
}
|
||||
|
||||
func (svc *CsvService) Export(exportType, target string, filter interfaces.Filter) (exportId string, err error) {
|
||||
func (svc *CsvService) Export(exportType, target string, query bson.M) (exportId string, err error) {
|
||||
// generate export id
|
||||
exportId, err = svc.GenerateId()
|
||||
if err != nil {
|
||||
@@ -48,7 +48,7 @@ func (svc *CsvService) Export(exportType, target string, filter interfaces.Filte
|
||||
Id: exportId,
|
||||
Type: exportType,
|
||||
Target: target,
|
||||
Filter: filter,
|
||||
Query: query,
|
||||
Status: constants.TaskStatusRunning,
|
||||
StartTs: time.Now(),
|
||||
FileName: svc.getFileName(exportId),
|
||||
@@ -90,18 +90,8 @@ func (svc *CsvService) export(export *entity.Export) {
|
||||
// mongo collection
|
||||
col := mongo.GetMongoCol(export.Target)
|
||||
|
||||
// mongo query
|
||||
query, err := utils.FilterToQuery(export.Filter)
|
||||
if err != nil {
|
||||
export.Status = constants.TaskStatusError
|
||||
export.EndTs = time.Now()
|
||||
svc.Errorf("export error (id: %s): %v", export.Id, err)
|
||||
svc.cache.Set(export.Id, export)
|
||||
return
|
||||
}
|
||||
|
||||
// mongo cursor
|
||||
cur := col.Find(query, nil).GetCursor()
|
||||
cur := col.Find(export.Query, nil).GetCursor()
|
||||
|
||||
// csv writer
|
||||
csvWriter, csvFile, err := svc.getCsvWriter(export)
|
||||
@@ -126,7 +116,7 @@ func (svc *CsvService) export(export *entity.Export) {
|
||||
}
|
||||
|
||||
// write csv header row
|
||||
columns, err := svc.getColumns(query, export)
|
||||
columns, err := svc.getColumns(export.Query, export)
|
||||
err = csvWriter.Write(columns)
|
||||
if err != nil {
|
||||
export.Status = constants.TaskStatusError
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/crawlab-team/crawlab/core/mongo"
|
||||
"github.com/crawlab-team/crawlab/core/utils"
|
||||
"github.com/hashicorp/go-uuid"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
mongo2 "go.mongodb.org/mongo-driver/mongo"
|
||||
"os"
|
||||
"path"
|
||||
@@ -31,7 +32,7 @@ func (svc *JsonService) GenerateId() (exportId string, err error) {
|
||||
return exportId, nil
|
||||
}
|
||||
|
||||
func (svc *JsonService) Export(exportType, target string, filter interfaces.Filter) (exportId string, err error) {
|
||||
func (svc *JsonService) Export(exportType, target string, query bson.M) (exportId string, err error) {
|
||||
// generate export id
|
||||
exportId, err = svc.GenerateId()
|
||||
if err != nil {
|
||||
@@ -43,7 +44,7 @@ func (svc *JsonService) Export(exportType, target string, filter interfaces.Filt
|
||||
Id: exportId,
|
||||
Type: exportType,
|
||||
Target: target,
|
||||
Filter: filter,
|
||||
Query: query,
|
||||
Status: constants.TaskStatusRunning,
|
||||
StartTs: time.Now(),
|
||||
FileName: svc.getFileName(exportId),
|
||||
@@ -85,18 +86,8 @@ func (svc *JsonService) export(export *entity.Export) {
|
||||
// mongo collection
|
||||
col := mongo.GetMongoCol(export.Target)
|
||||
|
||||
// mongo query
|
||||
query, err := utils.FilterToQuery(export.Filter)
|
||||
if err != nil {
|
||||
export.Status = constants.TaskStatusError
|
||||
export.EndTs = time.Now()
|
||||
svc.Errorf("export error (id: %s): %v", export.Id, err)
|
||||
svc.cache.Set(export.Id, export)
|
||||
return
|
||||
}
|
||||
|
||||
// mongo cursor
|
||||
cur := col.Find(query, nil).GetCursor()
|
||||
cur := col.Find(export.Query, nil).GetCursor()
|
||||
|
||||
// data
|
||||
var jsonData []interface{}
|
||||
|
||||
Reference in New Issue
Block a user