diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2022-05-20 22:08:52 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-20 22:08:52 +0800 |
commit | fd7d83ace60258acf7139c4c787aa8af75b7ba8c (patch) | |
tree | 50038348ec10485f72344f3ac80324e04abc1283 /models/repo/attachment.go | |
parent | d81e31ad7826a81fc7139f329f250594610a274b (diff) | |
download | gitea-fd7d83ace60258acf7139c4c787aa8af75b7ba8c.tar.gz gitea-fd7d83ace60258acf7139c4c787aa8af75b7ba8c.zip |
Move almost all functions' parameter db.Engine to context.Context (#19748)
* Move almost all functions' parameter db.Engine to context.Context
* remove some unnecessary wrap functions
Diffstat (limited to 'models/repo/attachment.go')
-rw-r--r-- | models/repo/attachment.go | 66 |
1 files changed, 17 insertions, 49 deletions
diff --git a/models/repo/attachment.go b/models/repo/attachment.go index f5351578fb..ddddac2c3d 100644 --- a/models/repo/attachment.go +++ b/models/repo/attachment.go @@ -60,11 +60,6 @@ func (a *Attachment) DownloadURL() string { return setting.AppURL + "attachments/" + url.PathEscape(a.UUID) } -// GetAttachmentByID returns attachment by given id -func GetAttachmentByID(id int64) (*Attachment, error) { - return getAttachmentByID(db.GetEngine(db.DefaultContext), id) -} - // _____ __ __ .__ __ // / _ \_/ |__/ |______ ____ | |__ _____ ____ _____/ |_ // / /_\ \ __\ __\__ \ _/ ___\| | \ / \_/ __ \ / \ __\ @@ -88,9 +83,10 @@ func (err ErrAttachmentNotExist) Error() string { return fmt.Sprintf("attachment does not exist [id: %d, uuid: %s]", err.ID, err.UUID) } -func getAttachmentByID(e db.Engine, id int64) (*Attachment, error) { +// GetAttachmentByID returns attachment by given id +func GetAttachmentByID(ctx context.Context, id int64) (*Attachment, error) { attach := &Attachment{} - if has, err := e.ID(id).Get(attach); err != nil { + if has, err := db.GetEngine(ctx).ID(id).Get(attach); err != nil { return nil, err } else if !has { return nil, ErrAttachmentNotExist{ID: id, UUID: ""} @@ -98,9 +94,10 @@ func getAttachmentByID(e db.Engine, id int64) (*Attachment, error) { return attach, nil } -func getAttachmentByUUID(e db.Engine, uuid string) (*Attachment, error) { +// GetAttachmentByUUID returns attachment by given UUID. +func GetAttachmentByUUID(ctx context.Context, uuid string) (*Attachment, error) { attach := &Attachment{} - has, err := e.Where("uuid=?", uuid).Get(attach) + has, err := db.GetEngine(ctx).Where("uuid=?", uuid).Get(attach) if err != nil { return nil, err } else if !has { @@ -111,22 +108,13 @@ func getAttachmentByUUID(e db.Engine, uuid string) (*Attachment, error) { // GetAttachmentsByUUIDs returns attachment by given UUID list. func GetAttachmentsByUUIDs(ctx context.Context, uuids []string) ([]*Attachment, error) { - return getAttachmentsByUUIDs(db.GetEngine(ctx), uuids) -} - -func getAttachmentsByUUIDs(e db.Engine, uuids []string) ([]*Attachment, error) { if len(uuids) == 0 { return []*Attachment{}, nil } // Silently drop invalid uuids. attachments := make([]*Attachment, 0, len(uuids)) - return attachments, e.In("uuid", uuids).Find(&attachments) -} - -// GetAttachmentByUUID returns attachment by given UUID. -func GetAttachmentByUUID(uuid string) (*Attachment, error) { - return getAttachmentByUUID(db.GetEngine(db.DefaultContext), uuid) + return attachments, db.GetEngine(ctx).In("uuid", uuids).Find(&attachments) } // ExistAttachmentsByUUID returns true if attachment is exist by given UUID @@ -134,37 +122,22 @@ func ExistAttachmentsByUUID(uuid string) (bool, error) { return db.GetEngine(db.DefaultContext).Where("`uuid`=?", uuid).Exist(new(Attachment)) } -// GetAttachmentByReleaseIDFileName returns attachment by given releaseId and fileName. -func GetAttachmentByReleaseIDFileName(releaseID int64, fileName string) (*Attachment, error) { - return getAttachmentByReleaseIDFileName(db.GetEngine(db.DefaultContext), releaseID, fileName) -} - -// GetAttachmentsByIssueIDCtx returns all attachments of an issue. -func GetAttachmentsByIssueIDCtx(ctx context.Context, issueID int64) ([]*Attachment, error) { +// GetAttachmentsByIssueID returns all attachments of an issue. +func GetAttachmentsByIssueID(ctx context.Context, issueID int64) ([]*Attachment, error) { attachments := make([]*Attachment, 0, 10) return attachments, db.GetEngine(ctx).Where("issue_id = ? AND comment_id = 0", issueID).Find(&attachments) } -// GetAttachmentsByIssueID returns all attachments of an issue. -func GetAttachmentsByIssueID(issueID int64) ([]*Attachment, error) { - return GetAttachmentsByIssueIDCtx(db.DefaultContext, issueID) -} - // GetAttachmentsByCommentID returns all attachments if comment by given ID. -func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) { - return GetAttachmentsByCommentIDCtx(db.DefaultContext, commentID) -} - -// GetAttachmentsByCommentIDCtx returns all attachments if comment by given ID. -func GetAttachmentsByCommentIDCtx(ctx context.Context, commentID int64) ([]*Attachment, error) { +func GetAttachmentsByCommentID(ctx context.Context, commentID int64) ([]*Attachment, error) { attachments := make([]*Attachment, 0, 10) return attachments, db.GetEngine(ctx).Where("comment_id=?", commentID).Find(&attachments) } -// getAttachmentByReleaseIDFileName return a file based on the the following infos: -func getAttachmentByReleaseIDFileName(e db.Engine, releaseID int64, fileName string) (*Attachment, error) { +// GetAttachmentByReleaseIDFileName returns attachment by given releaseId and fileName. +func GetAttachmentByReleaseIDFileName(ctx context.Context, releaseID int64, fileName string) (*Attachment, error) { attach := &Attachment{ReleaseID: releaseID, Name: fileName} - has, err := e.Get(attach) + has, err := db.GetEngine(ctx).Get(attach) if err != nil { return nil, err } else if !has { @@ -207,7 +180,7 @@ func DeleteAttachments(ctx context.Context, attachments []*Attachment, remove bo // DeleteAttachmentsByIssue deletes all attachments associated with the given issue. func DeleteAttachmentsByIssue(issueID int64, remove bool) (int, error) { - attachments, err := GetAttachmentsByIssueID(issueID) + attachments, err := GetAttachmentsByIssueID(db.DefaultContext, issueID) if err != nil { return 0, err } @@ -217,7 +190,7 @@ func DeleteAttachmentsByIssue(issueID int64, remove bool) (int, error) { // DeleteAttachmentsByComment deletes all attachments associated with the given comment. func DeleteAttachmentsByComment(commentID int64, remove bool) (int, error) { - attachments, err := GetAttachmentsByCommentID(commentID) + attachments, err := GetAttachmentsByCommentID(db.DefaultContext, commentID) if err != nil { return 0, err } @@ -225,11 +198,6 @@ func DeleteAttachmentsByComment(commentID int64, remove bool) (int, error) { return DeleteAttachments(db.DefaultContext, attachments, remove) } -// UpdateAttachment updates the given attachment in database -func UpdateAttachment(atta *Attachment) error { - return UpdateAttachmentCtx(db.DefaultContext, atta) -} - // UpdateAttachmentByUUID Updates attachment via uuid func UpdateAttachmentByUUID(ctx context.Context, attach *Attachment, cols ...string) error { if attach.UUID == "" { @@ -239,8 +207,8 @@ func UpdateAttachmentByUUID(ctx context.Context, attach *Attachment, cols ...str return err } -// UpdateAttachmentCtx updates the given attachment in database -func UpdateAttachmentCtx(ctx context.Context, atta *Attachment) error { +// UpdateAttachment updates the given attachment in database +func UpdateAttachment(ctx context.Context, atta *Attachment) error { sess := db.GetEngine(ctx).Cols("name", "issue_id", "release_id", "comment_id", "download_count") if atta.ID != 0 && atta.UUID == "" { sess = sess.ID(atta.ID) |