109 lines
3.0 KiB
Go
109 lines
3.0 KiB
Go
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) {
|
|
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.UserSourceIDIsEmpty}
|
|
}
|
|
|
|
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.UserSourceIDIsEmpty}
|
|
}
|
|
|
|
if len(id) != 25 {
|
|
return models.UserSource{}, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDToShort}
|
|
}
|
|
|
|
result := client.WithContext(ctx).First(&user, "id = ?", 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.UserSourceIDIsEmpty}
|
|
}
|
|
|
|
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
|
|
}
|