Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use XXXBean instead of XXXExample
  • Loading branch information
lunny committed Nov 21, 2021
commit 33f6a18e49c9177f5157d3dca3017c2a6bf0a48a
12 changes: 6 additions & 6 deletions models/db/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,18 @@ func Exec(ctx context.Context, sqlAndArgs ...interface{}) (sql.Result, error) {
return GetEngine(ctx).Exec(sqlAndArgs...)
}

// GetByExample filled empty fields of the bean according non-empty fields to query in database.
func GetByExample(ctx context.Context, bean interface{}) (bool, error) {
// GetByBean filled empty fields of the bean according non-empty fields to query in database.
func GetByBean(ctx context.Context, bean interface{}) (bool, error) {
return GetEngine(ctx).Get(bean)
}

// DeleteByExample deletes all records according non-empty fields of the bean as conditions.
func DeleteByExample(ctx context.Context, bean interface{}) (int64, error) {
// DeleteByBean deletes all records according non-empty fields of the bean as conditions.
func DeleteByBean(ctx context.Context, bean interface{}) (int64, error) {
return GetEngine(ctx).Delete(bean)
}

// CountByExample counts the number of database records according non-empty fields of the bean as conditions.
func CountByExample(ctx context.Context, bean interface{}) (int64, error) {
// CountByBean counts the number of database records according non-empty fields of the bean as conditions.
func CountByBean(ctx context.Context, bean interface{}) (int64, error) {
return GetEngine(ctx).Count(bean)
}

Expand Down
6 changes: 3 additions & 3 deletions models/lfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func NewLFSMetaObject(m *LFSMetaObject) (*LFSMetaObject, error) {
}
defer committer.Close()

has, err := db.GetByExample(ctx, m)
has, err := db.GetByBean(ctx, m)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -99,11 +99,11 @@ func (repo *Repository) RemoveLFSMetaObjectByOid(oid string) (int64, error) {
defer committer.Close()

m := &LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}, RepositoryID: repo.ID}
if _, err := db.DeleteByExample(ctx, m); err != nil {
if _, err := db.DeleteByBean(ctx, m); err != nil {
return -1, err
}

count, err := db.CountByExample(ctx, &LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}})
count, err := db.CountByBean(ctx, &LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}})
if err != nil {
return count, err
}
Expand Down
8 changes: 4 additions & 4 deletions models/login/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func ReadSession(key string) (*Session, error) {
}
defer committer.Close()

if has, err := db.GetByExample(ctx, &session); err != nil {
if has, err := db.GetByBean(ctx, &session); err != nil {
return nil, err
} else if !has {
session.Expiry = timeutil.TimeStampNow()
Expand Down Expand Up @@ -79,15 +79,15 @@ func RegenerateSession(oldKey, newKey string) (*Session, error) {
}
defer committer.Close()

if has, err := db.GetByExample(ctx, &Session{
if has, err := db.GetByBean(ctx, &Session{
Key: newKey,
}); err != nil {
return nil, err
} else if has {
return nil, fmt.Errorf("session Key: %s already exists", newKey)
}

if has, err := db.GetByExample(ctx, &Session{
if has, err := db.GetByBean(ctx, &Session{
Key: oldKey,
}); err != nil {
return nil, err
Expand All @@ -107,7 +107,7 @@ func RegenerateSession(oldKey, newKey string) (*Session, error) {
s := Session{
Key: newKey,
}
if _, err := db.GetByExample(ctx, &s); err != nil {
if _, err := db.GetByBean(ctx, &s); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion models/star.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func StarRepo(userID, repoID int64, star bool) error {
return nil
}

if _, err := db.DeleteByExample(ctx, &Star{UID: userID, RepoID: repoID}); err != nil {
if _, err := db.DeleteByBean(ctx, &Star{UID: userID, RepoID: repoID}); err != nil {
return err
}
if _, err := db.Exec(ctx, "UPDATE `repository` SET num_stars = num_stars - 1 WHERE id = ?", repoID); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion models/user/follow.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func UnfollowUser(userID, followID int64) (err error) {
}
defer committer.Close()

if _, err = db.DeleteByExample(ctx, &Follow{UserID: userID, FollowID: followID}); err != nil {
if _, err = db.DeleteByBean(ctx, &Follow{UserID: userID, FollowID: followID}); err != nil {
return err
}

Expand Down
6 changes: 3 additions & 3 deletions models/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,11 +513,11 @@ func deleteWebhook(bean *Webhook) (err error) {
}
defer committer.Close()

if count, err := db.DeleteByExample(ctx, bean); err != nil {
if count, err := db.DeleteByBean(ctx, bean); err != nil {
return err
} else if count == 0 {
return ErrWebhookNotExist{ID: bean.ID}
} else if _, err = db.DeleteByExample(ctx, &HookTask{HookID: bean.ID}); err != nil {
} else if _, err = db.DeleteByBean(ctx, &HookTask{HookID: bean.ID}); err != nil {
return err
}

Expand Down Expand Up @@ -557,7 +557,7 @@ func DeleteDefaultSystemWebhook(id int64) error {
return ErrWebhookNotExist{ID: id}
}

if _, err := db.DeleteByExample(ctx, &HookTask{HookID: id}); err != nil {
if _, err := db.DeleteByBean(ctx, &HookTask{HookID: id}); err != nil {
return err
}

Expand Down