func SearchUsers(ctx *middleware.Context) { opt := models.SearchOption{ Keyword: ctx.Query("q"), Limit: com.StrTo(ctx.Query("limit")).MustInt(), } if opt.Limit == 0 { opt.Limit = 10 } us, err := models.SearchUserByName(opt) if err != nil { ctx.JSON(500, map[string]interface{}{ "ok": false, "error": err.Error(), }) return } results := make([]*sdk.User, len(us)) for i := range us { results[i] = &sdk.User{ UserName: us[i].Name, AvatarUrl: us[i].AvatarLink(), FullName: us[i].FullName, } } ctx.Render.JSON(200, map[string]interface{}{ "ok": true, "data": results, }) }
func RenderUserSearch(ctx *context.Context, opts *UserSearchOptions) { page := ctx.QueryInt("page") if page <= 1 { page = 1 } var ( users []*models.User count int64 err error ) keyword := ctx.Query("q") if len(keyword) == 0 { users, err = opts.Ranger(page, opts.PageSize) if err != nil { ctx.Handle(500, "opts.Ranger", err) return } count = opts.Counter() } else { users, count, err = models.SearchUserByName(&models.SearchUserOptions{ Keyword: keyword, Type: opts.Type, OrderBy: opts.OrderBy, Page: page, PageSize: opts.PageSize, }) if err != nil { ctx.Handle(500, "SearchUserByName", err) return } } ctx.Data["Keyword"] = keyword ctx.Data["Total"] = count ctx.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5) ctx.Data["Users"] = users ctx.HTML(200, opts.TplName) }
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, }) }