-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from RicardoGEsteves/create-subspreadits
feat: ✨ Title: Enhancement of the Create Community Feature.
- Loading branch information
Showing
3 changed files
with
64 additions
and
2 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
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,52 @@ | ||
import { getAuthSession } from "@/lib/auth"; | ||
import { db } from "@/lib/db"; | ||
import { SubSpreadItValidator } from "@/lib/validators/sub-spreadIt"; | ||
import { z } from "zod"; | ||
|
||
export async function POST(req: Request) { | ||
try { | ||
const session = await getAuthSession(); | ||
|
||
if (!session?.user) { | ||
return new Response("Unauthorized", { status: 401 }); | ||
} | ||
|
||
const body = await req.json(); | ||
const { name } = SubSpreadItValidator.parse(body); | ||
|
||
// check if subSpreadIt already exists | ||
const subSpreadItExists = await db.subSpreadIt.findFirst({ | ||
where: { | ||
name, | ||
}, | ||
}); | ||
|
||
if (subSpreadItExists) { | ||
return new Response("SubSpreadIt already exists", { status: 409 }); | ||
} | ||
|
||
// create subSpreadIt and associate it with the user | ||
const subSpreadIt = await db.subSpreadIt.create({ | ||
data: { | ||
name, | ||
creatorId: session.user.id, | ||
}, | ||
}); | ||
|
||
// creator also has to be subSpreadIt | ||
await db.subscription.create({ | ||
data: { | ||
userId: session.user.id, | ||
subSpreadItId: subSpreadIt.id, | ||
}, | ||
}); | ||
|
||
return new Response(subSpreadIt.name); | ||
} catch (error) { | ||
if (error instanceof z.ZodError) { | ||
return new Response(error.message, { status: 422 }); | ||
} | ||
|
||
return new Response("Could not create subSpreadIt", { status: 500 }); | ||
} | ||
} |
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