From f70879cf8dbd624e7ec75e5a64a84313ef1b7b32 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 15 Oct 2024 12:36:56 +0200 Subject: [PATCH] feat(database): validation - added more validation for IDsi --- pkg/database/scrape_history.go | 32 ++++++++++++++++++++------------ pkg/error/validation.go | 2 +- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/pkg/database/scrape_history.go b/pkg/database/scrape_history.go index 8cf424f..c57412f 100644 --- a/pkg/database/scrape_history.go +++ b/pkg/database/scrape_history.go @@ -32,6 +32,14 @@ func CreateScrapeHistory(ctx context.Context, scrapeHistory models.ScrapeHistory return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } + if len(scrapeHistory.ScrapeTaskID) == 0 { + return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsEmpty}) + } + + if len(scrapeHistory.ScrapeTaskID) != 25 { + return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsWrongLength}) + } + result := client.WithContext(ctx).Create(&scrapeHistory) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { @@ -60,14 +68,14 @@ func GetScrapeHistoryByID(ctx context.Context, id models.ScrapeTaskID) (models.S var post models.ScrapeHistory - if client == nil { - return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) - } - if len(id) == 0 { return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsEmpty}) } + if len(id) != 25 { + return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsWrongLength}) + } + result := client.WithContext(ctx).First(&post, "scrape_task_id = ?", id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { @@ -100,14 +108,14 @@ func UpdateScrapeHistory(ctx context.Context, scrapeHistory models.ScrapeHistory utils.HandleEvent(span, localLogger, "Starting scrapeHistory update") - if client == nil { - return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) - } - if len(scrapeHistory.ScrapeTaskID) == 0 { return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsEmpty}) } + if len(scrapeHistory.ScrapeTaskID) != 25 { + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsWrongLength}) + } + updateScrapeHistory := models.ScrapeHistory{ ScrapeTaskID: scrapeHistory.ScrapeTaskID, UserSourceID: scrapeHistory.UserSourceID, @@ -145,14 +153,14 @@ func DeleteScrapeHistory(ctx context.Context, id models.ScrapeTaskID) error { var scrapeHistory models.ScrapeHistory - if client == nil { - return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) - } - if len(id) == 0 { return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsEmpty}) } + if len(id) != 25 { + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsWrongLength}) + } + result := client.WithContext(ctx).Delete(&scrapeHistory, "scrape_task_id = ?", id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { diff --git a/pkg/error/validation.go b/pkg/error/validation.go index 2fe7289..ab53258 100644 --- a/pkg/error/validation.go +++ b/pkg/error/validation.go @@ -37,7 +37,7 @@ const ( PoolURLIsEmpty = "PoolURL cannot be empty" ScrapeTaskIDIsEmpty = "ScrapeTaskID cannot be empty" - ScrapeTaskIDIsWrongLength = "ScrapeTaskID has the wrong length" + ScrapeTaskIDIsWrongLength = "ScrapeTaskID has the wrong length, needs to be 25 characters long" ) type EntityValidationFailed struct {