diff --git a/pkg/database/favorite.go b/pkg/database/favorite.go index e21fe97..1e642d2 100644 --- a/pkg/database/favorite.go +++ b/pkg/database/favorite.go @@ -158,7 +158,7 @@ func GetUserFavoritesByID(ctx context.Context, id models.UserFavoriteID) (models return userFavorites, nil } -func DeleteUserFavorite(ctx context.Context, id models.UserFavoriteID) error { +func DeleteUserFavorite(ctx context.Context, id models.UserFavoriteID, hardDelete bool) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteUserFavorite") defer span.End() @@ -186,7 +186,15 @@ func DeleteUserFavorite(ctx context.Context, id models.UserFavoriteID) error { return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIsWrongLength}) } - result := client.WithContext(ctx).Delete(&userFavorite, id) + db := client + + if hardDelete { + db = client.Unscoped() + } else { + db = client + } + + result := db.WithContext(ctx).Delete(&userFavorite, id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) diff --git a/pkg/database/pool.go b/pkg/database/pool.go index 8c5b3fe..85c416b 100644 --- a/pkg/database/pool.go +++ b/pkg/database/pool.go @@ -122,7 +122,7 @@ func UpdatePool(ctx context.Context, pool models.Pool) error { return nil } -func DeletePool(ctx context.Context, id models.PoolID) error { +func DeletePool(ctx context.Context, id models.PoolID, hardDelete bool) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeletePool") defer span.End() @@ -150,7 +150,15 @@ func DeletePool(ctx context.Context, id models.PoolID) error { return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsWrongLength}) } - result := client.WithContext(ctx).Delete(&pool, "id = ?", id) + db := client + + if hardDelete { + db = client.Unscoped() + } else { + db = client + } + + result := db.WithContext(ctx).Delete(&pool, "id = ?", id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) diff --git a/pkg/database/post.go b/pkg/database/post.go index fb4ddc0..11a2f01 100644 --- a/pkg/database/post.go +++ b/pkg/database/post.go @@ -166,7 +166,7 @@ func UpdatePost(ctx context.Context, anthrovePost models.Post) error { return nil } -func DeletePost(ctx context.Context, id models.PostID) error { +func DeletePost(ctx context.Context, id models.PostID, hardDelete bool) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeletePost") defer span.End() @@ -194,7 +194,15 @@ func DeletePost(ctx context.Context, id models.PostID) error { return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsWrongLength}) } - result := client.WithContext(ctx).Delete(&userFavorite, "id = ?", id) + db := client + + if hardDelete { + db = client.Unscoped() + } else { + db = client + } + + result := db.WithContext(ctx).Delete(&userFavorite, "id = ?", id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) diff --git a/pkg/database/post_report.go b/pkg/database/post_report.go index dceb894..9bafd6b 100644 --- a/pkg/database/post_report.go +++ b/pkg/database/post_report.go @@ -132,7 +132,7 @@ func UpdatePostReport(ctx context.Context, postReport models.PostReport) error { return nil } -func DeletePostReport(ctx context.Context, id models.PostReportID) error { +func DeletePostReport(ctx context.Context, id models.PostReportID, hardDelete bool) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeletePostReport") defer span.End() @@ -160,7 +160,15 @@ func DeletePostReport(ctx context.Context, id models.PostReportID) error { return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostReportIDIsWrongLength}) } - result := client.WithContext(ctx).Delete(&postReport, "id = ?", id) + db := client + + if hardDelete { + db = client.Unscoped() + } else { + db = client + } + + result := db.WithContext(ctx).Delete(&postReport, "id = ?", id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) diff --git a/pkg/database/source.go b/pkg/database/source.go index 04c4c1b..2c7fd05 100644 --- a/pkg/database/source.go +++ b/pkg/database/source.go @@ -206,7 +206,7 @@ func GetSourceByDomain(ctx context.Context, sourceDomain models.SourceDomain) (m return source, nil } -func DeleteSource(ctx context.Context, id models.SourceID) error { +func DeleteSource(ctx context.Context, id models.SourceID, hardDelete bool) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteSource") defer span.End() @@ -234,7 +234,15 @@ func DeleteSource(ctx context.Context, id models.SourceID) error { return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsWrongLength}) } - result := client.WithContext(ctx).Delete(&source, id) + db := client + + if hardDelete { + db = client.Unscoped() + } else { + db = client + } + + result := db.WithContext(ctx).Delete(&source, id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) diff --git a/pkg/database/tag.go b/pkg/database/tag.go index d6c6fc4..70cb833 100644 --- a/pkg/database/tag.go +++ b/pkg/database/tag.go @@ -97,7 +97,7 @@ func CreateTagInBatch(ctx context.Context, tags []models.Tag, batchSize int) ([] return tags, nil } -func DeleteTag(ctx context.Context, tagID models.TagID) error { +func DeleteTag(ctx context.Context, tagID models.TagID, hardDelete bool) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteTag") defer span.End() @@ -117,7 +117,15 @@ func DeleteTag(ctx context.Context, tagID models.TagID) error { return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } - result := client.WithContext(ctx).Delete(&tag, tagID) + db := client + + if hardDelete { + db = client.Unscoped() + } else { + db = client + } + + result := db.WithContext(ctx).Delete(&tag, tagID) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) diff --git a/pkg/database/user.go b/pkg/database/user.go index 5204b41..12c9cb0 100644 --- a/pkg/database/user.go +++ b/pkg/database/user.go @@ -75,7 +75,7 @@ func GetUserByID(ctx context.Context, id models.UserID) (models.User, error) { return user, nil } -func DeleteUser(ctx context.Context, id models.UserID) error { +func DeleteUser(ctx context.Context, id models.UserID, hardDelete bool) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteUser") defer span.End() @@ -95,7 +95,15 @@ func DeleteUser(ctx context.Context, id models.UserID) error { return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } - result := client.WithContext(ctx).Delete(&user, id) + db := client + + if hardDelete { + db = client.Unscoped() + } else { + db = client + } + + result := db.WithContext(ctx).Delete(&user, id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) diff --git a/pkg/database/userSource.go b/pkg/database/userSource.go index 5b225c3..86dca43 100644 --- a/pkg/database/userSource.go +++ b/pkg/database/userSource.go @@ -133,7 +133,7 @@ func GetUserSourceByID(ctx context.Context, id models.UserSourceID) (models.User return user, nil } -func DeleteUserSource(ctx context.Context, id models.UserSourceID) error { +func DeleteUserSource(ctx context.Context, id models.UserSourceID, hardDelete bool) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteUserSource") defer span.End() @@ -160,7 +160,15 @@ func DeleteUserSource(ctx context.Context, id models.UserSourceID) error { return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIsWrongLength}) } - result := client.WithContext(ctx).Delete(&user, id) + db := client + + if hardDelete { + db = client.Unscoped() + } else { + db = client + } + + result := db.WithContext(ctx).Delete(&user, id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})