diff options
author | Andrey Nering <andrey.nering@gmail.com> | 2017-03-29 20:31:47 -0300 |
---|---|---|
committer | Andrey Nering <andrey.nering@gmail.com> | 2017-03-29 20:31:47 -0300 |
commit | b6744607484008826d18f129326664105b9d7bfc (patch) | |
tree | 77a41fe51c110e211bc1b0de95f163e62859c781 /models/issue_watch.go | |
parent | a0d0de7233cd8a85d6572ae13d74078482a1ee27 (diff) | |
download | gitea-b6744607484008826d18f129326664105b9d7bfc.tar.gz gitea-b6744607484008826d18f129326664105b9d7bfc.zip |
Add watch button on issue
Diffstat (limited to 'models/issue_watch.go')
-rw-r--r-- | models/issue_watch.go | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/models/issue_watch.go b/models/issue_watch.go index 96e080136f..d082211c77 100644 --- a/models/issue_watch.go +++ b/models/issue_watch.go @@ -16,5 +16,45 @@ type IssueWatch struct { // BeforeInsert is invoked from XORM before inserting an object of this type. func (iw *IssueWatch) BeforeInsert() { + iw.Created = time.Now() iw.CreatedUnix = time.Now().Unix() } + +// CreateOrUpdateIssueWatch set watching for a user and issue +func CreateOrUpdateIssueWatch(userID, issueID int64, isWatching bool) error { + iw, exists, err := getIssueWatch(x, userID, issueID) + if err != nil { + return err + } + + if !exists { + iw = &IssueWatch{ + UserID: userID, + IssueID: issueID, + IsWatching: isWatching, + } + + if _, err := x.Insert(iw); err != nil { + return err + } + } else { + if _, err := x.Table(&IssueWatch{}).Id(iw.ID).Update(map[string]interface{}{"is_watching": isWatching}); err != nil { + return err + } + } + return nil +} + +// GetIssueWatch returns an issue watch by user and issue +func GetIssueWatch(userID, issueID int64) (iw *IssueWatch, exists bool, err error) { + iw, exists, err = getIssueWatch(x, userID, issueID) + return +} +func getIssueWatch(e Engine, userID, issueID int64) (iw *IssueWatch, exists bool, err error) { + iw = new(IssueWatch) + exists, err = e. + Where("user_id = ?", userID). + And("issue_id = ?", issueID). + Get(iw) + return +} |