Skip to content

Commit

Permalink
#67 Split models into scoped dto files
Browse files Browse the repository at this point in the history
  • Loading branch information
danielemery committed Nov 2, 2023
1 parent 60d4240 commit 4a8abd5
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ApolloServer } from '@apollo/server';
import { expressMiddleware } from '@apollo/server/express4';
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
import { Role } from '@prisma/client';
import bodyParser from 'body-parser';
import cors from 'cors';
import express from 'express';
Expand All @@ -15,6 +14,7 @@ import typeDefs from './gql';
import { persistence } from './persistence/persistence';
import { userQueries } from './user/user.gql';
import { quizMutations, quizQueries } from './quiz/quiz.gql';
import { Role } from './user/user.dto';

const QUIZLORD_VERSION_HEADER = 'X-Quizlord-Api-Version';

Expand Down
3 changes: 2 additions & 1 deletion src/persistence/persistence.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { PrismaClient, Quiz as QuizPersistence, QuizImage as QuizImagePersistence, Role, User } from '@prisma/client';
import { types } from 'pg';
import { v4 as uuidv4 } from 'uuid';
import { QuizFilters, UserSortOption } from '../models';
import { QuizFilters } from '../quiz/quiz.dto';
import { UserSortOption } from '../user/user.dto';

export interface PersistenceResult<T> {
data: T[];
Expand Down
23 changes: 5 additions & 18 deletions src/models.ts → src/quiz/quiz.dto.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { QuizImageState, QuizImageType, QuizType, Role } from '@prisma/client';
import { User } from '../user/user.dto';

export interface QuizCompletion {
completedAt: Date;
completedBy: User[];
score: number;
}

type QuizType = 'BRAINWAVES' | 'SHARK';

export interface Quiz {
id: string;
type: QuizType;
Expand All @@ -17,8 +19,8 @@ export interface Quiz {

export interface QuizImage {
imageLink: string;
state: QuizImageState;
type: QuizImageType;
state: 'PENDING_UPLOAD' | 'READY';
type: 'QUESTION' | 'ANSWER' | 'QUESTION_AND_ANSWER';
}

export interface QuizDetails {
Expand All @@ -35,25 +37,10 @@ export interface QuizFilters {
excludeCompletedBy?: string[];
}

export interface User {
id: string;
email: string;
name?: string;
}

export interface UserDetails {
id: string;
email: string;
name?: string;
roles: Role[];
}

export interface CreateQuizResult {
quiz: Quiz;
uploadLinks: {
fileName: string;
link: string;
}[];
}

export type UserSortOption = 'EMAIL_ASC' | 'NAME_ASC' | 'NUMBER_OF_QUIZZES_COMPLETED_WITH_DESC';
2 changes: 1 addition & 1 deletion src/quiz/quiz.gql.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { QuizImageType, QuizType } from '@prisma/client';

import { QuizlordContext } from '..';
import { Quiz, QuizDetails, QuizCompletion, CreateQuizResult, QuizFilters } from '../models';
import { base64Decode, base64Encode, PagedResult } from '../util/paging-helpers';
import { authorisationService, quizService } from '../service.locator';
import { CreateQuizResult, Quiz, QuizCompletion, QuizDetails, QuizFilters } from './quiz.dto';

async function quizzes(
_: unknown,
Expand Down
2 changes: 1 addition & 1 deletion src/quiz/quiz.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
QuizImageType,
QuizType,
} from '@prisma/client';
import { Quiz, QuizCompletion, QuizFilters, QuizImage } from '../models';
import { Quiz, QuizCompletion, QuizFilters, QuizImage } from './quiz.dto';
import { S3FileService } from '../file/s3.service';
import { persistence } from '../persistence/persistence';

Expand Down
16 changes: 16 additions & 0 deletions src/user/user.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export type Role = 'ADMIN' | 'USER';

export interface User {
id: string;
email: string;
name?: string;
}

export interface UserDetails {
id: string;
email: string;
name?: string;
roles: Role[];
}

export type UserSortOption = 'EMAIL_ASC' | 'NAME_ASC' | 'NUMBER_OF_QUIZZES_COMPLETED_WITH_DESC';
2 changes: 1 addition & 1 deletion src/user/user.gql.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { QuizlordContext } from '..';
import { User, UserDetails, UserSortOption } from '../models';
import { base64Decode, base64Encode, PagedResult } from '../util/paging-helpers';
import { authorisationService, userService } from '../service.locator';
import { User, UserDetails, UserSortOption } from './user.dto';

async function users(
_: unknown,
Expand Down
2 changes: 1 addition & 1 deletion src/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { User as UserPersistence } from '@prisma/client';
import { User, UserSortOption } from '../models';
import { persistence } from '../persistence/persistence';
import { User, UserSortOption } from './user.dto';

export class UserService {
async getUsers({
Expand Down

0 comments on commit 4a8abd5

Please sign in to comment.