-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/inv 122 템플릿 CRUD 메서드 구현 (#38)
* feat: implement list templates api * feat: 템플릿 단건 조회 api 구현 * feat: 템플릿 update api 구현 * feat: 템플릿 삭제 api 구현 * feat: 템플릿 생성 api 구현
- Loading branch information
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
"use server"; | ||
import { eq } from "drizzle-orm"; | ||
import { nanoid } from "nanoid"; | ||
|
||
import { db } from "~/lib/db"; | ||
import { templates } from "~/lib/db/schema/templates"; | ||
|
||
type CreateTemplateParams = { | ||
title: string; | ||
description?: string; | ||
customFields?: Record<string, any>; // JSON data | ||
}; | ||
|
||
type UpdateTemplateParams = { | ||
id: string; | ||
title?: string; | ||
description?: string; | ||
customFields?: Record<string, any>; // JSON data | ||
}; | ||
|
||
export async function getAllTemplates() { | ||
const result = await db.select().from(templates); | ||
return result; | ||
} | ||
|
||
export async function getById(id: string) { | ||
const result = await db.select().from(templates).where(eq(templates.id, id)); | ||
return result[0]; | ||
} | ||
|
||
async function updateTemplate(params: UpdateTemplateParams) { | ||
const { id, ...updates } = params; | ||
|
||
if (!id) { | ||
throw new Error("ID is required to update a template"); | ||
} | ||
|
||
const currentTimestamp = new Date(); | ||
|
||
try { | ||
await db | ||
.update(templates) | ||
.set({ | ||
...updates, | ||
updatedAt: currentTimestamp, | ||
}) | ||
.where(eq(templates.id, id)); | ||
} catch (error) { | ||
console.error("Error updating template:", error); | ||
throw new Error("Could not update template"); | ||
} | ||
} | ||
|
||
async function deleteTemplate(id: string): Promise<void> { | ||
if (!id) { | ||
throw new Error("ID is required to delete a template"); | ||
} | ||
|
||
try { | ||
await db.delete(templates).where(eq(templates.id, id)); | ||
} catch (error) { | ||
console.error("Error deleting template:", error); | ||
throw new Error("Could not delete template"); | ||
} | ||
} | ||
|
||
async function createTemplate(params: CreateTemplateParams): Promise<void> { | ||
const { title, description, customFields } = params; | ||
|
||
const id = nanoid(); | ||
|
||
const currentTimestamp = new Date(); | ||
|
||
try { | ||
await db.insert(templates).values({ | ||
id, | ||
title, | ||
description, | ||
customFields, | ||
createdAt: currentTimestamp, | ||
updatedAt: currentTimestamp, | ||
}); | ||
} catch (error) { | ||
console.error("Error creating template:", error); | ||
throw new Error("Could not create template"); | ||
} | ||
} |