| 5 | import { type InsertProject, projects, type SelectProject } from "@/db/schema"; |
| 6 | |
| 7 | export async function createProject( |
| 8 | project: InsertProject |
| 9 | ): Promise<string | SelectProject> { |
| 10 | const check = await db.query.projects.findFirst({ |
| 11 | where: and( |
| 12 | or(eq(projects.name, project.name), eq(projects.url, project.url)) |
| 13 | ), |
| 14 | }); |
| 15 | |
| 16 | if (check) { |
| 17 | return "Project already added."; |
| 18 | } |
| 19 | |
| 20 | try { |
| 21 | const [newProject] = await db.insert(projects).values(project).returning(); |
| 22 | return newProject; |
| 23 | } catch { |
| 24 | throw new Error("Failed to create project"); |
| 25 | } |
| 26 | } |