-- NOTE: This file is auto generated by ./sql-generator

-- SharedLinkRepository.get
select
  "shared_link".*,
  coalesce(
    json_agg("a") filter (
      where
        "a"."id" is not null
    ),
    '[]'
  ) as "assets",
  to_json("album") as "album"
from
  "shared_link"
  left join lateral (
    select
      "asset".*,
      to_json("exifInfo") as "exifInfo"
    from
      "shared_link_asset"
      inner join "asset" on "asset"."id" = "shared_link_asset"."assetId"
      inner join lateral (
        select
          "asset_exif".*
        from
          "asset_exif"
        where
          "asset_exif"."assetId" = "asset"."id"
      ) as "exifInfo" on true
    where
      "shared_link"."id" = "shared_link_asset"."sharedLinkId"
      and "asset"."deletedAt" is null
    order by
      "asset"."fileCreatedAt" asc
  ) as "a" on true
  left join lateral (
    select
      "album".*,
      coalesce(
        json_agg(
          "assets"
          order by
            "assets"."fileCreatedAt" asc
        ) filter (
          where
            "assets"."id" is not null
        ),
        '[]'
      ) as "assets",
      to_json("owner") as "owner"
    from
      "album"
      left join "album_asset" on "album_asset"."albumId" = "album"."id"
      left join lateral (
        select
          "asset".*,
          to_json("exifInfo") as "exifInfo"
        from
          "asset"
          inner join lateral (
            select
              "asset_exif".*
            from
              "asset_exif"
            where
              "asset_exif"."assetId" = "asset"."id"
          ) as "exifInfo" on true
        where
          "album_asset"."assetId" = "asset"."id"
          and "asset"."deletedAt" is null
        order by
          "asset"."fileCreatedAt" asc
      ) as "assets" on true
      inner join lateral (
        select
          "user".*
        from
          "user"
        where
          "user"."id" = "album"."ownerId"
          and "user"."deletedAt" is null
      ) as "owner" on true
    where
      "album"."id" = "shared_link"."albumId"
      and "album"."deletedAt" is null
    group by
      "album"."id",
      "owner".*
  ) as "album" on true
where
  "shared_link"."id" = $1
  and "shared_link"."userId" = $2
  and (
    "shared_link"."type" = $3
    or "album"."id" is not null
  )
group by
  "shared_link"."id",
  "album".*
order by
  "shared_link"."createdAt" desc

-- SharedLinkRepository.getAll
select distinct
  on ("shared_link"."createdAt") "shared_link".*,
  "assets"."assets",
  to_json("album") as "album"
from
  "shared_link"
  left join "shared_link_asset" on "shared_link_asset"."sharedLinkId" = "shared_link"."id"
  left join lateral (
    select
      json_agg("asset") as "assets"
    from
      "asset"
    where
      "asset"."id" = "shared_link_asset"."assetId"
      and "asset"."deletedAt" is null
  ) as "assets" on true
  left join lateral (
    select
      "album".*,
      to_json("owner") as "owner"
    from
      "album"
      inner join lateral (
        select
          "user"."id",
          "user"."email",
          "user"."createdAt",
          "user"."profileImagePath",
          "user"."isAdmin",
          "user"."shouldChangePassword",
          "user"."deletedAt",
          "user"."oauthId",
          "user"."updatedAt",
          "user"."storageLabel",
          "user"."name",
          "user"."quotaSizeInBytes",
          "user"."quotaUsageInBytes",
          "user"."status",
          "user"."profileChangedAt"
        from
          "user"
        where
          "user"."id" = "album"."ownerId"
          and "user"."deletedAt" is null
      ) as "owner" on true
    where
      "album"."id" = "shared_link"."albumId"
      and "album"."deletedAt" is null
  ) as "album" on true
where
  "shared_link"."userId" = $1
  and (
    "shared_link"."type" = $2
    or "album"."id" is not null
  )
  and "shared_link"."albumId" = $3
order by
  "shared_link"."createdAt" desc

-- SharedLinkRepository.getByKey
select
  "shared_link"."id",
  "shared_link"."userId",
  "shared_link"."expiresAt",
  "shared_link"."showExif",
  "shared_link"."allowUpload",
  "shared_link"."allowDownload",
  "shared_link"."password",
  (
    select
      to_json(obj)
    from
      (
        select
          "user"."id",
          "user"."name",
          "user"."email",
          "user"."isAdmin",
          "user"."quotaUsageInBytes",
          "user"."quotaSizeInBytes"
        from
          "user"
        where
          "user"."id" = "shared_link"."userId"
      ) as obj
  ) as "user"
from
  "shared_link"
  left join "album" on "album"."id" = "shared_link"."albumId"
where
  "album"."deletedAt" is null
  and (
    "shared_link"."type" = $1
    or "album"."id" is not null
  )
  and "shared_link"."key" = $2

-- SharedLinkRepository.getBySlug
select
  "shared_link"."id",
  "shared_link"."userId",
  "shared_link"."expiresAt",
  "shared_link"."showExif",
  "shared_link"."allowUpload",
  "shared_link"."allowDownload",
  "shared_link"."password",
  (
    select
      to_json(obj)
    from
      (
        select
          "user"."id",
          "user"."name",
          "user"."email",
          "user"."isAdmin",
          "user"."quotaUsageInBytes",
          "user"."quotaSizeInBytes"
        from
          "user"
        where
          "user"."id" = "shared_link"."userId"
      ) as obj
  ) as "user"
from
  "shared_link"
  left join "album" on "album"."id" = "shared_link"."albumId"
where
  "album"."deletedAt" is null
  and (
    "shared_link"."type" = $1
    or "album"."id" is not null
  )
  and "shared_link"."slug" = $2
