Use db.Find instead of writing methods for every object (#28084)
For those simple objects, it's unnecessary to write the find and count methods again and again.
This commit is contained in:
parent
d24a8223ce
commit
df1e7d0067
88 changed files with 611 additions and 685 deletions
|
@ -435,7 +435,7 @@ type ListWebhookOptions struct {
|
|||
IsActive util.OptionalBool
|
||||
}
|
||||
|
||||
func (opts *ListWebhookOptions) toCond() builder.Cond {
|
||||
func (opts ListWebhookOptions) ToConds() builder.Cond {
|
||||
cond := builder.NewCond()
|
||||
if opts.RepoID != 0 {
|
||||
cond = cond.And(builder.Eq{"webhook.repo_id": opts.RepoID})
|
||||
|
@ -449,27 +449,6 @@ func (opts *ListWebhookOptions) toCond() builder.Cond {
|
|||
return cond
|
||||
}
|
||||
|
||||
// ListWebhooksByOpts return webhooks based on options
|
||||
func ListWebhooksByOpts(ctx context.Context, opts *ListWebhookOptions) ([]*Webhook, error) {
|
||||
sess := db.GetEngine(ctx).Where(opts.toCond())
|
||||
|
||||
if opts.Page != 0 {
|
||||
sess = db.SetSessionPagination(sess, opts)
|
||||
webhooks := make([]*Webhook, 0, opts.PageSize)
|
||||
err := sess.Find(&webhooks)
|
||||
return webhooks, err
|
||||
}
|
||||
|
||||
webhooks := make([]*Webhook, 0, 10)
|
||||
err := sess.Find(&webhooks)
|
||||
return webhooks, err
|
||||
}
|
||||
|
||||
// CountWebhooksByOpts count webhooks based on options and ignore pagination
|
||||
func CountWebhooksByOpts(ctx context.Context, opts *ListWebhookOptions) (int64, error) {
|
||||
return db.GetEngine(ctx).Where(opts.toCond()).Count(&Webhook{})
|
||||
}
|
||||
|
||||
// UpdateWebhook updates information of webhook.
|
||||
func UpdateWebhook(ctx context.Context, w *Webhook) error {
|
||||
_, err := db.GetEngine(ctx).ID(w.ID).AllCols().Update(w)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue