From 9bf1979fa879b18c88e25009e1f23d311d6ae5b1 Mon Sep 17 00:00:00 2001 From: Steven Date: Tue, 19 Sep 2023 08:24:24 +0800 Subject: [PATCH] fix: list resources --- api/v2/resource_service.go | 2 +- store/resource.go | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/api/v2/resource_service.go b/api/v2/resource_service.go index f35afc0b..0f75cbe7 100644 --- a/api/v2/resource_service.go +++ b/api/v2/resource_service.go @@ -35,7 +35,7 @@ func (s *ResourceService) ListResources(ctx context.Context, _ *apiv2pb.ListReso HasRelatedMemo: true, }) if err != nil { - return nil, status.Errorf(codes.Internal, "failed to list tags: %v", err) + return nil, status.Errorf(codes.Internal, "failed to list resources: %v", err) } response := &apiv2pb.ListResourcesResponse{} diff --git a/store/resource.go b/store/resource.go index 7f97ebce..91c2c314 100644 --- a/store/resource.go +++ b/store/resource.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "fmt" + "strconv" "strings" ) @@ -108,7 +109,7 @@ func (s *Store) ListResources(ctx context.Context, find *FindResource) ([]*Resou query := fmt.Sprintf(` SELECT - GROUP_CONCAT(memo_resource.memo_id) as related_memo_id, + GROUP_CONCAT(memo_resource.memo_id) as related_memo_ids, %s FROM resource LEFT JOIN memo_resource ON resource.id = memo_resource.resource_id @@ -132,8 +133,9 @@ func (s *Store) ListResources(ctx context.Context, find *FindResource) ([]*Resou list := make([]*Resource, 0) for rows.Next() { resource := Resource{} + var relatedMemoIDs sql.NullString dests := []any{ - &resource.RelatedMemoID, + &relatedMemoIDs, &resource.ID, &resource.Filename, &resource.ExternalLink, @@ -150,6 +152,18 @@ func (s *Store) ListResources(ctx context.Context, find *FindResource) ([]*Resou if err := rows.Scan(dests...); err != nil { return nil, err } + if relatedMemoIDs.Valid { + relatedMemoIDList := strings.Split(relatedMemoIDs.String, ",") + if len(relatedMemoIDList) > 0 { + // Only take the first related memo ID. + relatedMemoIDInt, err := strconv.Atoi(relatedMemoIDList[0]) + if err != nil { + return nil, err + } + relatedMemoIDInt32 := int32(relatedMemoIDInt) + resource.RelatedMemoID = &relatedMemoIDInt32 + } + } list = append(list, &resource) }