Fixed creating a new dependency when the other issue didn't exists

Signed-off-by: Konrad <konrad@kola-entertainments.de>
This commit is contained in:
Konrad Langenberg 2017-08-27 20:32:57 +02:00 committed by Konrad
parent cf7e99fc4c
commit b2cb173199
2 changed files with 31 additions and 21 deletions

View File

@ -42,42 +42,48 @@ func (iw *IssueDependency) BeforeUpdate() {
iw.UpdatedUnix = u iw.UpdatedUnix = u
} }
// CreateOrUpdateIssueDependency creates a new dependency for an issue // CreateIssueDependency creates a new dependency for an issue
func CreateOrUpdateIssueDependency(userID, issueID int64, depID int64) (err error, exists bool) { func CreateIssueDependency(userID, issueID int64, depID int64) (err error, exists bool, depExists bool) {
err = x.Sync(new(IssueDependency)) err = x.Sync(new(IssueDependency))
if err != nil { if err != nil {
return err, exists return err, exists, false
} }
// Check if it aleready exists // Check if it aleready exists
exists, err = issueDepExists(x, issueID, depID) exists, err = issueDepExists(x, issueID, depID)
if err != nil { if err != nil {
return err, exists return err, exists, false
} }
// If it not exists, create it, otherwise show an error message // If it not exists, create it, otherwise show an error message
if !exists { if !exists {
newId := new(IssueDependency) // Check if the other issue exists
newId.UserID = userID var issue = Issue{}
newId.IssueID = issueID issueExists, err := x.Id(depID).Get(&issue)
newId.DependencyID = depID if issueExists {
newId := new(IssueDependency)
newId.UserID = userID
newId.IssueID = issueID
newId.DependencyID = depID
if _, err := x.Insert(newId); err != nil { if _, err := x.Insert(newId); err != nil {
return err, exists return err, exists, false
} }
// Add comment referencing to the stopwatch // Add comment referencing the new dependency
comment := &Comment{ comment := &Comment{
IssueID: issueID, IssueID: issueID,
PosterID: userID, PosterID: userID,
Type: CommentTypeAddedDependency, Type: CommentTypeAddedDependency,
} }
if _, err := x.Insert(comment); err != nil { if _, err := x.Insert(comment); err != nil {
return err, exists return err, exists, false
}
} }
return err, exists, true
} }
return nil, exists return nil, exists, false
} }
// Check if the dependency already exists // Check if the dependency already exists

View File

@ -28,12 +28,16 @@ func AddDependency(c *context.Context) {
return return
} }
err, exists := models.CreateOrUpdateIssueDependency(c.User.ID, issue.ID, dep); err, exists, depExists := models.CreateIssueDependency(c.User.ID, issue.ID, dep);
if err != nil { if err != nil {
c.Handle(http.StatusInternalServerError, "CreateOrUpdateIssueDependency", err) c.Handle(http.StatusInternalServerError, "CreateOrUpdateIssueDependency", err)
return return
} }
if depExists {
c.Flash.Error("Dependend issue does not exist!")
}
if exists { if exists {
c.Flash.Error("Dependency already exists!") c.Flash.Error("Dependency already exists!")
} }