-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature | Group question answer - cmd handler and repo (#18)
- Loading branch information
1 parent
51f12da
commit 1869295
Showing
8 changed files
with
108 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
backend/src/modules/group-question-answer/core/application/AnswerGroupQuestionRepository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { QuestionAnswer } from './domain/QuestionAnswer'; | ||
|
||
export interface AnswerGroupQuestionRepository { | ||
save(answeredGroupQuestion: QuestionAnswer): Promise<void>; | ||
|
||
findByGroupId(groupId: string): Promise<QuestionAnswer | undefined>; | ||
|
||
findAllByQuestionId(questionId: string): Promise<QuestionAnswer[]>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
backend/src/modules/group-question-answer/core/application/domain/QuestionAnswer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { CurrentTimeProvider } from '../../../../../shared/core/CurrentTimeProvider'; | ||
import { DomainCommandResult } from '../../../../../shared/core/domain/DomainCommandResult'; | ||
import { GroupQuestionWasAnswered } from './event/GroupQuestionWasAnswered'; | ||
|
||
export class QuestionAnswer { | ||
readonly questionId: string; | ||
readonly groupId: string; | ||
readonly answerAuthorId: string; | ||
readonly text: string; | ||
|
||
constructor(props: { questionId: string; groupId: string; answerAuthorId: string; text: string }) { | ||
this.questionId = props.questionId; | ||
this.groupId = props.groupId; | ||
this.answerAuthorId = props.answerAuthorId; | ||
this.text = props.text; | ||
} | ||
} | ||
|
||
export function answerGroupQuestion( | ||
command: { questionId: string; groupId: string; answerAuthorId: string; text: string }, | ||
currentTimeProvider: CurrentTimeProvider, | ||
): DomainCommandResult<QuestionAnswer> { | ||
if (command.text.trim().length <= 0) { | ||
throw new Error('An answer cannot be empty!'); | ||
} | ||
|
||
const groupQuestionWasAnswered = new GroupQuestionWasAnswered({ | ||
occurredAt: currentTimeProvider(), | ||
...command, | ||
}); | ||
|
||
const questionAnswer = new QuestionAnswer({ ...command }); | ||
|
||
return { | ||
state: questionAnswer, | ||
events: [groupQuestionWasAnswered], | ||
}; | ||
} |
14 changes: 14 additions & 0 deletions
14
...c/modules/group-question-answer/core/application/domain/event/GroupQuestionWasAnswered.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export class GroupQuestionWasAnswered { | ||
readonly occurredAt: Date; | ||
readonly questionId: string; | ||
readonly groupId: string; | ||
readonly answerAuthorId: string; | ||
readonly text: string; | ||
|
||
constructor(props: { occurredAt: Date; questionId: string; groupId: string; answerAuthorId: string; text: string }) { | ||
this.questionId = props.questionId; | ||
this.groupId = props.groupId; | ||
this.answerAuthorId = props.answerAuthorId; | ||
this.text = props.text; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...estion-answer/infrastructure/repository/inmemory/InMemoryAnswerGroupQuestionRepository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { AnswerGroupQuestionRepository } from '../../../core/application/AnswerGroupQuestionRepository'; | ||
import { QuestionAnswer } from '../../../core/application/domain/QuestionAnswer'; | ||
|
||
export class InMemoryAnswerGroupQuestionRepository implements AnswerGroupQuestionRepository { | ||
private readonly entities: { [id: string]: QuestionAnswer } = {}; | ||
|
||
findByGroupId(groupId: string): Promise<QuestionAnswer | undefined> { | ||
return Promise.resolve(this.entities[groupId]); | ||
} | ||
|
||
async save(question: QuestionAnswer): Promise<void> { | ||
this.entities[question.questionId] = question; | ||
} | ||
|
||
findAllByQuestionId(questionId: string): Promise<QuestionAnswer[]> { | ||
return Promise.resolve( | ||
Object.keys(this.entities) | ||
.filter((id) => this.entities[id].groupId === questionId) | ||
.map((id) => this.entities[id]), | ||
); | ||
} | ||
} |
1 change: 0 additions & 1 deletion
1
backend/src/modules/group-question-answer/presentation/rest-api/GroupQuestionAnswerRouter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters