diff --git a/app/(deploy)/[workspace]/databases/database-new-form.tsx b/app/(deploy)/[workspace]/databases/database-new-form.tsx index 7167860..bd27c8b 100644 --- a/app/(deploy)/[workspace]/databases/database-new-form.tsx +++ b/app/(deploy)/[workspace]/databases/database-new-form.tsx @@ -11,6 +11,7 @@ import { deployDatabase } from '@/lib/deploy/database'; import { cn } from '@/lib/utils'; import { Plus } from 'lucide-react'; import Image from 'next/image'; +import { useRouter } from 'next/navigation'; import { useState } from 'react'; interface DatabaseNewSteps { @@ -27,6 +28,7 @@ interface IDatabaseProvider { } export interface IDatabaseConfig { + workspaceId: string; name: string; provider: IDatabaseProvider; user: { @@ -73,8 +75,12 @@ export default function DatabaseNewForm() { const [open, setOpen] = useState(false); const [currentSteps, setCurrentSteps] = useState(1); - // TODO: Generate all data, but leave the user the choice to modify + const router = useRouter(); + + // TODO: Generate all data randomly, but leave the user the choice to modify + const { id } = useWorkspace(); const defaultDatabaseConfig: IDatabaseConfig = { + workspaceId: id, name: 'my-new-cool-db', provider: databaseProviders[0], user: { @@ -85,8 +91,6 @@ export default function DatabaseNewForm() { const [databaseConfig, setDatabaseConfig] = useState(defaultDatabaseConfig); - const { id } = useWorkspace(); - return ( setOpen((open) => !open)}> @@ -206,9 +210,15 @@ export default function DatabaseNewForm() { */} + + + + ); + } -export default async function Databases() { return (

Databases

- -
-

No database

-

Get started by creating a new database.

-
- {/* */} + {workspace.Database.map((database) => ( +
+ +
+

{database.name}

+

{database.provider}

+
+
+ ))}
diff --git a/lib/deploy/database.ts b/lib/deploy/database.ts index 35977bb..f8fa980 100644 --- a/lib/deploy/database.ts +++ b/lib/deploy/database.ts @@ -1,16 +1,65 @@ "use server"; import { IDatabaseConfig } from "@/app/(deploy)/[workspace]/databases/database-new-form"; +import prisma from "../prisma"; +import { DatabaseProvider } from "@prisma/client"; export async function deployDatabase(config: IDatabaseConfig) { - return fetch("http://127.0.0.1:8080/databases", { + // TODO: Refactor using transactions + const database = await prisma.database.create({ + data: { + name: config.name, + provider: DatabaseProvider.REDIS, + password: config.user.password, + username: config.user.username, + workspaceId: config.workspaceId, + host: "", + port: 0, + } + }); + + try { + const res = await fetch("http://127.0.0.1:8080/databases", { method: "POST", headers: { "Content-Type": "application/json", }, - body: JSON.stringify(config), - }).then((res) => res.json()).catch((err) => { - console.error(err); - }); + body: JSON.stringify({ + workspaceId: config.workspaceId, + id: database.id, + provider: { + id: config.provider.id, + }, + user: { + username: config.user.username, + password: config.user.password, + }, + }), + }); + + const json = await res.json(); + console.log(json) + + if (!res.ok) { + throw new Error(json.message); + } + + await prisma.database.update({ + where: { + id: database.id, + }, + data: { + host: "toset", + port: json.port, + } + }); + } catch(err) { + await prisma.database.delete({ + where: { + id: database.id, + } + }) + throw new Error("Failed to deploy database"); + } } \ No newline at end of file diff --git a/lib/nextauth.ts b/lib/nextauth.ts index 451163b..09f74e4 100644 --- a/lib/nextauth.ts +++ b/lib/nextauth.ts @@ -3,6 +3,7 @@ import NextAuth, { NextAuthOptions, TokenSet, User } from "next-auth" import Github from "next-auth/providers/github" import prisma from "./prisma" import { Adapter } from "next-auth/adapters" +import { createWorkspace } from "./deploy/workspace" export const authOptions: NextAuthOptions = { providers: [ @@ -95,7 +96,7 @@ export const authOptions: NextAuthOptions = { }, events: { createUser: async ({ user }) => { - await prisma.workspace.create({ + const worksapce = await prisma.workspace.create({ data: { name: user.username + "'s Personal", ownerId: user.id,