diff options
author | 6543 <6543@obermui.de> | 2019-12-31 09:21:21 +0100 |
---|---|---|
committer | techknowlogick <techknowlogick@gitea.io> | 2019-12-31 03:21:21 -0500 |
commit | 9600c27085df3f895b2128f1b77aa0ce0b57e7f2 (patch) | |
tree | 9dfbbf44126e7c39b77558ec943c8a2b9a717b10 /models/issue_reaction.go | |
parent | 655aea13a5ad5dd51bcaafd1b96ecce2673f0312 (diff) | |
download | gitea-9600c27085df3f895b2128f1b77aa0ce0b57e7f2.tar.gz gitea-9600c27085df3f895b2128f1b77aa0ce0b57e7f2.zip |
[API] Fix 9544 | return 200 when reaction already exist (#9550)
* add ErrReactionAlreadyExist
* extend CreateReaction
* reaction already exist = 200
* extend FindReactionsOptions
* refactor swagger options/definitions
* fix swagger-validate
* Update models/error.go
Co-Authored-By: zeripath <art27@cantab.net>
* fix test PART1
* extend FindReactionsOptions with UserID option
* catch error on test
* fix test PART2
* format ...
Co-authored-by: zeripath <art27@cantab.net>
Co-authored-by: techknowlogick <matti@mdranta.net>
Diffstat (limited to 'models/issue_reaction.go')
-rw-r--r-- | models/issue_reaction.go | 34 |
1 files changed, 29 insertions, 5 deletions
diff --git a/models/issue_reaction.go b/models/issue_reaction.go index 6896eeeafc..d421ab44e9 100644 --- a/models/issue_reaction.go +++ b/models/issue_reaction.go @@ -30,6 +30,8 @@ type Reaction struct { type FindReactionsOptions struct { IssueID int64 CommentID int64 + UserID int64 + Reaction string } func (opts *FindReactionsOptions) toConds() builder.Cond { @@ -46,6 +48,12 @@ func (opts *FindReactionsOptions) toConds() builder.Cond { } else if opts.CommentID == -1 { cond = cond.And(builder.Eq{"reaction.comment_id": 0}) } + if opts.UserID > 0 { + cond = cond.And(builder.Eq{"reaction.user_id": opts.UserID}) + } + if opts.Reaction != "" { + cond = cond.And(builder.Eq{"reaction.type": opts.Reaction}) + } return cond } @@ -80,9 +88,25 @@ func createReaction(e *xorm.Session, opts *ReactionOptions) (*Reaction, error) { UserID: opts.Doer.ID, IssueID: opts.Issue.ID, } + findOpts := FindReactionsOptions{ + IssueID: opts.Issue.ID, + CommentID: -1, // reaction to issue only + Reaction: opts.Type, + UserID: opts.Doer.ID, + } if opts.Comment != nil { reaction.CommentID = opts.Comment.ID + findOpts.CommentID = opts.Comment.ID } + + existingR, err := findReactions(e, findOpts) + if err != nil { + return nil, err + } + if len(existingR) > 0 { + return existingR[0], ErrReactionAlreadyExist{Reaction: opts.Type} + } + if _, err := e.Insert(reaction); err != nil { return nil, err } @@ -99,23 +123,23 @@ type ReactionOptions struct { } // CreateReaction creates reaction for issue or comment. -func CreateReaction(opts *ReactionOptions) (reaction *Reaction, err error) { +func CreateReaction(opts *ReactionOptions) (*Reaction, error) { if !setting.UI.ReactionsMap[opts.Type] { return nil, ErrForbiddenIssueReaction{opts.Type} } sess := x.NewSession() defer sess.Close() - if err = sess.Begin(); err != nil { + if err := sess.Begin(); err != nil { return nil, err } - reaction, err = createReaction(sess, opts) + reaction, err := createReaction(sess, opts) if err != nil { - return nil, err + return reaction, err } - if err = sess.Commit(); err != nil { + if err := sess.Commit(); err != nil { return nil, err } return reaction, nil |