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) }