From f7826d4ed780c29058a0825a9d821523980777de Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 25 Feb 2014 15:11:54 +0800 Subject: [PATCH] create repository from web --- models/repo.go | 17 +++++++++++++++-- models/user.go | 12 ++++++++++++ routers/repo/repo.go | 32 +++++++++++++++++++++++++++----- templates/repo/create.tmpl | 12 ++++++++++++ templates/repo/created.tmpl | 8 ++++++++ 5 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 templates/repo/created.tmpl diff --git a/models/repo.go b/models/repo.go index 6a0071ef9fbf..70e2673d34a8 100644 --- a/models/repo.go +++ b/models/repo.go @@ -46,12 +46,12 @@ func IsRepositoryExist(user *User, reposName string) (bool, error) { // func CreateRepository(user *User, reposName string) (*Repo, error) { f := RepoPath(user.Name, reposName) - _, err := git.InitRepository(f, false) + _, err := git.InitRepository(f, true) if err != nil { return nil, err } - repo := Repo{OwnerId: user.Id, Name: reposName} + repo := Repo{OwnerId: user.Id, Name: reposName, LowerName: strings.ToLower(reposName)} session := orm.NewSession() defer session.Close() session.Begin() @@ -64,6 +64,19 @@ func CreateRepository(user *User, reposName string) (*Repo, error) { session.Rollback() return nil, err } + access := Access{UserName: user.Name, + RepoName: repo.Name, + Mode: AU_WRITABLE, + } + _, err = session.Insert(&access) + if err != nil { + err2 := os.RemoveAll(f) + if err2 != nil { + log.Error("delete repo directory %s/%s failed", user.Name, reposName) + } + session.Rollback() + return nil, err + } _, err = session.Exec("update user set num_repos = num_repos + 1 where id = ?", user.Id) if err != nil { err2 := os.RemoveAll(f) diff --git a/models/user.go b/models/user.go index 4618273b3d3a..c5208f03131f 100644 --- a/models/user.go +++ b/models/user.go @@ -161,6 +161,18 @@ func GetUserByKeyId(keyId int64) (*User, error) { return user, nil } +func GetUserById(id int64) (*User, error) { + user := new(User) + has, err := orm.Id(id).Get(user) + if err != nil { + return nil, err + } + if !has { + return nil, ErrUserNotExist + } + return user, nil +} + // LoginUserPlain validates user by raw user name and password. func LoginUserPlain(name, passwd string) (*User, error) { user := User{Name: name, Passwd: passwd} diff --git a/routers/repo/repo.go b/routers/repo/repo.go index cf79d539b681..11e9e2fb653f 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -7,6 +7,7 @@ package repo import ( "fmt" "net/http" + "strconv" "github.com/martini-contrib/render" @@ -21,11 +22,32 @@ func Create(req *http.Request, r render.Render) { return } - u := &models.User{} - _, err := models.CreateRepository(u, "") - r.HTML(403, "status/403", map[string]interface{}{ - "Title": fmt.Sprintf("%v", err), - }) + // TODO: access check + fmt.Println(req.FormValue("userId"), req.FormValue("name")) + + id, err := strconv.ParseInt(req.FormValue("userId"), 10, 64) + if err == nil { + var user *models.User + user, err = models.GetUserById(id) + if user == nil { + err = models.ErrUserNotExist + } + if err == nil { + _, err = models.CreateRepository(user, req.FormValue("name")) + } + if err == nil { + r.HTML(200, "repo/created", map[string]interface{}{ + "RepoName": user.Name + "/" + req.FormValue("name"), + }) + return + } + } + + if err != nil { + r.HTML(403, "status/403", map[string]interface{}{ + "Title": fmt.Sprintf("%v", err), + }) + } } func Delete(req *http.Request, r render.Render) { diff --git a/templates/repo/create.tmpl b/templates/repo/create.tmpl index 4ec4f6f1e8ad..c93f9386dfc1 100644 --- a/templates/repo/create.tmpl +++ b/templates/repo/create.tmpl @@ -3,7 +3,19 @@
+
+ Owner: lunny +
+
+ repo name: +
+ description(optional): +
+
+ +
+
diff --git a/templates/repo/created.tmpl b/templates/repo/created.tmpl new file mode 100644 index 000000000000..aa47cb3fdcf2 --- /dev/null +++ b/templates/repo/created.tmpl @@ -0,0 +1,8 @@ +{{template "base/head" .}} +{{template "base/navbar" .}} +
+
+ Created successfully! +
+
+{{template "base/footer" .}} \ No newline at end of file