From f798869ef5fcb9722d5b210b4c09a437e7a47c06 Mon Sep 17 00:00:00 2001 From: SoXX Date: Fri, 9 Aug 2024 22:16:03 +0200 Subject: [PATCH] feat(database): implementation of functions - Added functionality - Add connection checks to `CreateUserSource`, `UpdateUserSource`, `GetUserSourceByID`, and `DeleteUserSource` functions. - Introduce validation for empty and improperly formatted IDs in `UpdateUserSource`, `GetUserSourceByID`, and `DeleteUserSource`. - Handle duplicate key errors and record not found errors with appropriate custom error messages. --- pkg/database/source.go | 88 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 2 deletions(-) diff --git a/pkg/database/source.go b/pkg/database/source.go index 3967a3f..d9d1db5 100644 --- a/pkg/database/source.go +++ b/pkg/database/source.go @@ -2,23 +2,107 @@ package database import ( "context" + "errors" + otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "gorm.io/gorm" ) func CreateUserSource(ctx context.Context, userSource models.UserSource) (models.UserSource, error) { - var user models.UserSource - return user, nil + if client == nil { + return models.UserSource{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + result := client.WithContext(ctx).Create(&userSource) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrDuplicatedKey) { + return models.UserSource{}, &otterError.Database{Reason: otterError.DuplicateKey} + } + return models.UserSource{}, result.Error + } + + return userSource, nil } func UpdateUserSource(ctx context.Context, userSource models.UserSource) error { + if client == nil { + return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if len(userSource.ID) == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDEmpty} + } + + updatedUserSource := models.UserSource{ + BaseModel: models.BaseModel[models.UserSourceID]{ + ID: userSource.ID, + }, + ScrapeTimeInterval: userSource.ScrapeTimeInterval, + AccountUsername: userSource.AccountUsername, + AccountID: userSource.AccountID, + LastScrapeTime: userSource.LastScrapeTime, + AccountValidate: userSource.AccountValidate, + } + + result := client.WithContext(ctx).Updates(&updatedUserSource) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrDuplicatedKey) { + return &otterError.Database{Reason: otterError.DuplicateKey} + } + return result.Error + } + return nil } func GetUserSourceByID(ctx context.Context, id models.UserSourceID) (models.UserSource, error) { var user models.UserSource + + if client == nil { + return models.UserSource{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if len(id) == 0 { + return models.UserSource{}, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDEmpty} + } + + if len(id) != 25 { + return models.UserSource{}, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDToShort} + } + + result := client.WithContext(ctx).First(&user, id) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return models.UserSource{}, &otterError.Database{Reason: otterError.NoDataFound} + } + return models.UserSource{}, result.Error + } + return user, nil } func DeleteUserSource(ctx context.Context, id models.UserSourceID) error { + var user models.UserSource + + if client == nil { + return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if len(id) == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDEmpty} + } + + if len(id) != 25 { + return &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDToShort} + } + + result := client.WithContext(ctx).Delete(&user, id) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return &otterError.Database{Reason: otterError.NoDataFound} + } + return result.Error + } + return nil }