Skip to content

Commit 6797152

Browse files
authored
Merge pull request #31 from arch-spatula/feat/card-model
Feat/card model
2 parents 8b7133f + 9246461 commit 6797152

File tree

3 files changed

+161
-0
lines changed

3 files changed

+161
-0
lines changed

src/models/Cards/Cards.test.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { CardCollection } from '.';
2+
3+
describe('CardCollection', () => {
4+
let cardCollection: CardCollection;
5+
6+
beforeEach(() => {
7+
cardCollection = new CardCollection();
8+
});
9+
10+
it('should add a card to the collection', () => {
11+
const question = 'What is the capital of France?';
12+
const answer = 'Paris';
13+
const count = 0;
14+
const submitDate = new Date();
15+
16+
cardCollection.addCard(question, answer, count, submitDate);
17+
18+
expect(cardCollection.items.length).toBe(1);
19+
expect(cardCollection.items[0].question).toBe(question);
20+
expect(cardCollection.items[0].answer).toBe(answer);
21+
expect(cardCollection.items[0].stackCount).toBe(count);
22+
expect(cardCollection.items[0].submitDate).toStrictEqual(submitDate);
23+
});
24+
25+
it('should delete a card from the collection', () => {
26+
const question = 'What is the capital of France?';
27+
const answer = 'Paris';
28+
const count = 0;
29+
const submitDate = new Date();
30+
const cardId = 'card-123';
31+
32+
cardCollection.addCard(question, answer, count, submitDate, cardId);
33+
34+
cardCollection.deleteCard(cardId);
35+
36+
expect(cardCollection.items.length).toBe(0);
37+
});
38+
39+
it('should edit a card in the collection', () => {
40+
const question = 'What is the capital of France?';
41+
const answer = 'Paris';
42+
const count = 0;
43+
const submitDate = new Date();
44+
const newQuestion = 'What is the capital of Germany?';
45+
const newAnswer = 'Berlin';
46+
const cardId = 'card-123';
47+
48+
cardCollection.addCard(question, answer, count, submitDate, cardId);
49+
50+
cardCollection.editCard(cardId, newQuestion, newAnswer);
51+
52+
expect(cardCollection.items[0].question).toBe(newQuestion);
53+
expect(cardCollection.items[0].answer).toBe(newAnswer);
54+
});
55+
56+
it('should update stackCount when checking answer', () => {
57+
const question = 'What is the capital of France?';
58+
const answer = 'Paris';
59+
const count = 0;
60+
const submitDate = new Date();
61+
const cardId = 'card-123';
62+
63+
cardCollection.addCard(question, answer, count, submitDate, cardId);
64+
65+
// Correct answer
66+
expect(cardCollection.checkAnswer(cardId, 'paris')).toBe(true);
67+
expect(cardCollection.items[0].stackCount).toBe(1);
68+
69+
// Incorrect answer
70+
expect(cardCollection.checkAnswer(cardId, 'berlin')).toBe(false);
71+
expect(cardCollection.items[0].stackCount).toBe(0);
72+
});
73+
});

src/models/Cards/index.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
class CardRecord {
2+
public question: string;
3+
public answer: string;
4+
public stackCount: number;
5+
public submitDate: Date;
6+
private _id?: string;
7+
readonly userId?: string;
8+
9+
constructor(
10+
question: string,
11+
answer: string,
12+
stackCount = -1,
13+
submitDate = new Date(),
14+
id?: string,
15+
userId?: string
16+
) {
17+
this.question = question;
18+
this.answer = answer;
19+
this.stackCount = stackCount;
20+
this.submitDate = submitDate;
21+
this._id = id;
22+
this.userId = userId;
23+
}
24+
25+
get count() {
26+
return this.stackCount;
27+
}
28+
29+
get id() {
30+
return this._id;
31+
}
32+
}
33+
34+
export class CardCollection {
35+
private cardArr: CardRecord[];
36+
constructor(...cards: CardRecord[]) {
37+
this.cardArr = [...cards];
38+
}
39+
40+
addCard(
41+
question: string,
42+
answer: string,
43+
count: number,
44+
submitDate: Date,
45+
id?: string
46+
) {
47+
const card = new CardRecord(question, answer, count, submitDate, id);
48+
this.cardArr.push(card);
49+
}
50+
51+
deleteCard(id: string) {
52+
this.cardArr = this.cardArr.filter((card) => card.id !== id);
53+
}
54+
55+
editCard(id: string, question: string, answer: string) {
56+
this.cardArr.map((card) => {
57+
if (card.id === id) {
58+
if (question) card.question = question;
59+
if (answer) card.answer = answer;
60+
} else return card;
61+
});
62+
}
63+
64+
checkAnswer(id: string, submit: string) {
65+
const [card] = this.cardArr.filter((card) => card.id === id);
66+
const regex = new RegExp(card.answer, 'i');
67+
68+
const isCorrect = regex.test(submit);
69+
isCorrect ? this.#correct(id) : this.#wrong(id);
70+
return isCorrect;
71+
}
72+
73+
#correct(id: string) {
74+
const [card] = this.cardArr.filter((card) => card.id === id);
75+
if (card.stackCount === -1) card.stackCount = 1;
76+
else card.stackCount += 1;
77+
}
78+
79+
#wrong(id: string) {
80+
const [card] = this.cardArr.filter((card) => card.id === id);
81+
card.stackCount = 0;
82+
}
83+
84+
get items() {
85+
return structuredClone(this.cardArr);
86+
}
87+
}

src/models/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './Cards';

0 commit comments

Comments
 (0)