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

Backport 

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

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
6543 2020-06-17 21:54:26 +02:00 committed by GitHub
parent 313ace93d0
commit dbe9c11238
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 25 additions and 29 deletions

@ -136,9 +136,8 @@ func GetAttachmentByID(id int64) (*Attachment, error) {
} }
func getAttachmentByID(e Engine, id int64) (*Attachment, error) { func getAttachmentByID(e Engine, id int64) (*Attachment, error) {
attach := &Attachment{ID: id} attach := &Attachment{}
if has, err := e.ID(id).Get(attach); err != nil {
if has, err := e.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: ""}
@ -147,8 +146,8 @@ func getAttachmentByID(e Engine, id int64) (*Attachment, error) {
} }
func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) { func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) {
attach := &Attachment{UUID: uuid} attach := &Attachment{}
has, err := e.Get(attach) 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 {

@ -231,8 +231,8 @@ func getProtectedBranchBy(e Engine, repoID int64, branchName string) (*Protected
// 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} rel := &ProtectedBranch{}
has, err := x.Get(rel) has, err := x.ID(id).Get(rel)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -521,9 +521,9 @@ func (repo *Repository) GetDeletedBranches() ([]*DeletedBranch, error) {
} }
// GetDeletedBranchByID get a deleted branch by its ID // GetDeletedBranchByID get a deleted branch by its ID
func (repo *Repository) GetDeletedBranchByID(ID int64) (*DeletedBranch, error) { func (repo *Repository) GetDeletedBranchByID(id int64) (*DeletedBranch, error) {
deletedBranch := &DeletedBranch{ID: ID} deletedBranch := &DeletedBranch{}
has, err := x.Get(deletedBranch) has, err := x.ID(id).Get(deletedBranch)
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -212,11 +212,8 @@ func getLabelInRepoByName(e Engine, repoID int64, labelName string) (*Label, err
return nil, ErrLabelNotExist{0, repoID} return nil, ErrLabelNotExist{0, repoID}
} }
l := &Label{ l := &Label{}
Name: labelName, has, err := e.Where("name=? AND repo_id=?", labelName, repoID).Get(l)
RepoID: repoID,
}
has, err := e.Get(l)
if err != nil { if err != nil {
return nil, err return nil, err
} else if !has { } else if !has {

@ -300,7 +300,7 @@ func (source *LoginSource) SSPI() *SSPIConfig {
// 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 {

@ -142,8 +142,8 @@ func UpdateTwoFactor(t *TwoFactor) error {
// 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} twofa := &TwoFactor{}
has, err := x.Get(twofa) 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 {

@ -76,8 +76,8 @@ func NewUpload(name string, buf []byte, file multipart.File) (_ *Upload, err err
// 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} upload := &Upload{}
has, err := x.Get(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 {

@ -1457,8 +1457,8 @@ func GetUserByEmail(email string) (*User, error) {
// 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} user := &User{}
has, err := x.Get(user) has, err := x.Where("lower_name=?", username).Get(user)
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -71,8 +71,8 @@ func GetEmailAddresses(uid int64) ([]*EmailAddress, error) {
// 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} email := &EmailAddress{UID: uid}
if has, err := x.Get(email); err != nil { 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
@ -126,7 +126,7 @@ func isEmailUsed(e Engine, email string) (bool, error) {
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.
@ -251,8 +251,8 @@ func MakeEmailPrimary(email *EmailAddress) error {
return ErrEmailNotActivated return ErrEmailNotActivated
} }
user := &User{ID: email.UID} user := &User{}
has, err = x.Get(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 {

@ -111,8 +111,8 @@ func GetUserByOpenID(uri string) (*User, error) {
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} oid := &UserOpenID{}
has, err := x.Get(oid) has, err := x.Where("uri=?", uri).Get(oid)
if err != nil { if err != nil {
return nil, err return nil, err
} }