Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backend-issues #2

Open
wants to merge 4 commits into
base: Tanay
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@
},
"dependencies": {
"@faker-js/faker": "^8.4.1",
"@prisma/client": "^5.16.1",
"@sveltejs/adapter-node": "^1.3.1",
"axios": "^1.7.2",
"countries-list": "^3.1.0",
"downloadjs": "^1.4.7",
"jspdf": "^2.5.1",
"pdf-lib": "^1.17.1",
"prisma": "^5.16.1",
"routing": "^0.0.2",
"svelte-awesome-color-picker": "^3.1.0",
"svelte-tags-input": "^6.0.0",
"tailwindcss": "^3.3.5",
Expand Down
Binary file added prisma/dev.db
Binary file not shown.
24 changes: 24 additions & 0 deletions prisma/migrations/20240703150933_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- CreateTable
CREATE TABLE "User" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"email" TEXT NOT NULL,
"password" TEXT NOT NULL
);

-- CreateTable
CREATE TABLE "Quiz" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"title" TEXT NOT NULL
);

-- CreateTable
CREATE TABLE "Question" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"quizId" INTEGER NOT NULL,
"question" TEXT NOT NULL,
"answer" TEXT NOT NULL,
CONSTRAINT "Question_quizId_fkey" FOREIGN KEY ("quizId") REFERENCES "Quiz" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
3 changes: 3 additions & 0 deletions prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "sqlite"
31 changes: 31 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}

model User {
id Int @id @default(autoincrement())
email String @unique
password String
}

model Quiz {
id Int @id @default(autoincrement())
title String
questions Question[]
}

model Question {
id Int @id @default(autoincrement())
quizId Int
question String
answer String
Quiz Quiz @relation(fields: [quizId], references: [id])
}
3 changes: 3 additions & 0 deletions src/lib/prisma.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
export default prisma;
2 changes: 1 addition & 1 deletion src/routes/(tools)/neumorphism-generator/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import { Label, Input, Range } from 'flowbite-svelte';

import Copy from '$lib/Copy.svelte';
import Copy from '../../../lib/Copy.svelte';

export let data;

Expand Down
67 changes: 64 additions & 3 deletions src/routes/(tools)/online-live-quiz/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,72 @@
<script>

import Quizes from "./quizes/+page.svelte";

const tools = [
{ name: "GIF Tool 1", component: Quizes },
];
let selectedTool = null;
import Register from './register/+page.svelte';
import login from './login/+page.svelte'

let showRegister = false;
let showLogin=false;
function loadRegister() {
showRegister = true;
}
function loadLogin() {
showLogin = true;
}
</script>

{#if !showRegister && !showLogin}
<div class="card gap-16 items-center mx-auto max-w-screen-xl lg:grid lg:grid-cols-2 overflow-hidden rounded-lg">
<!-- Add tool here -->
</div>
<button on:click={loadRegister}> Register </button>
<button on:click={loadLogin}> Login </button>
{#if selectedTool === null}
<div class="grid grid-cols-4 gap-8 p-4">
{#each tools as tool}
<button on:click={() => selectedTool = tool.component} class="homebutton bg-blue-700 hover:bg-blue-800 text-white p-4 rounded-lg">
{tool.name}
</button>
{/each}
</div>
{:else}
<div>


<svelte:component this={selectedTool}/>
<button on:click={() => selectedTool = null} class="backbutton bg-gray-700 hover:bg-gray-800 text-white p-2 rounded-lg mb-4">
Back to Tools
</button>
</div>
{/if}
</div>
{/if}
{#if showRegister}
<svelte:component this={Register} />
{/if}
{#if showLogin}
<svelte:component this={login} />
{/if}
<style>

.card {
padding: 20px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
border-radius: 20px;
margin: 20px;
margin-top: 20px;
}
.grid {
width: 100%;
}
.homebutton {
border-radius: 15px;
}
.backbutton {
border-radius: 15px;
}
</style>
14 changes: 14 additions & 0 deletions src/routes/(tools)/online-live-quiz/leaderboard/+page.server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import prisma from "../../../../lib/prisma";

export async function get() {
const leaderboard = await prisma.user.findMany({
orderBy: {
score: 'desc',
},
});

return {
status: 200,
body: leaderboard,
};
}
17 changes: 17 additions & 0 deletions src/routes/(tools)/online-live-quiz/leaderboard/leaderboard.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script>
import { onMount } from 'svelte';
let leaderboard = [];

onMount(async () => {
const res = await fetch('/api/leaderboard');
leaderboard = await res.json();
});
</script>

<h1>Leaderboard</h1>
{#each leaderboard as entry}
<div>
<p>{entry.email}: {entry.score}</p>
</div>
{/each}

54 changes: 54 additions & 0 deletions src/routes/(tools)/online-live-quiz/login/+page.server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import prisma from '../../../../lib/prisma';

export const actions = {
default: async ({ request }) => {
try {
const data = await request.formData();
const email = data.get('email').trim();
const password = data.get('password').trim();

const user = await prisma.user.findUnique({
where: { email },
});

console.log('User:', user);
console.log('Form Password:', password);
console.log('Stored Password:', user ? user.password : 'No user');

if (user && user.password === password) {
console.log("password is matching");
return {
status: 200,
body: {
type: 'success',
status: 200,
data: JSON.stringify([{ status: 1, body: 2 }, 500, { success: 1, error: 0 }, false, "Login successful"]),
},
};
} else {
console.log("here u are");
return {
status: 401,
body: {
type: 'error',
status: 400,
data: JSON.stringify([{ status: 0, body: 0 }, 400, { success: 0, error: 1 }, true, "Invalid credentials"]),
},
};
}

} catch (error) {
console.error('Error during login:', error);

return {
status: 500,
body: {
type: 'error',
status: 500,
data: JSON.stringify([{ status: 0, body: 0 }, 500, { success: 0, error: 1 }, true, "An error occurred during login"]),
},
};
}
},
};

59 changes: 59 additions & 0 deletions src/routes/(tools)/online-live-quiz/login/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<script>
import { onMount } from 'svelte';
import QuizSelect from '../quiz-select/+page.svelte';

let loginSuccessful = false;
let loginError = false;

async function handleSubmit(event) {
event.preventDefault();

const formData = new FormData(event.target);
const response = await fetch('/online-live-quiz/login', {
method: 'POST',
body: formData,
});

if (response.ok) {
console.log("response is ok");
const result = await response.json();
console.log('Login Result:', result);
console.log(result.success);
try {
const data = JSON.parse(result.data);
if (data[1] === 200) {
console.log("Login successful, status is 200");
loginSuccessful = true;
loginError = false;

} else {
console.log("Invalid login, status is not 200");
loginError = true;
loginSuccessful = false;
}
} catch (error) {
console.error('Error parsing response data:', error);
loginError = true;
loginSuccessful = false;
}
} else {
console.log("response isnt ok");
loginError = true;
loginSuccessful = false;
}
}
</script>

{#if loginSuccessful}
<svelte:component this={QuizSelect} />
{:else}
<h1>Login</h1>
<form on:submit={handleSubmit}>
<input type="email" name="email" placeholder="Email" required />
<input type="password" name="password" placeholder="Password" required />
<button type="submit">Login</button>
</form>
{#if loginError}
<p>Invalid login</p>
{/if}
{/if}
Loading