github.com/infraboard/keyauth@v0.8.1/apps/application/impl/query.go (about)

     1  package impl
     2  
     3  import (
     4  	"github.com/infraboard/mcube/exception"
     5  	"go.mongodb.org/mongo-driver/bson"
     6  	"go.mongodb.org/mongo-driver/mongo/options"
     7  
     8  	"github.com/infraboard/keyauth/apps/application"
     9  )
    10  
    11  func newPaggingQuery(req *application.QueryApplicationRequest) *queryRequest {
    12  	return &queryRequest{req}
    13  }
    14  
    15  type queryRequest struct {
    16  	*application.QueryApplicationRequest
    17  }
    18  
    19  func (r *queryRequest) FindOptions() *options.FindOptions {
    20  	pageSize := int64(r.Page.PageSize)
    21  	skip := int64(r.Page.PageSize) * int64(r.Page.PageNumber-1)
    22  
    23  	opt := &options.FindOptions{
    24  		Sort:  bson.D{{"create_at", -1}},
    25  		Limit: &pageSize,
    26  		Skip:  &skip,
    27  	}
    28  
    29  	return opt
    30  }
    31  
    32  func (r *queryRequest) FindFilter() bson.M {
    33  	filter := bson.M{}
    34  
    35  	return filter
    36  }
    37  
    38  func newDescribeQuery(req *application.DescribeApplicationRequest) (*describeRequest, error) {
    39  	if err := req.Validate(); err != nil {
    40  		return nil, exception.NewBadRequest(err.Error())
    41  	}
    42  
    43  	return &describeRequest{req}, nil
    44  }
    45  
    46  type describeRequest struct {
    47  	*application.DescribeApplicationRequest
    48  }
    49  
    50  func (r *describeRequest) FindFilter() bson.M {
    51  	filter := bson.M{}
    52  
    53  	if r.Id != "" {
    54  		filter["_id"] = r.Id
    55  	}
    56  	if r.ClientId != "" {
    57  		filter["client_id"] = r.ClientId
    58  	}
    59  
    60  	return filter
    61  }