repos.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 v1
  5. import (
  6. "path"
  7. "github.com/Unknwon/com"
  8. "github.com/gogits/gogs/models"
  9. "github.com/gogits/gogs/modules/middleware"
  10. )
  11. type repo struct {
  12. RepoLink string `json:"repolink"`
  13. }
  14. func SearchRepos(ctx *middleware.Context) {
  15. opt := models.SearchOption{
  16. Keyword: path.Base(ctx.Query("q")),
  17. Uid: com.StrTo(ctx.Query("uid")).MustInt64(),
  18. Limit: com.StrTo(ctx.Query("limit")).MustInt(),
  19. }
  20. if opt.Limit == 0 {
  21. opt.Limit = 10
  22. }
  23. repos, err := models.SearchRepositoryByName(opt)
  24. if err != nil {
  25. ctx.JSON(500, map[string]interface{}{
  26. "ok": false,
  27. "error": err.Error(),
  28. })
  29. return
  30. }
  31. results := make([]*repo, len(repos))
  32. for i := range repos {
  33. if err = repos[i].GetOwner(); err != nil {
  34. ctx.JSON(500, map[string]interface{}{
  35. "ok": false,
  36. "error": err.Error(),
  37. })
  38. return
  39. }
  40. results[i] = &repo{
  41. RepoLink: path.Join(repos[i].Owner.Name, repos[i].Name),
  42. }
  43. }
  44. ctx.Render.JSON(200, map[string]interface{}{
  45. "ok": true,
  46. "data": results,
  47. })
  48. }