Skip to content

Commit

Permalink
Change to upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
leung018 committed May 1, 2024
1 parent d8ceb66 commit 300de14
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/app/components/editor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ describe('QuestionSetEditor', () => {
.setName('Test name')
.appendQuestion()
.build()
questionSetRepo.addQuestionSet(questionSet)
questionSetRepo.upsertQuestionSet(questionSet)

const interactor = new UIServiceInteractor({
questionSetName: 'Test name',
Expand Down
6 changes: 3 additions & 3 deletions src/app/components/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useState } from 'react'
import {
QuestionSetRepo,
LocalStorageQuestionSetRepo,
AddQuestionSetError,
UpsertQuestionSetError,
} from '../../repo/question_set'
import { Question, QuestionSet } from '../../model/question_set'
import { MultipleChoiceBuilder, MultipleChoiceError } from '../../model/mc'
Expand Down Expand Up @@ -162,9 +162,9 @@ function QuestionSetEditor({
})

try {
questionSetRepo.addQuestionSet(questionSet)
questionSetRepo.upsertQuestionSet(questionSet)
} catch (e) {
if (e instanceof AddQuestionSetError) {
if (e instanceof UpsertQuestionSetError) {
if (e.cause.code === 'DUPLICATE_QUESTION_SET_NAME') {
setErrorMessage('Question set with same name already exists')
return
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/home.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ function renderHomePage({
questionSets: readonly QuestionSet[]
}) {
const questionSetRepo = LocalStorageQuestionSetRepo.createNull()
questionSets.forEach((set) => questionSetRepo.addQuestionSet(set))
questionSets.forEach((set) => questionSetRepo.upsertQuestionSet(set))
return render(HomePageUIService.createNull({ questionSetRepo }).getElement())
}
2 changes: 1 addition & 1 deletion src/app/components/quiz.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ function renderMultipleChoicePage({
questionSet: QuestionSet
}) {
const originalQuestionSetRepo = LocalStorageQuestionSetRepo.createNull()
originalQuestionSetRepo.addQuestionSet(questionSet)
originalQuestionSetRepo.upsertQuestionSet(questionSet)

const lastSubmittedQuestionSetRepo = LocalStorageQuestionSetRepo.createNull()

Expand Down
2 changes: 1 addition & 1 deletion src/app/components/quiz.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class MultipleChoiceQuizUIService {
this.originalQuestionSetRepo.getQuestionSetById(this.questionSetId)
}
recordSubmittedQuestionSet={(questionSet) =>
this.lastSubmittedQuestionSetRepo.addQuestionSet(questionSet)
this.lastSubmittedQuestionSetRepo.upsertQuestionSet(questionSet)
}
></MultipleChoiceQuiz>
)
Expand Down
25 changes: 16 additions & 9 deletions src/repo/question_set.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { MultipleChoiceBuilder } from '../model/mc'
import { QuestionSet, QuestionSetBuilderForTest } from '../model/question_set'
import {
LocalStorageQuestionSetRepo,
AddQuestionSetError,
UpsertQuestionSetError,
GetQuestionSetError,
QuestionSetRepo,
} from './question_set'
Expand All @@ -27,7 +27,7 @@ describe('LocalStorageQuestionSetRepo', () => {
})

it('should add question set and get question set by name', () => {
repo.addQuestionSet(questionSet)
repo.upsertQuestionSet(questionSet)
expect(repo.getQuestionSetByName(questionSet.name)).toEqual(questionSet)
})

Expand All @@ -38,16 +38,23 @@ describe('LocalStorageQuestionSetRepo', () => {
)
})

it('should throw error when question set name is taken', () => {
repo.addQuestionSet(questionSet)
expect(() => repo.addQuestionSet(questionSet)).toThrowCustomError(
AddQuestionSetError,
it('should throw error when question set name is taken by other questionSet', () => {
repo.upsertQuestionSet(questionSet)

const anotherQuestionSet = new QuestionSetBuilderForTest()
.setName(questionSet.name)
.build()
expect(() => repo.upsertQuestionSet(anotherQuestionSet)).toThrowCustomError(
UpsertQuestionSetError,
'DUPLICATE_QUESTION_SET_NAME',
)

// should not throw error when just updating the same question set
repo.upsertQuestionSet(questionSet)
})

it('should add question set and get question set by id', () => {
repo.addQuestionSet(questionSet)
repo.upsertQuestionSet(questionSet)
expect(repo.getQuestionSetById(questionSet.id)).toEqual(questionSet)
})

Expand All @@ -60,8 +67,8 @@ describe('LocalStorageQuestionSetRepo', () => {

it('should get all question sets for added', () => {
const questionSet2 = new QuestionSetBuilderForTest().setName('2').build()
repo.addQuestionSet(questionSet)
repo.addQuestionSet(questionSet2)
repo.upsertQuestionSet(questionSet)
repo.upsertQuestionSet(questionSet2)
expect(repo.getQuestionSets()).toEqual([questionSet, questionSet2])
})

Expand Down
16 changes: 8 additions & 8 deletions src/repo/question_set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { LocalStorageOperator } from '../utils/local_storage'

export interface QuestionSetRepo {
/**
* @throws {AddQuestionSetError}
* @throws {UpsertQuestionSetError}
*/
addQuestionSet(questionSet: QuestionSet): void
upsertQuestionSet(questionSet: QuestionSet): void

/**
* @throws {GetQuestionSetError}
Expand All @@ -24,9 +24,9 @@ export interface QuestionSetRepo {
getQuestionSets(): ReadonlyArray<QuestionSet>
}

type AddQuestionSetErrorCode = 'DUPLICATE_QUESTION_SET_NAME'
export class AddQuestionSetError extends CustomBaseError<AddQuestionSetErrorCode> {
constructor(code: AddQuestionSetErrorCode, message?: string) {
type UpsertQuestionSetErrorCode = 'DUPLICATE_QUESTION_SET_NAME'
export class UpsertQuestionSetError extends CustomBaseError<UpsertQuestionSetErrorCode> {
constructor(code: UpsertQuestionSetErrorCode, message?: string) {
super(code, message)
}
}
Expand Down Expand Up @@ -63,13 +63,13 @@ export class LocalStorageQuestionSetRepo implements QuestionSetRepo {

private localStorageOperator: LocalStorageOperator<QuestionSet>

addQuestionSet(questionSet: QuestionSet): void {
upsertQuestionSet(questionSet: QuestionSet): void {
if (
this.localStorageOperator.findOneByFilter(
(q) => q.name === questionSet.name,
(q) => q.name === questionSet.name && q.id !== questionSet.id,
)
) {
throw new AddQuestionSetError(
throw new UpsertQuestionSetError(
'DUPLICATE_QUESTION_SET_NAME',
`QuestionSet with name ${questionSet.name} already exists`,
)
Expand Down

0 comments on commit 300de14

Please sign in to comment.