2024-07-19 08:03:35 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-08-09 20:16:03 +00:00
|
|
|
"errors"
|
|
|
|
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error"
|
2024-07-22 09:11:48 +00:00
|
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
|
2024-08-09 20:16:03 +00:00
|
|
|
"gorm.io/gorm"
|
2024-07-19 08:03:35 +00:00
|
|
|
)
|
|
|
|
|
2024-08-09 19:37:54 +00:00
|
|
|
func CreateUserSource(ctx context.Context, userSource models.UserSource) (models.UserSource, error) {
|
2024-08-09 20:16:03 +00:00
|
|
|
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
|
2024-08-09 19:37:54 +00:00
|
|
|
}
|
2024-07-19 08:03:35 +00:00
|
|
|
|
2024-08-09 19:37:54 +00:00
|
|
|
func UpdateUserSource(ctx context.Context, userSource models.UserSource) error {
|
2024-08-09 20:16:03 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-08-09 19:37:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-07-19 08:03:35 +00:00
|
|
|
|
2024-08-09 19:37:54 +00:00
|
|
|
func GetUserSourceByID(ctx context.Context, id models.UserSourceID) (models.UserSource, error) {
|
|
|
|
var user models.UserSource
|
2024-08-09 20:16:03 +00:00
|
|
|
|
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
2024-08-09 20:44:55 +00:00
|
|
|
result := client.WithContext(ctx).First(&user, "id = ?", id)
|
2024-08-09 20:16:03 +00:00
|
|
|
if result.Error != nil {
|
|
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
|
|
return models.UserSource{}, &otterError.Database{Reason: otterError.NoDataFound}
|
|
|
|
}
|
|
|
|
return models.UserSource{}, result.Error
|
|
|
|
}
|
|
|
|
|
2024-08-09 19:37:54 +00:00
|
|
|
return user, nil
|
|
|
|
}
|
2024-07-19 08:03:35 +00:00
|
|
|
|
2024-08-09 19:37:54 +00:00
|
|
|
func DeleteUserSource(ctx context.Context, id models.UserSourceID) error {
|
2024-08-09 20:16:03 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-08-09 19:37:54 +00:00
|
|
|
return nil
|
2024-07-19 08:03:35 +00:00
|
|
|
}
|