Exemplo n.º 1
0
    async def get_collection(self, id: str, **kwargs) -> ORJSONResponse:
        """Get collection by id.

        Called with `GET /collections/{collectionId}`.

        Args:
            id: Id of the collection.

        Returns:
            Collection.
        """
        request = kwargs["request"]
        pool = kwargs["request"].app.state.readpool
        async with pool.acquire() as conn:
            q, p = render(
                """
                SELECT * FROM get_collection(:id::text);
                """,
                id=id,
            )
            collection = await conn.fetchval(q, *p)
        if collection is None:
            raise NotFoundError
        links = await CollectionLinks(collection_id=id,
                                      request=request).get_links()
        collection["links"] = links
        return ORJSONResponse(
            Collection.construct(**collection).dict(exclude_none=True))
Exemplo n.º 2
0
    async def _all_collections_func(self, **kwargs) -> List[Dict]:
        """Read all collections from the database."""
        request = kwargs["request"]
        pool = request.app.state.readpool

        async with pool.acquire() as conn:
            collections = await conn.fetchval("""
                SELECT * FROM all_collections();
                """)
        linked_collections = []
        if collections is not None and len(collections) > 0:
            for c in collections:
                coll = Collection.construct(**c)
                coll.links = await CollectionLinks(
                    collection_id=coll.id, request=request).get_links()
                linked_collections.append(coll)
        return linked_collections