summaryrefslogtreecommitdiff
path: root/lib/information/repository.ts
diff options
context:
space:
mode:
authordujinkim <dujin.kim@dtsolution.co.kr>2025-08-21 06:57:36 +0000
committerdujinkim <dujin.kim@dtsolution.co.kr>2025-08-21 06:57:36 +0000
commit02b1cf005cf3e1df64183d20ba42930eb2767a9f (patch)
treee932c54d5260b0e6fda2b46be2a6ba1c3ee30434 /lib/information/repository.ts
parentd78378ecd7ceede1429359f8058c7a99ac34b1b7 (diff)
(대표님, 최겸) 설계메뉴추가, 작업사항 업데이트
설계메뉴 - 문서관리 설계메뉴 - 벤더 데이터 gtc 메뉴 업데이트 정보시스템 - 메뉴리스트 및 정보 업데이트 파일 라우트 업데이트 엑셀임포트 개선 기본계약 개선 벤더 가입과정 변경 및 개선 벤더 기본정보 - pq 돌체 오류 수정 및 개선 벤더 로그인 과정 이메일 오류 수정
Diffstat (limited to 'lib/information/repository.ts')
-rw-r--r--lib/information/repository.ts192
1 files changed, 74 insertions, 118 deletions
diff --git a/lib/information/repository.ts b/lib/information/repository.ts
index f640a4c6..c7c000b1 100644
--- a/lib/information/repository.ts
+++ b/lib/information/repository.ts
@@ -1,125 +1,52 @@
-import { asc, desc, eq, ilike, and, count, sql } from "drizzle-orm"
+import { asc, desc, eq, and } 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, pagePath, isActive, from, to } = input
-
- const conditions = []
-
- if (pagePath) {
- conditions.push(ilike(pageInformation.pagePath, `%${pagePath}%`))
- }
-
- if (isActive !== null && isActive !== undefined) {
- conditions.push(eq(pageInformation.isActive, isActive))
- }
-
- if (from) {
- conditions.push(sql`${pageInformation.createdAt} >= ${from}`)
- }
+import {
+ pageInformation,
+ informationAttachments,
+ type PageInformation,
+ type NewPageInformation,
+ type InformationAttachment,
+ type NewInformationAttachment
+} from "@/db/schema/information"
- 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);
- }
- }
+// 인포메이션 수정
+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()
- const whereClause = conditions.length > 0 ? and(...conditions) : undefined
+ return result[0] || null
+}
- const data = await db
+// 인포메이션과 첨부파일 함께 조회
+export async function getInformationWithAttachments(id: number) {
+ const information = await db
.select()
.from(pageInformation)
- .where(whereClause)
- .orderBy(orderBy)
- .limit(per_page)
- .offset(offset)
-
- return data
-}
-
-// 기존 패턴: 인포메이션 총 개수 조회
-export async function countInformation(input: GetInformationSchema) {
- const { pagePath, isActive, from, to } = input
-
- const conditions = []
-
- if (pagePath) {
- conditions.push(ilike(pageInformation.pagePath, `%${pagePath}%`))
- }
+ .where(eq(pageInformation.id, id))
+ .limit(1)
- if (isActive !== null && isActive !== undefined) {
- conditions.push(eq(pageInformation.isActive, isActive))
- }
+ if (!information[0]) return null
- if (from) {
- conditions.push(sql`${pageInformation.createdAt} >= ${from}`)
- }
+ const attachments = await db
+ .select()
+ .from(informationAttachments)
+ .where(eq(informationAttachments.informationId, id))
+ .orderBy(asc(informationAttachments.createdAt))
- if (to) {
- conditions.push(sql`${pageInformation.createdAt} <= ${to}`)
+ return {
+ ...information[0],
+ attachments
}
-
- 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 getInformationByPagePath(pagePath: string): Promise<PageInformation | null> {
- const result = await db
+// 페이지 경로로 인포메이션과 첨부파일 함께 조회
+export async function getInformationByPagePathWithAttachments(pagePath: string) {
+ const information = await db
.select()
.from(pageInformation)
.where(and(
@@ -128,26 +55,55 @@ export async function getInformationByPagePath(pagePath: string): Promise<PageIn
))
.limit(1)
- return result[0] || null
+ if (!information[0]) return null
+
+ const attachments = await db
+ .select()
+ .from(informationAttachments)
+ .where(eq(informationAttachments.informationId, information[0].id))
+ .orderBy(asc(informationAttachments.createdAt))
+
+ return {
+ ...information[0],
+ attachments
+ }
}
-// 인포메이션 수정
-export async function updateInformation(id: number, data: Partial<NewPageInformation>): Promise<PageInformation | null> {
+// 첨부파일 추가
+export async function addInformationAttachment(data: NewInformationAttachment): Promise<InformationAttachment | null> {
const result = await db
- .update(pageInformation)
- .set({ ...data, updatedAt: new Date() })
- .where(eq(pageInformation.id, id))
+ .insert(informationAttachments)
+ .values(data)
.returning()
return result[0] || null
}
-// ID로 인포메이션 조회
-export async function getInformationById(id: number): Promise<PageInformation | null> {
+// 첨부파일 삭제
+export async function deleteInformationAttachment(id: number): Promise<boolean> {
+ const result = await db
+ .delete(informationAttachments)
+ .where(eq(informationAttachments.id, id))
+ .returning()
+
+ return result.length > 0
+}
+
+// 인포메이션 ID로 모든 첨부파일 조회
+export async function getAttachmentsByInformationId(informationId: number): Promise<InformationAttachment[]> {
+ return await db
+ .select()
+ .from(informationAttachments)
+ .where(eq(informationAttachments.informationId, informationId))
+ .orderBy(asc(informationAttachments.createdAt))
+}
+
+// 첨부파일 ID로 조회
+export async function getAttachmentById(id: number): Promise<InformationAttachment | null> {
const result = await db
.select()
- .from(pageInformation)
- .where(eq(pageInformation.id, id))
+ .from(informationAttachments)
+ .where(eq(informationAttachments.id, id))
.limit(1)
return result[0] || null