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

-- SharedLinkRepository.get
select
  "shared_link".*,
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        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"."assetId",
              "asset_exif"."autoStackId",
              "asset_exif"."bitsPerSample",
              "asset_exif"."city",
              "asset_exif"."colorspace",
              "asset_exif"."country",
              "asset_exif"."dateTimeOriginal",
              "asset_exif"."description",
              "asset_exif"."exifImageHeight",
              "asset_exif"."exifImageWidth",
              "asset_exif"."exposureTime",
              "asset_exif"."fileSizeInByte",
              "asset_exif"."fNumber",
              "asset_exif"."focalLength",
              "asset_exif"."fps",
              "asset_exif"."iso",
              "asset_exif"."latitude",
              "asset_exif"."lensModel",
              "asset_exif"."livePhotoCID",
              "asset_exif"."longitude",
              "asset_exif"."make",
              "asset_exif"."model",
              "asset_exif"."modifyDate",
              "asset_exif"."orientation",
              "asset_exif"."profileDescription",
              "asset_exif"."projectionType",
              "asset_exif"."rating",
              "asset_exif"."state",
              "asset_exif"."tags",
              "asset_exif"."timeZone"
            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 agg
  ) as "assets",
  to_json("album") as "album"
from
  "shared_link"
  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"."assetId",
              "asset_exif"."autoStackId",
              "asset_exif"."bitsPerSample",
              "asset_exif"."city",
              "asset_exif"."colorspace",
              "asset_exif"."country",
              "asset_exif"."dateTimeOriginal",
              "asset_exif"."description",
              "asset_exif"."exifImageHeight",
              "asset_exif"."exifImageWidth",
              "asset_exif"."exposureTime",
              "asset_exif"."fileSizeInByte",
              "asset_exif"."fNumber",
              "asset_exif"."focalLength",
              "asset_exif"."fps",
              "asset_exif"."iso",
              "asset_exif"."latitude",
              "asset_exif"."lensModel",
              "asset_exif"."livePhotoCID",
              "asset_exif"."longitude",
              "asset_exif"."make",
              "asset_exif"."model",
              "asset_exif"."modifyDate",
              "asset_exif"."orientation",
              "asset_exif"."profileDescription",
              "asset_exif"."projectionType",
              "asset_exif"."rating",
              "asset_exif"."state",
              "asset_exif"."tags",
              "asset_exif"."timeZone"
            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
          "id",
          "name",
          "email",
          "avatarColor",
          "profileImagePath",
          "profileChangedAt"
        from
          "user"
        where
          exists (
            select
            from
              "album_user"
            where
              "album_user"."role" = 'owner'
              and "album_user"."albumId" = "album"."id"
              and "album_user"."userId" = "user"."id"
          )
          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
  )
order by
  "shared_link"."createdAt" desc

-- SharedLinkRepository.getAll
select
  "shared_link".*,
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "asset".*
        from
          "shared_link_asset"
          inner join "asset" on "asset"."id" = "shared_link_asset"."assetId"
        where
          "shared_link"."id" = "shared_link_asset"."sharedLinkId"
          and "asset"."deletedAt" is null
        order by
          "asset"."fileCreatedAt" asc
        limit
          $1
      ) as agg
  ) as "assets",
  to_json("album") as "album"
from
  "shared_link"
  left join lateral (
    select
      "album".*,
      to_json("owner") as "owner"
    from
      "album"
      inner join lateral (
        select
          "id",
          "name",
          "email",
          "avatarColor",
          "profileImagePath",
          "profileChangedAt"
        from
          "user"
        where
          exists (
            select
            from
              "album_user"
            where
              "album_user"."role" = 'owner'
              and "album_user"."albumId" = "album"."id"
              and "album_user"."userId" = "user"."id"
          )
          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" = $2
  and (
    "shared_link"."type" = $3
    or "album"."id" is not null
  )
  and "shared_link"."albumId" = $4
order by
  "shared_link"."createdAt" desc

-- SharedLinkRepository.getByKey
select
  "shared_link"."id",
  "shared_link"."userId",
  "shared_link"."albumId",
  "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"."albumId",
  "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

-- SharedLinkRepository.getSharedLinks
select
  "shared_link".*,
  coalesce(
    json_agg("assets") filter (
      where
        "assets"."id" is not null
    ),
    '[]'
  ) as "assets"
from
  "shared_link"
  left join "shared_link_asset" on "shared_link_asset"."sharedLinkId" = "shared_link"."id"
  left join lateral (
    select
      "asset".*
    from
      "asset"
      inner join lateral (
        select
          "asset_exif"."assetId",
          "asset_exif"."autoStackId",
          "asset_exif"."bitsPerSample",
          "asset_exif"."city",
          "asset_exif"."colorspace",
          "asset_exif"."country",
          "asset_exif"."dateTimeOriginal",
          "asset_exif"."description",
          "asset_exif"."exifImageHeight",
          "asset_exif"."exifImageWidth",
          "asset_exif"."exposureTime",
          "asset_exif"."fileSizeInByte",
          "asset_exif"."fNumber",
          "asset_exif"."focalLength",
          "asset_exif"."fps",
          "asset_exif"."iso",
          "asset_exif"."latitude",
          "asset_exif"."lensModel",
          "asset_exif"."livePhotoCID",
          "asset_exif"."longitude",
          "asset_exif"."make",
          "asset_exif"."model",
          "asset_exif"."modifyDate",
          "asset_exif"."orientation",
          "asset_exif"."profileDescription",
          "asset_exif"."projectionType",
          "asset_exif"."rating",
          "asset_exif"."state",
          "asset_exif"."tags",
          "asset_exif"."timeZone"
        from
          "asset_exif"
        where
          "asset_exif"."assetId" = "asset"."id"
      ) as "exifInfo" on true
    where
      "asset"."id" = "shared_link_asset"."assetId"
  ) as "assets" on true
where
  "shared_link"."id" = $1
group by
  "shared_link"."id"
