feat(database): implementation of functions
Some checks failed
Gitea Build Check / Build (push) Failing after 28s
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:
parent
598ca747eb
commit
f798869ef5
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user