feat(database): validation
Some checks failed
Gitea Build Check / Build (pull_request) Failing after 3m0s

- added more validation for IDsi
This commit is contained in:
SoXX 2024-10-15 12:36:56 +02:00
parent 59f404883e
commit f70879cf8d
2 changed files with 21 additions and 13 deletions

View File

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

View File

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