diff options
author | Unknwon <u@gogs.io> | 2016-08-26 13:40:53 -0700 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2016-08-26 13:40:53 -0700 |
commit | 6b98d58906e4df063a2af59f97bd74c1c4cc1c84 (patch) | |
tree | 55ee0ed0cf2ce7984f3e724ba6a8675ece4771c1 /models | |
parent | 8dca9f95fab909c52cd34fb401f171d22f0065ae (diff) | |
download | gitea-6b98d58906e4df063a2af59f97bd74c1c4cc1c84.tar.gz gitea-6b98d58906e4df063a2af59f97bd74c1c4cc1c84.zip |
#2966 code cleanup
Diffstat (limited to 'models')
-rw-r--r-- | models/error.go | 5 | ||||
-rw-r--r-- | models/issue.go | 121 | ||||
-rw-r--r-- | models/issue_comment.go | 46 |
3 files changed, 112 insertions, 60 deletions
diff --git a/models/error.go b/models/error.go index 89c6389b0d..240e606776 100644 --- a/models/error.go +++ b/models/error.go @@ -526,7 +526,8 @@ func (err ErrPullRequestNotExist) Error() string { // \/ \/ \/ \/ \/ type ErrCommentNotExist struct { - ID int64 + ID int64 + IssueID int64 } func IsErrCommentNotExist(err error) bool { @@ -535,7 +536,7 @@ func IsErrCommentNotExist(err error) bool { } func (err ErrCommentNotExist) Error() string { - return fmt.Sprintf("comment does not exist [id: %d]", err.ID) + return fmt.Sprintf("comment does not exist [id: %d, issue_id: %d]", err.ID, err.IssueID) } // .____ ___. .__ diff --git a/models/issue.go b/models/issue.go index 1446056e5d..a90ebaf866 100644 --- a/models/issue.go +++ b/models/issue.go @@ -73,70 +73,55 @@ func (issue *Issue) BeforeUpdate() { } func (issue *Issue) AfterSet(colName string, _ xorm.Cell) { - var err error switch colName { - case "id": - issue.Attachments, err = GetAttachmentsByIssueID(issue.ID) - if err != nil { - log.Error(3, "GetAttachmentsByIssueID[%d]: %v", issue.ID, err) - } - - issue.Comments, err = GetCommentsByIssueID(issue.ID) - if err != nil { - log.Error(3, "GetCommentsByIssueID[%d]: %v", issue.ID, err) - } + case "deadline_unix": + issue.Deadline = time.Unix(issue.DeadlineUnix, 0).Local() + case "created_unix": + issue.Created = time.Unix(issue.CreatedUnix, 0).Local() + case "updated_unix": + issue.Updated = time.Unix(issue.UpdatedUnix, 0).Local() + } +} - issue.Labels, err = GetLabelsByIssueID(issue.ID) +func (issue *Issue) loadAttributes(e Engine) (err error) { + if issue.Repo == nil { + issue.Repo, err = getRepositoryByID(e, issue.RepoID) if err != nil { - log.Error(3, "GetLabelsByIssueID[%d]: %v", issue.ID, err) + return fmt.Errorf("getRepositoryByID [%d]: %v", issue.RepoID, err) } + } - case "poster_id": - issue.Poster, err = GetUserByID(issue.PosterID) + if issue.Poster == nil { + issue.Poster, err = getUserByID(e, issue.PosterID) if err != nil { if IsErrUserNotExist(err) { issue.PosterID = -1 issue.Poster = NewGhostUser() } else { - log.Error(3, "GetUserByID[%d]: %v", issue.ID, err) + return fmt.Errorf("getUserByID.(poster) [%d]: %v", issue.PosterID, err) } return } + } - case "milestone_id": - if issue.MilestoneID == 0 { - return - } - - issue.Milestone, err = GetMilestoneByRepoID(issue.RepoID, issue.MilestoneID) + if issue.Labels == nil { + issue.Labels, err = getLabelsByIssueID(e, issue.ID) if err != nil { - log.Error(3, "GetMilestoneById[%d]: %v", issue.ID, err) - } - - case "assignee_id": - if issue.AssigneeID == 0 { - return + return fmt.Errorf("getLabelsByIssueID [%d]: %v", issue.ID, err) } + } - issue.Assignee, err = GetUserByID(issue.AssigneeID) + if issue.Milestone == nil && issue.MilestoneID > 0 { + issue.Milestone, err = getMilestoneByRepoID(e, issue.RepoID, issue.MilestoneID) if err != nil { - log.Error(3, "GetUserByID[%d]: %v", issue.ID, err) + return fmt.Errorf("getMilestoneByRepoID [repo_id: %d, milestone_id: %d]: %v", issue.RepoID, issue.MilestoneID, err) } - - case "deadline_unix": - issue.Deadline = time.Unix(issue.DeadlineUnix, 0).Local() - case "created_unix": - issue.Created = time.Unix(issue.CreatedUnix, 0).Local() - case "updated_unix": - issue.Updated = time.Unix(issue.UpdatedUnix, 0).Local() } -} -func (issue *Issue) loadAttributes(e Engine) (err error) { - if issue.Repo == nil { - issue.Repo, err = getRepositoryByID(e, issue.RepoID) + if issue.Assignee == nil && issue.AssigneeID > 0 { + issue.Assignee, err = getUserByID(e, issue.AssigneeID) if err != nil { - return fmt.Errorf("getRepositoryByID [%d]: %v", issue.RepoID, err) + return fmt.Errorf("getUserByID.(assignee) [%d]: %v", issue.AssigneeID, err) } } @@ -148,6 +133,20 @@ func (issue *Issue) loadAttributes(e Engine) (err error) { } } + if issue.Attachments == nil { + issue.Attachments, err = getAttachmentsByIssueID(e, issue.ID) + if err != nil { + return fmt.Errorf("getAttachmentsByIssueID [%d]: %v", issue.ID, err) + } + } + + if issue.Comments == nil { + issue.Comments, err = getCommentsByIssueID(e, issue.ID) + if err != nil { + return fmt.Errorf("getCommentsByIssueID [%d]: %v", issue.ID, err) + } + } + return nil } @@ -757,8 +756,8 @@ func GetIssueByRef(ref string) (*Issue, error) { return issue, issue.LoadAttributes() } -// GetIssueByIndex returns issue by given index in repository. -func GetIssueByIndex(repoID, index int64) (*Issue, error) { +// GetIssueByIndex returns raw issue without loading attributes by index in a repository. +func GetRawIssueByIndex(repoID, index int64) (*Issue, error) { issue := &Issue{ RepoID: repoID, Index: index, @@ -769,6 +768,15 @@ func GetIssueByIndex(repoID, index int64) (*Issue, error) { } else if !has { return nil, ErrIssueNotExist{0, repoID, index} } + return issue, nil +} + +// GetIssueByIndex returns issue by index in a repository. +func GetIssueByIndex(repoID, index int64) (*Issue, error) { + issue, err := GetRawIssueByIndex(repoID, index) + if err != nil { + return nil, err + } return issue, issue.LoadAttributes() } @@ -868,7 +876,18 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) { } issues := make([]*Issue, 0, setting.UI.IssuePagingNum) - return issues, sess.Find(&issues) + if err := sess.Find(&issues); err != nil { + return nil, fmt.Errorf("Find: %v", err) + } + + // FIXME: use IssueList to improve performance. + for i := range issues { + if err := issues[i].LoadAttributes(); err != nil { + return nil, fmt.Errorf("LoadAttributes [%d]: %v", issues[i].ID, err) + } + } + + return issues, nil } // .___ ____ ___ @@ -1412,7 +1431,7 @@ func getMilestoneByRepoID(e Engine, repoID, id int64) (*Milestone, error) { return m, nil } -// GetWebhookByRepoID returns milestone of repository by given ID. +// GetWebhookByRepoID returns the milestone in a repository. func GetMilestoneByRepoID(repoID, id int64) (*Milestone, error) { return getMilestoneByRepoID(x, repoID, id) } @@ -1721,10 +1740,14 @@ func GetAttachmentByUUID(uuid string) (*Attachment, error) { return getAttachmentByUUID(x, uuid) } -// GetAttachmentsByIssueID returns all attachments for given issue by ID. -func GetAttachmentsByIssueID(issueID int64) ([]*Attachment, error) { +func getAttachmentsByIssueID(e Engine, issueID int64) ([]*Attachment, error) { attachments := make([]*Attachment, 0, 10) - return attachments, x.Where("issue_id=? AND comment_id=0", issueID).Find(&attachments) + return attachments, e.Where("issue_id = ? AND comment_id = 0", issueID).Find(&attachments) +} + +// GetAttachmentsByIssueID returns all attachments of an issue. +func GetAttachmentsByIssueID(issueID int64) ([]*Attachment, error) { + return getAttachmentsByIssueID(x, issueID) } // GetAttachmentsByCommentID returns all attachments if comment by given ID. diff --git a/models/issue_comment.go b/models/issue_comment.go index 8abc063328..c914e0a56f 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -11,6 +11,7 @@ import ( "github.com/Unknwon/com" "github.com/go-xorm/xorm" + api "github.com/gogits/go-gogs-client" "github.com/gogits/gogs/modules/log" @@ -59,6 +60,8 @@ type Comment struct { Created time.Time `xorm:"-"` CreatedUnix int64 + Updated time.Time `xorm:"-"` + UpdatedUnix int64 // Reference issue in commit message CommitSHA string `xorm:"VARCHAR(40)"` @@ -71,6 +74,11 @@ type Comment struct { func (c *Comment) BeforeInsert() { c.CreatedUnix = time.Now().Unix() + c.UpdatedUnix = c.CreatedUnix +} + +func (c *Comment) BeforeUpdate() { + c.UpdatedUnix = time.Now().Unix() } func (c *Comment) AfterSet(colName string, _ xorm.Cell) { @@ -94,6 +102,8 @@ func (c *Comment) AfterSet(colName string, _ xorm.Cell) { } case "created_unix": c.Created = time.Unix(c.CreatedUnix, 0).Local() + case "updated_unix": + c.Updated = time.Unix(c.UpdatedUnix, 0).Local() } } @@ -105,16 +115,14 @@ func (c *Comment) AfterDelete() { } } -// APIFormat convert Comment struct to api.Comment struct func (c *Comment) APIFormat() *api.Comment { - apiComment := &api.Comment{ + return &api.Comment{ ID: c.ID, Poster: c.Poster.APIFormat(), Body: c.Content, Created: c.Created, + Updated: c.Updated, } - - return apiComment } // HashTag returns unique hash tag for comment. @@ -341,15 +349,32 @@ func GetCommentByID(id int64) (*Comment, error) { if err != nil { return nil, err } else if !has { - return nil, ErrCommentNotExist{id} + return nil, ErrCommentNotExist{id, 0} } return c, nil } -// GetCommentsByIssueID returns all comments of issue by given ID. -func GetCommentsByIssueID(issueID int64) ([]*Comment, error) { +func getCommentsByIssueIDSince(e Engine, issueID, since int64) ([]*Comment, error) { comments := make([]*Comment, 0, 10) - return comments, x.Where("issue_id=?", issueID).Asc("created_unix").Find(&comments) + sess := e.Where("issue_id = ?", issueID).Asc("created_unix") + if since > 0 { + sess.And("created_unix >= ?", since) + } + return comments, sess.Find(&comments) +} + +func getCommentsByIssueID(e Engine, issueID int64) ([]*Comment, error) { + return getCommentsByIssueIDSince(e, issueID, -1) +} + +// GetCommentsByIssueID returns all comments of an issue. +func GetCommentsByIssueID(issueID int64) ([]*Comment, error) { + return getCommentsByIssueID(x, issueID) +} + +// GetCommentsByIssueID returns a list of comments of an issue since a given time point. +func GetCommentsByIssueIDSince(issueID, since int64) ([]*Comment, error) { + return getCommentsByIssueIDSince(x, issueID, since) } // UpdateComment updates information of comment. @@ -358,10 +383,13 @@ func UpdateComment(c *Comment) error { return err } -// DeleteCommentByID deletes a comment by given ID. +// DeleteCommentByID deletes the comment by given ID. func DeleteCommentByID(id int64) error { comment, err := GetCommentByID(id) if err != nil { + if IsErrCommentNotExist(err) { + return nil + } return err } |