Add password for teams

This commit is contained in:
orosmatthew 2023-05-01 13:48:16 -04:00
parent b103cef169
commit 8a886d00b8
4 changed files with 110 additions and 2 deletions

View File

@ -56,6 +56,7 @@ model Team {
name String @unique
Submission Submission[]
contests Contest[] @relation("TeamContestRelation")
password String
}
model Contest {

View File

@ -76,7 +76,7 @@
<div class="list-group">
{#each data.teams as team}
<div class="list-group-item">
<a href={'/admin/teams/' + team.id.toString()} class="list-group-item list-group-item-action">
<div class="row">
<div class="col-6">
{team.name}
@ -92,6 +92,6 @@
{/if}
</div>
</div>
</div>
</a>
{/each}
</div>

View File

@ -0,0 +1,38 @@
import { db } from '$lib/server/prisma';
import { error } from 'console';
import type { Actions, PageServerLoad } from './$types';
import { redirect } from '@sveltejs/kit';
export const load = (async ({ params }) => {
const teamId = parseInt(params.teamId);
if (isNaN(teamId)) {
throw error(400, 'Invalid request');
}
const team = await db.team.findUnique({
where: { id: teamId },
select: { id: true, name: true, password: true }
});
if (!team) {
throw redirect(302, '/admin/teams');
}
return { team: team };
}) satisfies PageServerLoad;
export const actions = {
password: async ({ request, params }) => {
const data = await request.formData();
const newPass = data.get('password');
if (!newPass) {
return { success: false };
}
try {
await db.team.update({
where: { id: parseInt(params.teamId) },
data: { password: newPass.toString() }
});
} catch {
return { success: false };
}
return { success: true };
}
} satisfies Actions;

View File

@ -0,0 +1,69 @@
<script lang="ts">
import { enhance } from '$app/forms';
import type { Actions, PageData } from './$types';
export let data: PageData;
export let form: Actions;
let changingPassword = false;
$: if (form && form.success) {
changingPassword = false;
}
</script>
<svelte:head>
<title>Team</title>
</svelte:head>
<h1 style="text-align:center" class="mb-4">{data.team.name}</h1>
<a href="/admin/teams" class="mb-3 btn btn-outline-primary">All Teams</a>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Name</th>
<th>Id</th>
<th>Password</th>
</tr>
</thead>
<tbody>
<tr>
<td>{data.team.name}</td>
<td>{data.team.id}</td>
<td>{data.team.password}</td>
</tr>
</tbody>
</table>
{#if form && !form.success}
<div class="alert alert-danger">Invalid entry</div>
{/if}
{#if !changingPassword}
<button
on:click={() => {
changingPassword = true;
}}
type="button"
class="btn btn-warning">Change Password</button
>
{:else}
<form method="POST" action="?/password" use:enhance>
<h4>Change Password</h4>
<input name="password" class="form-control" />
<div class="mt-2 row">
<div class="text-end">
<button
on:click={() => {
changingPassword = false;
}}
type="button"
class="btn btn-outline-secondary">Cancel</button
>
<button type="submit" class="btn btn-success">Change</button>
</div>
</div>
</form>
{/if}