Skip to content

Commit

Permalink
Merge pull request #167 from Project-Juniors-Club/bryann/sorting-game-BE
Browse files Browse the repository at this point in the history
Sorting Game
  • Loading branch information
sltsheryl authored Dec 7, 2023
2 parents 0662af5 + 4a6931a commit 5bcb285
Show file tree
Hide file tree
Showing 10 changed files with 327 additions and 416 deletions.
1 change: 0 additions & 1 deletion components/sorting-game-editor/Creator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ type EditorSortingGameBucketItem = {
export type EditorSerializedSortingGame = Omit<SortingGame, 'buckets'> & {
text: string;
buckets: EditorSortingGameBucket[];
duration: number;
};

type SortingGameCreatorProp = {
Expand Down
36 changes: 18 additions & 18 deletions lib/server/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { EditorPageFormValues } from '../../pages/courses/staff/editor/content/p
import prisma from '../prisma';
import { findUniqueMatchingGame } from './matchingGame';
import { findUniqueQuiz } from './quiz';
import { findUniqueSorting } from './sorting';
import { findQuiz } from './quiz';
import { findUniqueSortingGame } from './sortingGame';
import { EditorSerializedSortingGame } from '../../components/sorting-game-editor/Creator';

const getEditorQuizGame = async (gameId: string): Promise<{ questions: EditorSerializedQuizQuestion[] }> => {
Expand Down Expand Up @@ -50,33 +50,34 @@ const getEditorQuizGame = async (gameId: string): Promise<{ questions: EditorSer
};
};

const getEditorMatchingGame = async (gameId: string): Promise<EditorSerializedMatchingGame> => {
const matchingGame = await findUniqueMatchingGame(gameId);
return {
duration: matchingGame.duration,
images: matchingGame.images.map(image => image.image),
};
};

const getEditorSortingGame = async (gameId: string): Promise<EditorSerializedSortingGame> => {
const sortingGame = await findUniqueSortingGame(gameId);
const sortingGame = await findUniqueSorting(gameId);
return {
text: '',
duration: sortingGame.duration,
buckets: sortingGame.buckets.map(bucket => {
text: sortingGame.description,
buckets: sortingGame.sortingGameBuckets.map(bucket => {
return {
name: bucket.name,
bucketItems: bucket.bucketItems.map(bucketItem => {
name: '',
bucketItems: bucket.sortingGameObjects.map(object => {
return {
text: bucketItem.text,
image: bucketItem.image ? { assetId: bucketItem.image.imageId } : undefined,
objectType: object.objectType,
text: object.text,
image: object.image,
};
}),
};
}),
};
};

const getEditorMatchingGame = async (gameId: string): Promise<EditorSerializedMatchingGame> => {
const matchingGame = await findUniqueMatchingGame(gameId);
return {
duration: matchingGame.duration,
images: matchingGame.images.map(image => image.image),
};
};


// fill in here with whatever value is needed for the SSR form data
const getPageEditorFormValue = async (id: string): Promise<EditorPageFormValues> => {
const page = await prisma.page.findUnique({
Expand Down Expand Up @@ -145,7 +146,6 @@ const getPageEditorFormValue = async (id: string): Promise<EditorPageFormValues>
? await getEditorSortingGame(page.asset.game.assetId)
: {
text: '',
duration: 0,
buckets: [],
},

Expand Down
92 changes: 92 additions & 0 deletions lib/server/sorting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { Prisma, GameType, AssetType, SortingGame, SortingGameObjectType, Image } from '@prisma/client';
import prisma from '../prisma';

export type SerializedSortingGameObject = {
objectType: SortingGameObjectType;
text: string | null;
image: Image | null;
};

export type SerializedBucket = {
description: string;
sortingGameObjects: SerializedSortingGameObject[];
};

export const createSorting = async (description: string, sortingGameBuckets: SerializedBucket[]) => {
return (await prisma.sortingGame.create({
data: {
game: {
create: {
type: GameType.sortingGame,
asset: {
create: {
assetType: AssetType.game,
},
},
},
},
description,
sortingGameBuckets: {
create: sortingGameBuckets.map(bucket => ({
description: bucket.description,
sortingGameObjects: {
create: bucket.sortingGameObjects.map(object => ({
objectType: object.objectType,
text: object.text,
image: object.image && {
connect: {
assetId: object.image.assetId,
},
},
})),
},
})),
},
},
})) as SortingGame;
};

export const findSorting = async (where?: Partial<Prisma.SortingGameWhereInput>, select?: Prisma.SortingGameSelect) => {
return (await prisma.sortingGame.findMany({
where,
select,
})) as SortingGame[];
};

export const findUniqueSorting = async (gameId: string) => {
return await prisma.sortingGame.findUnique({
where: {
gameId: gameId,
},
include: {
sortingGameBuckets: {
include: {
sortingGameObjects: {
include: {
image: true,
},
},
},
},
},
});
}

export const updateSorting = async (
where: Partial<Prisma.SortingGameWhereUniqueInput>,
data: Prisma.SortingGameUpdateInput,
select?: Prisma.SortingGameSelect,
) => {
return (await prisma.sortingGame.update({
where,
data,
select,
})) as SortingGame;
};

export const deleteSorting = async (where: Partial<Prisma.SortingGameWhereUniqueInput>, select?: Prisma.SortingGameSelect) => {
return (await prisma.sortingGame.delete({
where,
select,
})) as SortingGame;
};
185 changes: 0 additions & 185 deletions lib/server/sortingGame.ts

This file was deleted.

Loading

0 comments on commit 5bcb285

Please sign in to comment.