From f2772b5920967f5dacc3de27dee2bc1b464533e2 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 13 Feb 2023 13:11:41 +0800 Subject: Move delete user to service (#22478) Move delete user to service Co-authored-by: delvh Co-authored-by: Jason Song --- models/db/context.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'models/db') diff --git a/models/db/context.go b/models/db/context.go index 911dbd1c6f..4b3f7f0ee7 100644 --- a/models/db/context.go +++ b/models/db/context.go @@ -7,6 +7,7 @@ import ( "context" "database/sql" + "xorm.io/builder" "xorm.io/xorm" "xorm.io/xorm/schemas" ) @@ -183,6 +184,31 @@ func DeleteByBean(ctx context.Context, bean interface{}) (int64, error) { return GetEngine(ctx).Delete(bean) } +// DeleteByID deletes the given bean with the given ID +func DeleteByID(ctx context.Context, id int64, bean interface{}) (int64, error) { + return GetEngine(ctx).ID(id).NoAutoTime().Delete(bean) +} + +// FindIDs finds the IDs for the given table name satisfying the given condition +// By passing a different value than "id" for "idCol", you can query for foreign IDs, i.e. the repo IDs which satisfy the condition +func FindIDs(ctx context.Context, tableName, idCol string, cond builder.Cond) ([]int64, error) { + ids := make([]int64, 0, 10) + if err := GetEngine(ctx).Table(tableName). + Cols(idCol). + Where(cond). + Find(&ids); err != nil { + return nil, err + } + return ids, nil +} + +// DecrByIDs decreases the given column for entities of the "bean" type with one of the given ids by one +// Timestamps of the entities won't be updated +func DecrByIDs(ctx context.Context, ids []int64, decrCol string, bean interface{}) error { + _, err := GetEngine(ctx).Decr(decrCol).In("id", ids).NoAutoCondition().NoAutoTime().Update(bean) + return err +} + // DeleteBeans deletes all given beans, beans should contain delete conditions. func DeleteBeans(ctx context.Context, beans ...interface{}) (err error) { e := GetEngine(ctx) -- cgit v1.2.3