feat: add hard delete option to delete functions
Some checks failed
Gitea Build Check / Build (pull_request) Failing after 31s

This commit is contained in:
soxx 2025-01-28 13:59:39 +01:00
parent ecbf139a9b
commit c9b0c71bef
8 changed files with 80 additions and 16 deletions

View File

@ -158,7 +158,7 @@ func GetUserFavoritesByID(ctx context.Context, id models.UserFavoriteID) (models
return userFavorites, nil 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") ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteUserFavorite")
defer span.End() 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}) 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 result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})

View File

@ -122,7 +122,7 @@ func UpdatePool(ctx context.Context, pool models.Pool) error {
return nil 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") ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeletePool")
defer span.End() 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}) 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 result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})

View File

@ -166,7 +166,7 @@ func UpdatePost(ctx context.Context, anthrovePost models.Post) error {
return nil 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") ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeletePost")
defer span.End() 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}) 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 result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})

View File

@ -132,7 +132,7 @@ func UpdatePostReport(ctx context.Context, postReport models.PostReport) error {
return nil 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") ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeletePostReport")
defer span.End() 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}) 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 result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})

View File

@ -206,7 +206,7 @@ func GetSourceByDomain(ctx context.Context, sourceDomain models.SourceDomain) (m
return source, nil 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") ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteSource")
defer span.End() 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}) 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 result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})

View File

@ -97,7 +97,7 @@ func CreateTagInBatch(ctx context.Context, tags []models.Tag, batchSize int) ([]
return tags, nil 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") ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteTag")
defer span.End() 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}) 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 result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})

View File

@ -75,7 +75,7 @@ func GetUserByID(ctx context.Context, id models.UserID) (models.User, error) {
return user, nil 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") ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteUser")
defer span.End() 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}) 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 result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})

View File

@ -133,7 +133,7 @@ func GetUserSourceByID(ctx context.Context, id models.UserSourceID) (models.User
return user, nil 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") ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteUserSource")
defer span.End() 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}) 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 result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})