Skip to content

Commit

Permalink
add sort in organisation and user admin list api
Browse files Browse the repository at this point in the history
  • Loading branch information
shreeharsha-factly committed Aug 9, 2023
1 parent 190d091 commit 8d1db60
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion server/action/admin/organisation/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ func list(w http.ResponseWriter, r *http.Request) {

orgIDs := r.URL.Query()["id"]
searchQuery := r.URL.Query().Get("q")
sort := r.URL.Query().Get("sort")

if sort != "asc" {
sort = "desc"
}
// if orgIDs is empty, then return all organisations
// else return organisations with given ids
res := &response{}
if len(orgIDs) == 0 {
offset, limit := paginationx.Parse(r.URL.Query())
err := model.DB.Model(&model.Organisation{}).Where("title ILIKE ?", "%"+searchQuery+"%").Count(&res.Total).Offset(offset).Limit(limit).Find(&res.Nodes).Error
err := model.DB.Model(&model.Organisation{}).Where("title ILIKE ?", "%"+searchQuery+"%").Order("created_at " + sort).Count(&res.Total).Offset(offset).Limit(limit).Find(&res.Nodes).Error
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DBError()))
Expand Down
7 changes: 6 additions & 1 deletion server/action/admin/user/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ func list(w http.ResponseWriter, r *http.Request) {

userIDs := r.URL.Query()["id"]
searchQuery := r.URL.Query().Get("q")
sort := r.URL.Query().Get("sort")
res := &response{}

if sort != "asc" {
sort = "desc"
}

if len(userIDs) == 0 {
qs := "%" + searchQuery + "%"
offset, limit := paginationx.Parse(r.URL.Query())
err := model.DB.Model(&model.User{}).Where("display_name ILIKE ? OR email ILIKE ?", qs, qs).Count(&res.Total).Offset(offset).Limit(limit).Find(&res.Nodes).Error
err := model.DB.Model(&model.User{}).Where("display_name ILIKE ? OR email ILIKE ?", qs, qs).Order("created_at " + sort).Count(&res.Total).Offset(offset).Limit(limit).Find(&res.Nodes).Error
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DBError()))
Expand Down

0 comments on commit 8d1db60

Please sign in to comment.