diff --git a/gogs.go b/gogs.go index 552a4db27..1e689e83b 100644 --- a/gogs.go +++ b/gogs.go @@ -19,7 +19,7 @@ import ( // Test that go1.1 tag above is included in builds. main.go refers to this definition. const go11tag = true -const APP_VER = "0.0.2.0307" +const APP_VER = "0.0.2.0308" func init() { runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/modules/auth/form.go b/modules/auth/form.go index a90f3d854..b08552755 100644 --- a/modules/auth/form.go +++ b/modules/auth/form.go @@ -17,6 +17,7 @@ import ( "github.com/gogits/gogs/modules/log" ) +// Web form interface. type Form interface { Name(field string) string } diff --git a/modules/auth/repo.go b/modules/auth/repo.go new file mode 100644 index 000000000..6656654f5 --- /dev/null +++ b/modules/auth/repo.go @@ -0,0 +1,53 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package auth + +import ( + "net/http" + "reflect" + + "github.com/codegangsta/martini" + + "github.com/gogits/binding" + + "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/modules/log" +) + +type CreateRepoForm struct { + UserId int64 `form:"userId"` + RepoName string `form:"repo" binding:"Required"` + Visibility string `form:"visibility"` + Description string `form:"desc" binding:"MaxSize(100)"` + Language string `form:"language"` + InitReadme string `form:"initReadme"` +} + +func (f *CreateRepoForm) Name(field string) string { + names := map[string]string{ + "RepoName": "Repository name", + "Description": "Description", + } + return names[field] +} + +func (f *CreateRepoForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) { + if req.Method == "GET" || errors.Count() == 0 { + return + } + + data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) + data["HasError"] = true + AssignForm(f, data) + + if len(errors.Overall) > 0 { + for _, err := range errors.Overall { + log.Error("CreateRepoForm.Validate: %v", err) + } + return + } + + validate(errors, data, f) +} diff --git a/routers/repo/repo.go b/routers/repo/repo.go index e9ad7d1ab..834195d83 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -7,7 +7,6 @@ package repo import ( "fmt" "net/http" - "strconv" "github.com/martini-contrib/render" "github.com/martini-contrib/sessions" @@ -18,7 +17,7 @@ import ( "github.com/gogits/gogs/modules/log" ) -func Create(req *http.Request, r render.Render, data base.TmplData, session sessions.Session) { +func Create(form auth.CreateRepoForm, req *http.Request, r render.Render, data base.TmplData, session sessions.Session) { data["Title"] = "Create repository" if req.Method == "GET" { @@ -26,30 +25,37 @@ func Create(req *http.Request, r render.Render, data base.TmplData, session sess return } + if hasErr, ok := data["HasError"]; ok && hasErr.(bool) { + r.HTML(200, "repo/create", data) + return + } + // TODO: access check - id, err := strconv.ParseInt(req.FormValue("userId"), 10, 64) - if err == nil { - var u *models.User - u, err = models.GetUserById(id) - if u == nil { - err = models.ErrUserNotExist - } - if err == nil { - _, err = models.CreateRepository(u, req.FormValue("name")) - } - if err == nil { - data["RepoName"] = u.Name + "/" + req.FormValue("name") - r.HTML(200, "repo/created", data) + user, err := models.GetUserById(form.UserId) + if err != nil { + if err.Error() == models.ErrUserNotExist.Error() { + data["HasError"] = true + data["ErrorMsg"] = "User does not exist" + auth.AssignForm(form, data) + r.HTML(200, "repo/create", data) return } } - if err != nil { - data["ErrorMsg"] = err - log.Error("repo.Create: %v", err) - r.HTML(200, "base/error", data) + if err == nil { + // TODO: init description and readme + if _, err = models.CreateRepository(user, form.RepoName); err == nil { + data["RepoName"] = user.Name + "/" + form.RepoName + r.HTML(200, "repo/created", data) + fmt.Println("good!!!!") + return + } } + + data["ErrorMsg"] = err + log.Error("repo.Create: %v", err) + r.HTML(200, "base/error", data) } func Delete(req *http.Request, r render.Render, data base.TmplData, session sessions.Session) { diff --git a/routers/user/user.go b/routers/user/user.go index a07d79e45..51a84400a 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -64,8 +64,8 @@ func SignIn(form auth.LogInForm, data base.TmplData, req *http.Request, r render } data["ErrorMsg"] = err - log.Error("user.SignIn: %v", data) - r.HTML(200, "base/error", nil) + log.Error("user.SignIn: %v", err) + r.HTML(200, "base/error", data) return } diff --git a/templates/repo/create.tmpl b/templates/repo/create.tmpl index 4c7f4dd27..e7a363348 100644 --- a/templates/repo/create.tmpl +++ b/templates/repo/create.tmpl @@ -3,6 +3,7 @@