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
|
import { asc, desc, eq, and } from "drizzle-orm"
import db from "@/db/db"
import {
pageInformation,
informationAttachments,
type PageInformation,
type NewPageInformation,
type InformationAttachment,
type NewInformationAttachment
} from "@/db/schema/information"
import { users } from "@/db/schema/users"
// 인포메이션 수정
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 getInformationWithAttachments(id: number) {
const information = await db
.select({
id: pageInformation.id,
pagePath: pageInformation.pagePath,
pageName: pageInformation.pageName,
informationContent: pageInformation.informationContent,
isActive: pageInformation.isActive,
createdBy: pageInformation.createdBy,
createdAt: pageInformation.createdAt,
updatedBy: pageInformation.updatedBy,
updatedAt: pageInformation.updatedAt,
updatedByName: users.name,
updatedByEmail: users.email,
})
.from(pageInformation)
.where(eq(pageInformation.id, id))
.limit(1)
if (!information[0]) return null
const attachments = await db
.select()
.from(informationAttachments)
.where(eq(informationAttachments.informationId, id))
.orderBy(asc(informationAttachments.createdAt))
return {
...information[0],
attachments
}
}
// 페이지 경로로 인포메이션과 첨부파일 함께 조회 (사용자 정보 포함)
export async function getInformationByPagePathWithAttachments(pagePath: string) {
const information = await db
.select({
id: pageInformation.id,
pagePath: pageInformation.pagePath,
pageName: pageInformation.pageName,
informationContent: pageInformation.informationContent,
isActive: pageInformation.isActive,
createdBy: pageInformation.createdBy,
createdAt: pageInformation.createdAt,
updatedBy: pageInformation.updatedBy,
updatedAt: pageInformation.updatedAt,
updatedByName: users.name,
updatedByEmail: users.email,
})
.from(pageInformation)
.leftJoin(users, eq(pageInformation.updatedBy, users.id))
.where(and(
eq(pageInformation.pagePath, pagePath),
eq(pageInformation.isActive, true)
))
.limit(1)
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 addInformationAttachment(data: NewInformationAttachment): Promise<InformationAttachment | null> {
const result = await db
.insert(informationAttachments)
.values(data)
.returning()
return result[0] || 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(informationAttachments)
.where(eq(informationAttachments.id, id))
.limit(1)
return result[0] || null
}
|