You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.0 KiB

API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
  1. // Copyright 2016 The Gitea 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 models
  5. // Star represents a starred repo by an user.
  6. type Star struct {
  7. ID int64 `xorm:"pk autoincr"`
  8. UID int64 `xorm:"UNIQUE(s)"`
  9. RepoID int64 `xorm:"UNIQUE(s)"`
  10. }
  11. // StarRepo or unstar repository.
  12. func StarRepo(userID, repoID int64, star bool) error {
  13. sess := x.NewSession()
  14. defer sess.Close()
  15. if err := sess.Begin(); err != nil {
  16. return err
  17. }
  18. if star {
  19. if isStaring(sess, userID, repoID) {
  20. return nil
  21. }
  22. if _, err := sess.Insert(&Star{UID: userID, RepoID: repoID}); err != nil {
  23. return err
  24. }
  25. if _, err := sess.Exec("UPDATE `repository` SET num_stars = num_stars + 1 WHERE id = ?", repoID); err != nil {
  26. return err
  27. }
  28. if _, err := sess.Exec("UPDATE `user` SET num_stars = num_stars + 1 WHERE id = ?", userID); err != nil {
  29. return err
  30. }
  31. } else {
  32. if !isStaring(sess, userID, repoID) {
  33. return nil
  34. }
  35. if _, err := sess.Delete(&Star{0, userID, repoID}); err != nil {
  36. return err
  37. }
  38. if _, err := sess.Exec("UPDATE `repository` SET num_stars = num_stars - 1 WHERE id = ?", repoID); err != nil {
  39. return err
  40. }
  41. if _, err := sess.Exec("UPDATE `user` SET num_stars = num_stars - 1 WHERE id = ?", userID); err != nil {
  42. return err
  43. }
  44. }
  45. return sess.Commit()
  46. }
  47. // IsStaring checks if user has starred given repository.
  48. func IsStaring(userID, repoID int64) bool {
  49. return isStaring(x, userID, repoID)
  50. }
  51. func isStaring(e Engine, userID, repoID int64) bool {
  52. has, _ := e.Get(&Star{0, userID, repoID})
  53. return has
  54. }
  55. // GetStargazers returns the users that starred the repo.
  56. func (repo *Repository) GetStargazers(opts ListOptions) ([]*User, error) {
  57. sess := x.Where("star.repo_id = ?", repo.ID).
  58. Join("LEFT", "star", "`user`.id = star.uid")
  59. if opts.Page > 0 {
  60. sess = opts.setSessionPagination(sess)
  61. users := make([]*User, 0, opts.PageSize)
  62. return users, sess.Find(&users)
  63. }
  64. users := make([]*User, 0, 8)
  65. return users, sess.Find(&users)
  66. }
  67. // GetStarredRepos returns the repos the user starred.
  68. func (u *User) GetStarredRepos(private bool, page, pageSize int, orderBy string) (repos RepositoryList, err error) {
  69. if len(orderBy) == 0 {
  70. orderBy = "updated_unix DESC"
  71. }
  72. sess := x.
  73. Join("INNER", "star", "star.repo_id = repository.id").
  74. Where("star.uid = ?", u.ID).
  75. OrderBy(orderBy)
  76. if !private {
  77. sess = sess.And("is_private = ?", false)
  78. }
  79. if page <= 0 {
  80. page = 1
  81. }
  82. sess.Limit(pageSize, (page-1)*pageSize)
  83. repos = make([]*Repository, 0, pageSize)
  84. if err = sess.Find(&repos); err != nil {
  85. return
  86. }
  87. if err = repos.loadAttributes(x); err != nil {
  88. return
  89. }
  90. return
  91. }
  92. // GetStarredRepoCount returns the numbers of repo the user starred.
  93. func (u *User) GetStarredRepoCount(private bool) (int64, error) {
  94. sess := x.
  95. Join("INNER", "star", "star.repo_id = repository.id").
  96. Where("star.uid = ?", u.ID)
  97. if !private {
  98. sess = sess.And("is_private = ?", false)
  99. }
  100. return sess.Count(&Repository{})
  101. }