71 lines
1.3 KiB
Go
71 lines
1.3 KiB
Go
|
package repository
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"database/sql"
|
||
|
"time"
|
||
|
|
||
|
"github.com/huandu/go-sqlbuilder"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
timeZero = time.Time{}
|
||
|
)
|
||
|
|
||
|
func deleteFromTable(ctx context.Context, conn *sql.DB, tableName string, id int64) (int64, error) {
|
||
|
b := sqlbuilder.NewDeleteBuilder()
|
||
|
b.DeleteFrom(tableName)
|
||
|
b.Where(
|
||
|
b.Equal("Id", id),
|
||
|
)
|
||
|
query, args := b.Build()
|
||
|
|
||
|
_, err := conn.ExecContext(ctx, query, args...)
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
return 1, nil
|
||
|
}
|
||
|
|
||
|
func restoreRow(ctx context.Context, conn *sql.DB, tableName string, id int64) (int64, error) {
|
||
|
timeZero := time.Time{}
|
||
|
b := sqlbuilder.NewUpdateBuilder()
|
||
|
b.Update(tableName)
|
||
|
b.Set(
|
||
|
b.Assign("UpdatedAt", time.Now()),
|
||
|
b.Assign("DeletedAt", timeZero),
|
||
|
)
|
||
|
b.Where(
|
||
|
b.Equal("Id", id),
|
||
|
)
|
||
|
query, args := b.Build()
|
||
|
|
||
|
_, err := conn.ExecContext(ctx, query, args...)
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
return 1, nil
|
||
|
}
|
||
|
|
||
|
func softDeleteRow(ctx context.Context, conn *sql.DB, tableName string, id int64) (int64, error) {
|
||
|
now := time.Now()
|
||
|
b := sqlbuilder.NewUpdateBuilder()
|
||
|
b.Update(tableName)
|
||
|
b.Set(
|
||
|
b.Assign("UpdatedAt", now),
|
||
|
b.Assign("DeletedAt", now),
|
||
|
)
|
||
|
b.Where(
|
||
|
b.Equal("Id", id),
|
||
|
)
|
||
|
query, args := b.Build()
|
||
|
|
||
|
_, err := conn.ExecContext(ctx, query, args...)
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
return 1, nil
|
||
|
}
|