From 4a8abd595bf209300808bcedacf0f6fd0d99214a Mon Sep 17 00:00:00 2001 From: Daniel Emery Date: Thu, 2 Nov 2023 22:01:48 +0100 Subject: [PATCH] #67 Split models into scoped dto files --- src/index.ts | 2 +- src/persistence/persistence.ts | 3 ++- src/{models.ts => quiz/quiz.dto.ts} | 23 +++++------------------ src/quiz/quiz.gql.ts | 2 +- src/quiz/quiz.service.ts | 2 +- src/user/user.dto.ts | 16 ++++++++++++++++ src/user/user.gql.ts | 2 +- src/user/user.service.ts | 2 +- 8 files changed, 28 insertions(+), 24 deletions(-) rename src/{models.ts => quiz/quiz.dto.ts} (62%) create mode 100644 src/user/user.dto.ts diff --git a/src/index.ts b/src/index.ts index f2544f0..e5a6551 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; @@ -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'; diff --git a/src/persistence/persistence.ts b/src/persistence/persistence.ts index 2b3ea2c..9f7fe99 100644 --- a/src/persistence/persistence.ts +++ b/src/persistence/persistence.ts @@ -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 { data: T[]; diff --git a/src/models.ts b/src/quiz/quiz.dto.ts similarity index 62% rename from src/models.ts rename to src/quiz/quiz.dto.ts index 17cfc3b..72942dc 100644 --- a/src/models.ts +++ b/src/quiz/quiz.dto.ts @@ -1,4 +1,4 @@ -import { QuizImageState, QuizImageType, QuizType, Role } from '@prisma/client'; +import { User } from '../user/user.dto'; export interface QuizCompletion { completedAt: Date; @@ -6,6 +6,8 @@ export interface QuizCompletion { score: number; } +type QuizType = 'BRAINWAVES' | 'SHARK'; + export interface Quiz { id: string; type: QuizType; @@ -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 { @@ -35,19 +37,6 @@ 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: { @@ -55,5 +44,3 @@ export interface CreateQuizResult { link: string; }[]; } - -export type UserSortOption = 'EMAIL_ASC' | 'NAME_ASC' | 'NUMBER_OF_QUIZZES_COMPLETED_WITH_DESC'; diff --git a/src/quiz/quiz.gql.ts b/src/quiz/quiz.gql.ts index 3845e29..ed46444 100644 --- a/src/quiz/quiz.gql.ts +++ b/src/quiz/quiz.gql.ts @@ -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, diff --git a/src/quiz/quiz.service.ts b/src/quiz/quiz.service.ts index 7b0f3f3..b9291c0 100644 --- a/src/quiz/quiz.service.ts +++ b/src/quiz/quiz.service.ts @@ -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'; diff --git a/src/user/user.dto.ts b/src/user/user.dto.ts new file mode 100644 index 0000000..17e4c22 --- /dev/null +++ b/src/user/user.dto.ts @@ -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'; diff --git a/src/user/user.gql.ts b/src/user/user.gql.ts index d09443b..fea3991 100644 --- a/src/user/user.gql.ts +++ b/src/user/user.gql.ts @@ -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, diff --git a/src/user/user.service.ts b/src/user/user.service.ts index 4f867d1..4835f04 100644 --- a/src/user/user.service.ts +++ b/src/user/user.service.ts @@ -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({