From 2eee1e9bc22d77a68b436682fdd4eb8f60fd5d45 Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 13 May 2014 13:28:21 -0400 Subject: [PATCH] Finish edit a milestone --- cmd/web.go | 3 +- models/issue.go | 22 +++++++- routers/repo/issue.go | 84 +++++++++++++++++++++++++---- templates/issue/milestone.tmpl | 2 +- templates/issue/milestone_edit.tmpl | 11 ++-- 5 files changed, 102 insertions(+), 20 deletions(-) diff --git a/cmd/web.go b/cmd/web.go index c343566d7..d2b148d7e 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -188,7 +188,8 @@ func runWeb(*cli.Context) { r.Get("/issues/milestones", repo.Milestones) r.Get("/issues/milestones/new", repo.NewMilestone) r.Post("/issues/milestones/new", bindIgnErr(auth.CreateMilestoneForm{}), repo.NewMilestonePost) - r.Get("/issues/milestones/edit", repo.UpdateMilestones) + r.Get("/issues/milestones/:index/edit", repo.UpdateMilestone) + r.Post("/issues/milestones/:index/edit", bindIgnErr(auth.CreateMilestoneForm{}), repo.UpdateMilestonePost) r.Post("/comment/:action", repo.Comment) r.Get("/releases/new", repo.ReleasesNew) }, reqSignIn, middleware.RepoAssignment(true)) diff --git a/models/issue.go b/models/issue.go index fa9d9427a..213fd1f2a 100644 --- a/models/issue.go +++ b/models/issue.go @@ -14,7 +14,8 @@ import ( ) var ( - ErrIssueNotExist = errors.New("Issue does not exist") + ErrIssueNotExist = errors.New("Issue does not exist") + ErrMilestoneNotExist = errors.New("Milestone does not exist") ) // Issue represents an issue or pull request of repository. @@ -412,6 +413,7 @@ type Milestone struct { NumOpenIssues int `xorm:"-"` Completeness int // Percentage(1-100). Deadline time.Time + DeadlineString string `xorm:"-"` ClosedDate time.Time } @@ -441,6 +443,18 @@ func NewMilestone(m *Milestone) (err error) { return sess.Commit() } +// GetMilestoneByIndex returns the milestone of given repository and index. +func GetMilestoneByIndex(repoId, idx int64) (*Milestone, error) { + m := &Milestone{RepoId: repoId, Index: idx} + has, err := orm.Get(m) + if err != nil { + return nil, err + } else if !has { + return nil, ErrMilestoneNotExist + } + return m, nil +} + // GetMilestones returns a list of milestones of given repository and status. func GetMilestones(repoId int64, isClosed bool) ([]*Milestone, error) { miles := make([]*Milestone, 0, 10) @@ -448,6 +462,12 @@ func GetMilestones(repoId int64, isClosed bool) ([]*Milestone, error) { return miles, err } +// UpdateMilestone updates information of given milestone. +func UpdateMilestone(m *Milestone) error { + _, err := orm.Id(m.Id).Update(m) + return err +} + // Issue types. const ( IT_PLAIN = iota // Pure comment. diff --git a/routers/repo/issue.go b/routers/repo/issue.go index 2958d8eb1..38a971a0f 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -524,23 +524,22 @@ func NewMilestonePost(ctx *middleware.Context, form auth.CreateMilestoneForm) { var deadline time.Time var err error if len(form.Deadline) == 0 { - deadline = time.Now().AddDate(100, 0, 0) - } else { - deadline, err = time.Parse("01/02/2006", form.Deadline) - if err != nil { - ctx.Handle(500, "issue.NewMilestonePost(time.Parse)", err) - return - } + form.Deadline = "12/31/9999" + } + deadline, err = time.Parse("01/02/2006", form.Deadline) + if err != nil { + ctx.Handle(500, "issue.NewMilestonePost(time.Parse)", err) + return } - m := &models.Milestone{ + mile := &models.Milestone{ RepoId: ctx.Repo.Repository.Id, Index: int64(ctx.Repo.Repository.NumMilestones) + 1, Name: form.Title, Content: form.Content, Deadline: deadline, } - if err = models.NewMilestone(m); err != nil { + if err = models.NewMilestone(mile); err != nil { ctx.Handle(500, "issue.NewMilestonePost(NewMilestone)", err) return } @@ -548,10 +547,73 @@ func NewMilestonePost(ctx *middleware.Context, form auth.CreateMilestoneForm) { ctx.Redirect(ctx.Repo.RepoLink + "/issues/milestones") } -func UpdateMilestones(ctx *middleware.Context) { - ctx.Data["Title"] = "Update Milestones" +func UpdateMilestone(ctx *middleware.Context, params martini.Params) { + ctx.Data["Title"] = "Update Milestone" ctx.Data["IsRepoToolbarIssues"] = true ctx.Data["IsRepoToolbarIssuesList"] = true + idx, _ := base.StrTo(params["index"]).Int64() + if idx == 0 { + ctx.Handle(404, "issue.UpdateMilestone", nil) + return + } + + mile, err := models.GetMilestoneByIndex(ctx.Repo.Repository.Id, idx) + if err != nil { + if err == models.ErrMilestoneNotExist { + ctx.Handle(404, "issue.UpdateMilestone(GetMilestoneByIndex)", err) + } else { + ctx.Handle(500, "issue.UpdateMilestone(GetMilestoneByIndex)", err) + } + return + } + mile.DeadlineString = mile.Deadline.UTC().Format("01/02/2006") + if mile.DeadlineString == "12/31/9999" { + mile.DeadlineString = "" + } + ctx.Data["Milestone"] = mile + ctx.HTML(200, "issue/milestone_edit") } + +func UpdateMilestonePost(ctx *middleware.Context, params martini.Params, form auth.CreateMilestoneForm) { + ctx.Data["Title"] = "Update Milestone" + ctx.Data["IsRepoToolbarIssues"] = true + ctx.Data["IsRepoToolbarIssuesList"] = true + + idx, _ := base.StrTo(params["index"]).Int64() + if idx == 0 { + ctx.Handle(404, "issue.UpdateMilestone", nil) + return + } + + mile, err := models.GetMilestoneByIndex(ctx.Repo.Repository.Id, idx) + if err != nil { + if err == models.ErrMilestoneNotExist { + ctx.Handle(404, "issue.UpdateMilestone(GetMilestoneByIndex)", err) + } else { + ctx.Handle(500, "issue.UpdateMilestone(GetMilestoneByIndex)", err) + } + return + } + + var deadline time.Time + if len(form.Deadline) == 0 { + form.Deadline = "12/31/9999" + } + deadline, err = time.Parse("01/02/2006", form.Deadline) + if err != nil { + ctx.Handle(500, "issue.UpdateMilestonePost(time.Parse)", err) + return + } + + mile.Name = form.Title + mile.Content = form.Content + mile.Deadline = deadline + if err = models.UpdateMilestone(mile); err != nil { + ctx.Handle(500, "issue.UpdateMilestonePost(UpdateMilestone)", err) + return + } + + ctx.Redirect(ctx.Repo.RepoLink + "/issues/milestones") +} diff --git a/templates/issue/milestone.tmpl b/templates/issue/milestone.tmpl index 3c2cf93a6..e15d96414 100644 --- a/templates/issue/milestone.tmpl +++ b/templates/issue/milestone.tmpl @@ -22,7 +22,7 @@ {{.NumClosedIssues}} {{.NumOpenIssues}}

- + Edit diff --git a/templates/issue/milestone_edit.tmpl b/templates/issue/milestone_edit.tmpl index 2db55ae91..8f1a05e01 100644 --- a/templates/issue/milestone_edit.tmpl +++ b/templates/issue/milestone_edit.tmpl @@ -4,7 +4,7 @@ {{template "repo/toolbar" .}}

-
+ {{.CsrfTokenHtml}} {{template "base/alert" .}}
@@ -12,7 +12,7 @@
- +
@@ -25,7 +25,7 @@
- +
loading...
@@ -34,15 +34,14 @@
- +

Milestone Due Date

-
- +