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

-- MemoryRepository.statistics
select
  count(*) as "total"
from
  "memory"
where
  "deletedAt" is null
  and "ownerId" = $1

-- MemoryRepository.statistics (date filter)
select
  count(*) as "total"
from
  "memory"
where
  (
    "showAt" is null
    or "showAt" <= $1
  )
  and (
    "hideAt" is null
    or "hideAt" >= $2
  )
  and "deletedAt" is null
  and "ownerId" = $3

-- MemoryRepository.search
select
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "asset".*
        from
          "asset"
          inner join "memory_asset" on "asset"."id" = "memory_asset"."assetId"
        where
          "memory_asset"."memoriesId" = "memory"."id"
          and "asset"."visibility" = 'timeline'
          and "asset"."deletedAt" is null
          and not exists (
            select
              $1 as "one"
            from
              "asset_face"
              inner join "person" on "person"."id" = "asset_face"."personId"
            where
              "asset_face"."assetId" = "asset"."id"
              and "person"."isHidden" = $2
          )
        order by
          "asset"."fileCreatedAt" asc
      ) as agg
  ) as "assets",
  "memory".*
from
  "memory"
where
  "deletedAt" is null
  and "ownerId" = $3
order by
  "memoryAt" desc

-- MemoryRepository.search (date filter)
select
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "asset".*
        from
          "asset"
          inner join "memory_asset" on "asset"."id" = "memory_asset"."assetId"
        where
          "memory_asset"."memoriesId" = "memory"."id"
          and "asset"."visibility" = 'timeline'
          and "asset"."deletedAt" is null
          and not exists (
            select
              $1 as "one"
            from
              "asset_face"
              inner join "person" on "person"."id" = "asset_face"."personId"
            where
              "asset_face"."assetId" = "asset"."id"
              and "person"."isHidden" = $2
          )
        order by
          "asset"."fileCreatedAt" asc
      ) as agg
  ) as "assets",
  "memory".*
from
  "memory"
where
  (
    "showAt" is null
    or "showAt" <= $3
  )
  and (
    "hideAt" is null
    or "hideAt" >= $4
  )
  and "deletedAt" is null
  and "ownerId" = $5
order by
  "memoryAt" desc

-- MemoryRepository.get
select
  "memory".*,
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "asset".*
        from
          "asset"
          inner join "memory_asset" on "asset"."id" = "memory_asset"."assetId"
        where
          "memory_asset"."memoriesId" = "memory"."id"
          and "asset"."visibility" = 'timeline'
          and "asset"."deletedAt" is null
        order by
          "asset"."fileCreatedAt" asc
      ) as agg
  ) as "assets"
from
  "memory"
where
  "id" = $1
  and "deletedAt" is null

-- MemoryRepository.update
update "memory"
set
  "ownerId" = $1,
  "isSaved" = $2
where
  "id" = $3
select
  "memory".*,
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "asset".*
        from
          "asset"
          inner join "memory_asset" on "asset"."id" = "memory_asset"."assetId"
        where
          "memory_asset"."memoriesId" = "memory"."id"
          and "asset"."visibility" = 'timeline'
          and "asset"."deletedAt" is null
        order by
          "asset"."fileCreatedAt" asc
      ) as agg
  ) as "assets"
from
  "memory"
where
  "id" = $1
  and "deletedAt" is null

-- MemoryRepository.delete
delete from "memory"
where
  "id" = $1

-- MemoryRepository.getAssetIds
select
  "assetId"
from
  "memory_asset"
where
  "memoriesId" = $1
  and "assetId" in ($2)

-- MemoryRepository.addAssetIds
insert into
  "memory_asset" ("memoriesId", "assetId")
values
  ($1, $2)
