summaryrefslogtreecommitdiff
path: root/lib/information/repository.ts
blob: 2a3bc1c020d784cc1e1677858d13d32dbef3e477 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { asc, desc, eq, ilike, and, count, sql } from "drizzle-orm"
import db from "@/db/db"
import { pageInformation, type PageInformation, type NewPageInformation } from "@/db/schema/information"
import type { GetInformationSchema } from "./validations"
import { PgTransaction } from "drizzle-orm/pg-core"

// 최신 패턴: 트랜잭션을 지원하는 인포메이션 조회
export async function selectInformationLists(
  tx: PgTransaction<any, any, any>,
  params: {
    where?: ReturnType<typeof and>
    orderBy?: (ReturnType<typeof asc> | ReturnType<typeof desc>)[]
    offset?: number
    limit?: number
  }
) {
  const { where, orderBy, offset = 0, limit = 10 } = params

  return tx
    .select()
    .from(pageInformation)
    .where(where)
    .orderBy(...(orderBy ?? [desc(pageInformation.createdAt)]))
    .offset(offset)
    .limit(limit)
}

// 최신 패턴: 트랜잭션을 지원하는 카운트 조회
export async function countInformationLists(
  tx: PgTransaction<any, any, any>,
  where?: ReturnType<typeof and>
) {
  const res = await tx
    .select({ count: count() })
    .from(pageInformation)
    .where(where)
  
  return res[0]?.count ?? 0
}

// 기존 패턴 (하위 호환성을 위해 유지)
export async function selectInformation(input: GetInformationSchema) {
  const { page, per_page = 50, sort, pageCode, pageName, isActive, from, to } = input

  const conditions = []

  if (pageCode) {
    conditions.push(ilike(pageInformation.pageCode, `%${pageCode}%`))
  }

  if (pageName) {
    conditions.push(ilike(pageInformation.pageName, `%${pageName}%`))
  }

  if (isActive !== null) {
    conditions.push(eq(pageInformation.isActive, isActive))
  }

  if (from) {
    conditions.push(sql`${pageInformation.createdAt} >= ${from}`)
  }

  if (to) {
    conditions.push(sql`${pageInformation.createdAt} <= ${to}`)
  }

  const offset = (page - 1) * per_page

  // 정렬 설정
  let orderBy = desc(pageInformation.createdAt);
  
  if (sort && Array.isArray(sort) && sort.length > 0) {
    const sortItem = sort[0];
    if (sortItem.id === "createdAt") {
      orderBy = sortItem.desc ? desc(pageInformation.createdAt) : asc(pageInformation.createdAt);
    }
  }

  const whereClause = conditions.length > 0 ? and(...conditions) : undefined

  const data = await db
    .select()
    .from(pageInformation)
    .where(whereClause)
    .orderBy(orderBy)
    .limit(per_page)
    .offset(offset)

  return data
}

// 기존 패턴: 인포메이션 총 개수 조회
export async function countInformation(input: GetInformationSchema) {
  const { pageCode, pageName, isActive, from, to } = input

  const conditions = []

  if (pageCode) {
    conditions.push(ilike(pageInformation.pageCode, `%${pageCode}%`))
  }

  if (pageName) {
    conditions.push(ilike(pageInformation.pageName, `%${pageName}%`))
  }

  if (isActive !== null) {
    conditions.push(eq(pageInformation.isActive, isActive))
  }

  if (from) {
    conditions.push(sql`${pageInformation.createdAt} >= ${from}`)
  }

  if (to) {
    conditions.push(sql`${pageInformation.createdAt} <= ${to}`)
  }

  const whereClause = conditions.length > 0 ? and(...conditions) : undefined

  const result = await db
    .select({ count: count() })
    .from(pageInformation)
    .where(whereClause)

  return result[0]?.count ?? 0
}

// 페이지 코드별 인포메이션 조회 (활성화된 것만)
export async function getInformationByPageCode(pageCode: string): Promise<PageInformation | null> {
  const result = await db
    .select()
    .from(pageInformation)
    .where(and(
      eq(pageInformation.pageCode, pageCode),
      eq(pageInformation.isActive, true)
    ))
    .limit(1)

  return result[0] || null
}

// 인포메이션 생성
export async function insertInformation(data: NewPageInformation): Promise<PageInformation> {
  const result = await db
    .insert(pageInformation)
    .values(data)
    .returning()

  return result[0]
}

// 인포메이션 수정
export async function updateInformation(id: number, data: Partial<NewPageInformation>): Promise<PageInformation | null> {
  const result = await db
    .update(pageInformation)
    .set({ ...data, updatedAt: new Date() })
    .where(eq(pageInformation.id, id))
    .returning()

  return result[0] || null
}

// 인포메이션 삭제
export async function deleteInformationById(id: number): Promise<boolean> {
  const result = await db
    .delete(pageInformation)
    .where(eq(pageInformation.id, id))

  return (result.rowCount ?? 0) > 0
}

// 인포메이션 다중 삭제
export async function deleteInformationByIds(ids: number[]): Promise<number> {
  const result = await db
    .delete(pageInformation)
    .where(sql`${pageInformation.id} = ANY(${ids})`)

  return result.rowCount ?? 0
}

// ID로 인포메이션 조회
export async function getInformationById(id: number): Promise<PageInformation | null> {
  const result = await db
    .select()
    .from(pageInformation)
    .where(eq(pageInformation.id, id))
    .limit(1)

  return result[0] || null
}