File size: 1,258 Bytes
4f2c36e
 
03138b9
 
4f2c36e
 
17f8f56
6233641
17f8f56
95183d4
80165bc
17f8f56
95183d4
03138b9
95183d4
03138b9
 
6233641
3df25d0
5881efa
 
cca515d
0aedfd1
 
 
 
 
f7561cb
 
 
 
248e594
6028520
 
 
 
 
5033071
6028520
 
 
 
 
 
 
248e594
 
 
f7561cb
4f2c36e
7e19cbb
848e268
 
 
7e19cbb
848e268
 
6407b30
848e268
 
 
 
 
 
4f2c36e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { PrismaClient } from '@prisma/client'

import { isAdmin } from '@/utils/checker/is_admin'

const prisma = new PrismaClient()

export async function GET(request: Request) {
  const { headers } = request
  const { searchParams } = new URL(request.url)

  const userId = searchParams.get('userId') ?? undefined
  const page = searchParams.get('page') ? parseInt(searchParams.get('page') as string) : 0
  
  let is_admin = false
  if (headers.get('Authorization')) {
    is_admin = await isAdmin(headers) as boolean
  }

  const query: any =  {
    orderBy: {
      id: 'desc'
    },
    where: {
      is_visible: {
        equals: true
      }
    },
    take: 15,
    skip: page * 15
  }

  if (userId) {
    query.where = {
      userId: {
        equals: userId
      },
      is_visible: {
        equals: undefined
      }
    }
  } else if (is_admin) {
    query.where = {
      is_visible: {
        equals: undefined
      }
    }
  }

  const collections = await prisma.collection.findMany(query)

  const total = await prisma.collection.count()

  return Response.json(
    {
      collections, 
      pagination: {
        total,
        page,
        total_pages: Math.ceil(total / 15)
      },
      status: 200,
      ok: true
    }
  )
}