Browse Source

Use ID or Where to instead directly use Get when load object from database (#11925) (#11935)

Backport #11925

Use ID or Where to instead directly use Get when load object from database

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
tags/v1.11.7
6543 3 years ago
parent
commit
dbe9c11238
No account linked to committer's email address

+ 4
- 5
models/attachment.go View File

} }


func getAttachmentByID(e Engine, id int64) (*Attachment, error) { func getAttachmentByID(e Engine, id int64) (*Attachment, error) {
attach := &Attachment{ID: id}

if has, err := e.Get(attach); err != nil {
attach := &Attachment{}
if has, err := e.ID(id).Get(attach); err != nil {
return nil, err return nil, err
} else if !has { } else if !has {
return nil, ErrAttachmentNotExist{ID: id, UUID: ""} return nil, ErrAttachmentNotExist{ID: id, UUID: ""}
} }


func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) { func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) {
attach := &Attachment{UUID: uuid}
has, err := e.Get(attach)
attach := &Attachment{}
has, err := e.Where("uuid=?", uuid).Get(attach)
if err != nil { if err != nil {
return nil, err return nil, err
} else if !has { } else if !has {

+ 5
- 5
models/branches.go View File



// GetProtectedBranchByID getting protected branch by ID // GetProtectedBranchByID getting protected branch by ID
func GetProtectedBranchByID(id int64) (*ProtectedBranch, error) { func GetProtectedBranchByID(id int64) (*ProtectedBranch, error) {
rel := &ProtectedBranch{ID: id}
has, err := x.Get(rel)
rel := &ProtectedBranch{}
has, err := x.ID(id).Get(rel)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} }


// GetDeletedBranchByID get a deleted branch by its ID // GetDeletedBranchByID get a deleted branch by its ID
func (repo *Repository) GetDeletedBranchByID(ID int64) (*DeletedBranch, error) {
deletedBranch := &DeletedBranch{ID: ID}
has, err := x.Get(deletedBranch)
func (repo *Repository) GetDeletedBranchByID(id int64) (*DeletedBranch, error) {
deletedBranch := &DeletedBranch{}
has, err := x.ID(id).Get(deletedBranch)
if err != nil { if err != nil {
return nil, err return nil, err
} }

+ 2
- 5
models/issue_label.go View File

return nil, ErrLabelNotExist{0, repoID} return nil, ErrLabelNotExist{0, repoID}
} }


l := &Label{
Name: labelName,
RepoID: repoID,
}
has, err := e.Get(l)
l := &Label{}
has, err := e.Where("name=? AND repo_id=?", labelName, repoID).Get(l)
if err != nil { if err != nil {
return nil, err return nil, err
} else if !has { } else if !has {

+ 1
- 1
models/login_source.go View File

// CreateLoginSource inserts a LoginSource in the DB if not already // CreateLoginSource inserts a LoginSource in the DB if not already
// existing with the given name. // existing with the given name.
func CreateLoginSource(source *LoginSource) error { func CreateLoginSource(source *LoginSource) error {
has, err := x.Get(&LoginSource{Name: source.Name})
has, err := x.Where("name=?", source.Name).Exist(new(LoginSource))
if err != nil { if err != nil {
return err return err
} else if has { } else if has {

+ 2
- 2
models/twofactor.go View File

// GetTwoFactorByUID returns the two-factor authentication token associated with // GetTwoFactorByUID returns the two-factor authentication token associated with
// the user, if any. // the user, if any.
func GetTwoFactorByUID(uid int64) (*TwoFactor, error) { func GetTwoFactorByUID(uid int64) (*TwoFactor, error) {
twofa := &TwoFactor{UID: uid}
has, err := x.Get(twofa)
twofa := &TwoFactor{}
has, err := x.Where("uid=?", uid).Get(twofa)
if err != nil { if err != nil {
return nil, err return nil, err
} else if !has { } else if !has {

+ 2
- 2
models/upload.go View File



// GetUploadByUUID returns the Upload by UUID // GetUploadByUUID returns the Upload by UUID
func GetUploadByUUID(uuid string) (*Upload, error) { func GetUploadByUUID(uuid string) (*Upload, error) {
upload := &Upload{UUID: uuid}
has, err := x.Get(upload)
upload := &Upload{}
has, err := x.Where("uuid=?", uuid).Get(upload)
if err != nil { if err != nil {
return nil, err return nil, err
} else if !has { } else if !has {

+ 2
- 2
models/user.go View File

// Finally, if email address is the protected email address: // Finally, if email address is the protected email address:
if strings.HasSuffix(email, fmt.Sprintf("@%s", setting.Service.NoReplyAddress)) { if strings.HasSuffix(email, fmt.Sprintf("@%s", setting.Service.NoReplyAddress)) {
username := strings.TrimSuffix(email, fmt.Sprintf("@%s", setting.Service.NoReplyAddress)) username := strings.TrimSuffix(email, fmt.Sprintf("@%s", setting.Service.NoReplyAddress))
user := &User{LowerName: username}
has, err := x.Get(user)
user := &User{}
has, err := x.Where("lower_name=?", username).Get(user)
if err != nil { if err != nil {
return nil, err return nil, err
} }

+ 5
- 5
models/user_mail.go View File

// GetEmailAddressByID gets a user's email address by ID // GetEmailAddressByID gets a user's email address by ID
func GetEmailAddressByID(uid, id int64) (*EmailAddress, error) { func GetEmailAddressByID(uid, id int64) (*EmailAddress, error) {
// User ID is required for security reasons // User ID is required for security reasons
email := &EmailAddress{ID: id, UID: uid}
if has, err := x.Get(email); err != nil {
email := &EmailAddress{UID: uid}
if has, err := x.ID(id).Get(email); err != nil {
return nil, err return nil, err
} else if !has { } else if !has {
return nil, nil return nil, nil
return true, nil return true, nil
} }


return e.Get(&EmailAddress{Email: email})
return e.Where("email=?", email).Get(&EmailAddress{})
} }


// IsEmailUsed returns true if the email has been used. // IsEmailUsed returns true if the email has been used.
return ErrEmailNotActivated return ErrEmailNotActivated
} }


user := &User{ID: email.UID}
has, err = x.Get(user)
user := &User{}
has, err = x.ID(email.UID).Get(user)
if err != nil { if err != nil {
return err return err
} else if !has { } else if !has {

+ 2
- 2
models/user_openid.go View File

log.Trace("Normalized OpenID URI: " + uri) log.Trace("Normalized OpenID URI: " + uri)


// Otherwise, check in openid table // Otherwise, check in openid table
oid := &UserOpenID{URI: uri}
has, err := x.Get(oid)
oid := &UserOpenID{}
has, err := x.Where("uri=?", uri).Get(oid)
if err != nil { if err != nil {
return nil, err return nil, err
} }

Loading…
Cancel
Save