feat(database): implementation of functions
Some checks failed
Gitea Build Check / Build (push) Failing after 28s

- 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.
This commit is contained in:
SoXX 2024-08-09 22:16:03 +02:00
parent 598ca747eb
commit f798869ef5

View File

@ -2,23 +2,107 @@ package database
import ( import (
"context" "context"
"errors"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
"gorm.io/gorm"
) )
func CreateUserSource(ctx context.Context, userSource models.UserSource) (models.UserSource, error) { func CreateUserSource(ctx context.Context, userSource models.UserSource) (models.UserSource, error) {
var user models.UserSource if client == nil {
return user, 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 { 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 return nil
} }
func GetUserSourceByID(ctx context.Context, id models.UserSourceID) (models.UserSource, error) { func GetUserSourceByID(ctx context.Context, id models.UserSourceID) (models.UserSource, error) {
var user models.UserSource 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 return user, nil
} }
func DeleteUserSource(ctx context.Context, id models.UserSourceID) error { 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 return nil
} }