-
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
Showing
10 changed files
with
194 additions
and
29 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
8 changes: 3 additions & 5 deletions
8
...tes/games/[game]/[hack]/+layout.server.ts → src/routes/hacks/[slug]/+layout.server.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,60 @@ | ||
import { toSlug } from '$lib/convert'; | ||
import { error, fail, redirect } from '@sveltejs/kit'; | ||
import { superValidate } from 'sveltekit-superforms/server'; | ||
|
||
import type { Actions, PageServerLoad } from './$types'; | ||
import { formSchema } from './schema'; | ||
|
||
export async function load() { | ||
export const load: PageServerLoad = async ({ locals: { getSession } }) => { | ||
// if not logged in, redirect to login | ||
const session = await getSession(); | ||
if (!session) { | ||
return redirect(303, `/auth?redirect=/new`); | ||
} | ||
|
||
return { | ||
form: await superValidate(formSchema), | ||
}; | ||
} | ||
}; | ||
|
||
export const actions: Actions = { | ||
default: async (event) => { | ||
const form = await superValidate(event, formSchema); | ||
if (!form.valid) { | ||
return fail(400, { | ||
form, | ||
}); | ||
} | ||
|
||
const { supabase } = event.locals; | ||
|
||
// Get the game id | ||
const gameResp = await supabase | ||
.from('games') | ||
.select('id') | ||
.eq('name', form.data.game_name) | ||
.single(); | ||
if (gameResp.error) { | ||
console.error(gameResp.error); | ||
throw error(500, 'Error finding game'); | ||
} | ||
|
||
// Create the hack | ||
const createResp = await supabase | ||
.from('hacks') | ||
.insert({ | ||
game_id: gameResp.data.id, | ||
name: form.data.name, | ||
slug: form.data.slug.length === 0 ? toSlug(form.data.name) : form.data.slug, | ||
}) | ||
.select('slug') | ||
.single(); | ||
if (createResp.error) { | ||
console.error(createResp.error); | ||
throw error(500, 'Error creating hack'); | ||
} | ||
|
||
// Redirect to the hack's page | ||
return redirect(303, `/hacks/${createResp.data.slug}`); | ||
}, | ||
}; |
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
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,96 @@ | ||
create table "public"."hacks" ( | ||
"id" bigint generated by default as identity not null, | ||
"game_id" bigint, | ||
"created_at" timestamp with time zone not null default now(), | ||
"name" text not null, | ||
"short_description" text not null default ''::text, | ||
"markdown_description" text not null default ''::text, | ||
"creator" uuid default auth.uid(), | ||
"slug" text not null, | ||
"public" boolean default false | ||
); | ||
|
||
|
||
alter table "public"."hacks" enable row level security; | ||
|
||
CREATE UNIQUE INDEX hacks_pkey ON public.hacks USING btree (id); | ||
|
||
CREATE UNIQUE INDEX hacks_slug_idx ON public.hacks USING btree (slug); | ||
|
||
alter table "public"."hacks" add constraint "hacks_pkey" PRIMARY KEY using index "hacks_pkey"; | ||
|
||
alter table "public"."hacks" add constraint "hacks_creator_fkey" FOREIGN KEY (creator) REFERENCES auth.users(id) ON UPDATE CASCADE ON DELETE CASCADE not valid; | ||
|
||
alter table "public"."hacks" validate constraint "hacks_creator_fkey"; | ||
|
||
alter table "public"."hacks" add constraint "hacks_game_id_fkey" FOREIGN KEY (game_id) REFERENCES games(id) ON UPDATE CASCADE ON DELETE CASCADE not valid; | ||
|
||
alter table "public"."hacks" validate constraint "hacks_game_id_fkey"; | ||
|
||
grant delete on table "public"."hacks" to "anon"; | ||
|
||
grant insert on table "public"."hacks" to "anon"; | ||
|
||
grant references on table "public"."hacks" to "anon"; | ||
|
||
grant select on table "public"."hacks" to "anon"; | ||
|
||
grant trigger on table "public"."hacks" to "anon"; | ||
|
||
grant truncate on table "public"."hacks" to "anon"; | ||
|
||
grant update on table "public"."hacks" to "anon"; | ||
|
||
grant delete on table "public"."hacks" to "authenticated"; | ||
|
||
grant insert on table "public"."hacks" to "authenticated"; | ||
|
||
grant references on table "public"."hacks" to "authenticated"; | ||
|
||
grant select on table "public"."hacks" to "authenticated"; | ||
|
||
grant trigger on table "public"."hacks" to "authenticated"; | ||
|
||
grant truncate on table "public"."hacks" to "authenticated"; | ||
|
||
grant update on table "public"."hacks" to "authenticated"; | ||
|
||
grant delete on table "public"."hacks" to "service_role"; | ||
|
||
grant insert on table "public"."hacks" to "service_role"; | ||
|
||
grant references on table "public"."hacks" to "service_role"; | ||
|
||
grant select on table "public"."hacks" to "service_role"; | ||
|
||
grant trigger on table "public"."hacks" to "service_role"; | ||
|
||
grant truncate on table "public"."hacks" to "service_role"; | ||
|
||
grant update on table "public"."hacks" to "service_role"; | ||
|
||
create policy "Enable insert for authenticated users only" | ||
on "public"."hacks" | ||
as permissive | ||
for insert | ||
to authenticated | ||
with check (true); | ||
|
||
|
||
create policy "Enable read access for public hacks" | ||
on "public"."hacks" | ||
as permissive | ||
for select | ||
to public | ||
using ((public = true)); | ||
|
||
|
||
create policy "Users can do anything with hacks they created" | ||
on "public"."hacks" | ||
as permissive | ||
for all | ||
to authenticated | ||
using ((auth.uid() = creator)); | ||
|
||
|
||
|
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,5 @@ | ||
alter table "public"."hacks" alter column "game_id" set not null; | ||
|
||
alter table "public"."hacks" alter column "public" set not null; | ||
|
||
|