-
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.
- Loading branch information
1 parent
f5e25a6
commit 20d5ab3
Showing
11 changed files
with
349 additions
and
293 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,60 @@ | ||
import * as z from "zod"; | ||
|
||
import { createRouteHandlerSupabaseClient } from "@supabase/auth-helpers-nextjs"; | ||
import { headers, cookies } from "next/headers"; | ||
import getUser from "~/lib/getUser"; | ||
import { Database } from "~/types/supabase"; | ||
import { fromZodError } from "zod-validation-error"; | ||
|
||
const routeContextSchema = z.object({ | ||
params: z.object({ | ||
siteDesignId: z.string(), | ||
}), | ||
}); | ||
|
||
const patchRequestSchema = z.object({ | ||
gradientId: z.string(), | ||
backgroundType: z.string(), | ||
solid: z.string(), | ||
}); | ||
|
||
export async function PATCH( | ||
req: Request, | ||
context: z.infer<typeof routeContextSchema> | ||
) { | ||
try { | ||
// Validate route params. | ||
const { params } = routeContextSchema.parse(context); | ||
|
||
// Get the request body and validate it. | ||
const json = await req.json(); | ||
const { gradientId, backgroundType, solid } = | ||
patchRequestSchema.parse(json); | ||
|
||
// Update the post. | ||
const supabase = createRouteHandlerSupabaseClient<Database>({ | ||
headers, | ||
cookies, | ||
}); | ||
const { error: updateError } = await supabase | ||
.from("site_design") | ||
.update({ | ||
gradient_id: gradientId, | ||
background_type: backgroundType, | ||
solid, | ||
}) | ||
.eq("id", parseInt(params.siteDesignId)); | ||
|
||
if (updateError) { | ||
console.error(updateError); | ||
return new Response(null, { status: 500 }); | ||
} | ||
return new Response(null, { status: 200 }); | ||
} catch (error) { | ||
if (error instanceof z.ZodError) { | ||
return new Response(fromZodError(error).message, { status: 422 }); | ||
} | ||
|
||
return new Response(null, { 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
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
Oops, something went wrong.