From eec73862a29ec7ba913fb62240bd94e891b5ddc4 Mon Sep 17 00:00:00 2001 From: Siddhartha Basu Date: Thu, 15 Aug 2024 14:39:57 -0500 Subject: [PATCH] feat(service.go): add ListContents method to ContentService for content retrieval based on filters The new `ListContents` method in `ContentService` allows fetching a list of content entries based on provided filters and pagination parameters. This method supports dynamic query generation based on the `ListParameters` input, which includes a limit and a filter string. --- internal/app/service/service.go | 53 ++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/internal/app/service/service.go b/internal/app/service/service.go index 4a8c3bd..c9e2ec1 100644 --- a/internal/app/service/service.go +++ b/internal/app/service/service.go @@ -132,7 +132,8 @@ func (srv *ContentService) buildContent( UpdatedAt: aphgrpc.TimestampProto(mcont.UpdatedOn), Content: mcont.Content, }, - }} + }, + } } func (srv *ContentService) StoreContent( @@ -190,6 +191,56 @@ func (srv *ContentService) DeleteContent( return &empty.Empty{}, nil } +func (srv *ContentService) ListContents( + ctx context.Context, + req *content.ListParameters, +) (*content.ContentCollection, error) { + limit := int64(10) + if req.Limit > 0 { + limit = req.Limit + } + astmt, err := filterStrToQuery(req.Filter) + if err != nil { + return nil, aphgrpc.HandleInvalidParamError(ctx, err) + } + cntModel, err := srv.repo.ListContents(req.Cursor, limit, astmt) + if err != nil { + if repository.IsContentListNotFound(err) { + return nil, aphgrpc.HandleNotFoundError(ctx, err) + } + } + cntDataSlice := make([]*content.ContentCollection_Data, 0) + for _, cntD := range cntModel { + cntDataSlice = append(cntDataSlice, &content.ContentCollection_Data{ + Id: cntD.Key, + Attributes: &content.ContentAttributes{ + Name: cntD.Name, + Namespace: cntD.Namespace, + Slug: cntD.Slug, + CreatedBy: cntD.CreatedBy, + UpdatedBy: cntD.UpdatedBy, + Content: cntD.Content, + CreatedAt: aphgrpc.TimestampProto(cntD.CreatedOn), + UpdatedAt: aphgrpc.TimestampProto(cntD.UpdatedOn), + }, + }) + } + + cnt := &content.ContentCollection{} + if len(cntDataSlice) < int(limit)-2 { // fewer result than limit + cnt.Data = cntDataSlice + cnt.Meta = &content.Meta{Limit: req.Limit} + return cnt, nil + } + cnt.Data = cntDataSlice[:len(cntDataSlice)-1] + cnt.Meta = &content.Meta{ + Limit: limit, + NextCursor: cntModel[len(cntModel)-1].CreatedOn.UnixMilli(), + } + + return cnt, nil +} + func filterStrToQuery(filter string) (string, error) { var empty string if len(filter) == 0 {