repo.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "fmt"
  7. "net/http"
  8. "strconv"
  9. "github.com/martini-contrib/render"
  10. "github.com/martini-contrib/sessions"
  11. "github.com/gogits/gogs/models"
  12. "github.com/gogits/gogs/modules/auth"
  13. "github.com/gogits/gogs/modules/base"
  14. )
  15. func Create(req *http.Request, r render.Render, data base.TmplData, session sessions.Session) {
  16. data["Title"] = "Create repository"
  17. if req.Method == "GET" {
  18. r.HTML(200, "repo/create", map[string]interface{}{
  19. "UserName": auth.SignedInName(session),
  20. "UserId": auth.SignedInId(session),
  21. "IsSigned": auth.IsSignedIn(session),
  22. })
  23. return
  24. }
  25. // TODO: access check
  26. id, err := strconv.ParseInt(req.FormValue("userId"), 10, 64)
  27. if err == nil {
  28. var u *models.User
  29. u, err = models.GetUserById(id)
  30. if u == nil {
  31. err = models.ErrUserNotExist
  32. }
  33. if err == nil {
  34. _, err = models.CreateRepository(u, req.FormValue("name"))
  35. }
  36. if err == nil {
  37. r.HTML(200, "repo/created", map[string]interface{}{
  38. "RepoName": u.Name + "/" + req.FormValue("name"),
  39. "IsSigned": auth.IsSignedIn(session),
  40. })
  41. return
  42. }
  43. }
  44. if err != nil {
  45. r.HTML(200, "base/error", map[string]interface{}{
  46. "Error": fmt.Sprintf("%v", err),
  47. "IsSigned": auth.IsSignedIn(session),
  48. })
  49. }
  50. }
  51. func Delete(req *http.Request, r render.Render, session sessions.Session) {
  52. if req.Method == "GET" {
  53. r.HTML(200, "repo/delete", map[string]interface{}{
  54. "Title": "Delete repository",
  55. "IsSigned": auth.IsSignedIn(session),
  56. })
  57. return
  58. }
  59. u := &models.User{}
  60. err := models.DeleteRepository(u, "")
  61. if err != nil {
  62. r.HTML(200, "base/error", map[string]interface{}{
  63. "Error": fmt.Sprintf("%v", err),
  64. "IsSigned": auth.IsSignedIn(session),
  65. })
  66. }
  67. }
  68. func List(req *http.Request, r render.Render, session sessions.Session) {
  69. u := auth.SignedInUser(session)
  70. repos, err := models.GetRepositories(u)
  71. fmt.Println("repos", repos)
  72. if err != nil {
  73. r.HTML(200, "base/error", map[string]interface{}{
  74. "Error": fmt.Sprintf("%v", err),
  75. "IsSigned": auth.IsSignedIn(session),
  76. })
  77. return
  78. }
  79. r.HTML(200, "repo/list", map[string]interface{}{
  80. "Title": "repositories",
  81. "Repos": repos,
  82. "IsSigned": auth.IsSignedIn(session),
  83. })
  84. }