func ListUsers(ctx *context.APIContext) { page := com.StrTo(ctx.Query("page")).MustInt() limit := com.StrTo(ctx.Query("limit")).MustInt() if limit <= 0 { limit = 10 } else if limit > 100 { limit = 100 } users, err := models.Users(page, limit) if err != nil { ctx.Error(500, "", err) } apiUsers := make([]*api.User, 0) for _, u := range users { apiUsers = append(apiUsers, convert.ToUser(u)) } ctx.JSON(200, apiUsers) }
// https://github.com/gigforks/go-gogs-client/wiki/Repositories#search-repositories func Search(ctx *context.APIContext) { opts := &models.SearchRepoOptions{ Keyword: path.Base(ctx.Query("q")), OwnerID: com.StrTo(ctx.Query("uid")).MustInt64(), PageSize: com.StrTo(ctx.Query("limit")).MustInt(), } if opts.PageSize == 0 { opts.PageSize = 10 } // Check visibility. if ctx.IsSigned && opts.OwnerID > 0 { if ctx.User.Id == opts.OwnerID { opts.Private = true } else { u, err := models.GetUserByID(opts.OwnerID) if err != nil { ctx.JSON(500, map[string]interface{}{ "ok": false, "error": err.Error(), }) return } if u.IsOrganization() && u.IsOwnedBy(ctx.User.Id) { opts.Private = true } // FIXME: how about collaborators? } } repos, _, err := models.SearchRepositoryByName(opts) if err != nil { ctx.JSON(500, map[string]interface{}{ "ok": false, "error": err.Error(), }) return } results := make([]*api.Repository, len(repos)) for i := range repos { if err = repos[i].GetOwner(); err != nil { ctx.JSON(500, map[string]interface{}{ "ok": false, "error": err.Error(), }) return } results[i] = &api.Repository{ ID: repos[i].ID, FullName: path.Join(repos[i].Owner.Name, repos[i].Name), } } ctx.JSON(200, map[string]interface{}{ "ok": true, "data": results, }) }
// https://github.com/gigforks/go-gogs-client/wiki/Users#search-users func Search(ctx *context.APIContext) { opts := &models.SearchUserOptions{ Keyword: ctx.Query("q"), Type: models.USER_TYPE_INDIVIDUAL, PageSize: com.StrTo(ctx.Query("limit")).MustInt(), } if opts.PageSize == 0 { opts.PageSize = 10 } users, _, err := models.SearchUserByName(opts) if err != nil { ctx.JSON(500, map[string]interface{}{ "ok": false, "error": err.Error(), }) return } results := make([]*api.User, len(users)) for i := range users { results[i] = &api.User{ ID: users[i].Id, UserName: users[i].Name, AvatarUrl: users[i].AvatarLink(), FullName: users[i].FullName, } if ctx.IsSigned { results[i].Email = users[i].Email } } ctx.JSON(200, map[string]interface{}{ "ok": true, "data": results, }) }