SHARE
TWEET

Untitled

a guest Aug 20th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. func paginatedQuery(query *orm.Query, pagination *person.PaginationRequest, allowedFields ...string) (*orm.Query, error) {
  2.     query.Offset(int(pagination.Offset))
  3.     query.Limit(int(pagination.Limit))
  4.  
  5.     if pagination.SortBy != "" {
  6.         flag := false
  7.         for i := range allowedFields {
  8.             if pagination.SortBy == allowedFields[i] {
  9.                 flag = true
  10.                 break
  11.             }
  12.         }
  13.         if !flag {
  14.             return nil, status.Error(codes.InvalidArgument, "invalid sort_by field (permitted value ["+
  15.                 strings.Join(allowedFields, ",")+"])")
  16.         }
  17.  
  18.         if pagination.Order != "" {
  19.             if pagination.Order != "desc" && pagination.Order != "asc" {
  20.                 return nil, status.Error(codes.InvalidArgument, "invalid order value (permitted value [desc,asc])")
  21.             }
  22.             query.Order(pagination.SortBy + " " + pagination.Order)
  23.         } else {
  24.             query.Order(pagination.SortBy)
  25.         }
  26.     } else {
  27.         if pagination.Order != "" {
  28.             return nil, status.Error(codes.InvalidArgument, "invalid order value (sort_by field not set)")
  29.         }
  30.     }
  31.  
  32.     return query, nil
  33. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top