Skip to content

Enhance user session management and quiz functionality by integrating… #3

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

Merged
merged 1 commit into from
Apr 9, 2025
Merged
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
8 changes: 7 additions & 1 deletion src/app.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
// See https://svelte.dev/docs/kit/types#app.d.ts

import type { User, Session } from '$lib/auth';

// for information about these interfaces
declare global {
namespace App {
// interface Error {}
// interface Locals {}
interface Locals {
user: User | null;
session: Session | null;
}
// interface PageData {}
// interface PageState {}
// interface Platform {}
Expand Down
7 changes: 7 additions & 0 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@ import { auth } from '$lib/auth';
import { svelteKitHandler } from 'better-auth/svelte-kit';

export async function handle({ event, resolve }) {
const sessionData = await auth.api.getSession(event.request);

if (sessionData) {
event.locals.user = sessionData.user;
event.locals.session = sessionData.session;
}

return svelteKitHandler({ event, resolve, auth });
}
49 changes: 36 additions & 13 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
leaderboard
} from './server/db/schema';

// For SELECT queries (what you get back from the database)
// For SELECT queries - base types from the database
export type Quiz = InferSelectModel<typeof quiz>;
export type User = InferSelectModel<typeof user>;
export type Question = InferSelectModel<typeof question>;
Expand All @@ -18,25 +18,48 @@ export type Attempt = InferSelectModel<typeof attempt>;
export type AttemptAnswer = InferSelectModel<typeof attemptAnswer>;
export type Leaderboard = InferSelectModel<typeof leaderboard>;

// For a quiz with its creator relation included
// Relation types - using Drizzle's type system
export type QuizWithCreator = Quiz & {
creator: User;
creator: Profile;
};

// For a quiz with creator profile info
export type Profile = Pick<User, 'username' | 'image'>;
export type QuizWithProfile = Omit<
Quiz & {
creator: Profile;
},
'creatorId'
>;
export type QuizWithQuestions = Quiz & {
questions: Question[];
};

export type QuizWithQuestionsAndAnswers = Quiz & {
questions: (Question & {
answers: Answer[];
})[];
};

export type QuizWithCreatorAndQuestions = Quiz & {
creator: Profile;
questions: Question[];
};

export type CompleteAttempt = Attempt & {
answers: (AttemptAnswer & {
question: Question;
answer: Answer;
})[];
user: Profile;
quiz: QuizWithQuestionsAndAnswers;
};

// Profile type
export type Profile = Pick<User, 'id' | 'username' | 'image' | 'displayUsername'>;

// Type for pagination options
export type PaginationOptions = {
page?: number;
limit?: number;
};

// Utility type for relationships
export type WithRelations<T, R extends Record<string, unknown>> = T & R;
// Pagination result type
export type PaginatedResult<T> = {
items: T[];
page: number;
totalPages: number;
totalCount: number;
};
130 changes: 0 additions & 130 deletions src/lib/server/db/services/attempt.service.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/lib/server/db/services/index.ts

This file was deleted.

71 changes: 0 additions & 71 deletions src/lib/server/db/services/leaderboard.service.ts

This file was deleted.

Loading