|
| 1 | +import { Participant } from '@entities/participant.entity'; |
1 | 2 | import { Poker } from '@entities/poker.entity'; |
| 3 | +import { CreatePokerDto } from '@modules/poker/dto/create-poker.dto'; |
2 | 4 | import { Inject, Injectable } from '@nestjs/common'; |
3 | 5 | import { Model } from 'mongoose'; |
4 | 6 |
|
5 | | -import { GroupRepository } from '../group/repository'; |
6 | | - |
7 | 7 | @Injectable() |
8 | 8 | export class PokerRepository { |
9 | | - constructor( |
10 | | - @Inject('POKER_MODEL') private readonly pokerModel: Model<Poker>, |
11 | | - private readonly groupRepository: GroupRepository, |
12 | | - ) {} |
13 | | - |
14 | | - create(poker: Poker): Promise<Poker> { |
15 | | - const createdPoker = new this.pokerModel(poker); |
16 | | - return createdPoker.save(); |
| 9 | + constructor(@Inject('POKER_MODEL') private readonly pokerModel: Model<Poker>) {} |
| 10 | + |
| 11 | + async create(createPokerDto: CreatePokerDto, participants: Participant[]): Promise<Poker> { |
| 12 | + const pokerDoc = new this.pokerModel({ |
| 13 | + name: createPokerDto.name, |
| 14 | + participants: participants, |
| 15 | + startTime: new Date(), |
| 16 | + endTime: createPokerDto.endDate, |
| 17 | + }); |
| 18 | + |
| 19 | + return Poker.fromDocument(await pokerDoc.save()); |
17 | 20 | } |
18 | 21 |
|
19 | | - getAll(): Promise<Poker[]> { |
20 | | - return this.pokerModel.find(); |
| 22 | + async getAll(): Promise<Poker[]> { |
| 23 | + const pokerDocs = await this.pokerModel.find().exec(); |
| 24 | + if (!pokerDocs) { |
| 25 | + throw new Error('No pokers found'); |
| 26 | + } |
| 27 | + |
| 28 | + return pokerDocs.map((document) => Poker.fromDocument(document)); |
21 | 29 | } |
22 | 30 |
|
23 | 31 | async get(id: string): Promise<Poker> { |
24 | | - const poker = (await this.pokerModel.findById(id)) as Poker; |
25 | | - if (!poker) { |
| 32 | + const pokerDoc = await this.pokerModel.findById(id); |
| 33 | + if (!pokerDoc) { |
26 | 34 | throw new Error(`Poker not found: ${id.toString()}`); |
27 | 35 | } |
28 | 36 |
|
29 | | - return poker; |
| 37 | + return Poker.fromDocument(pokerDoc); |
30 | 38 | } |
31 | 39 |
|
32 | | - validate(id: string, poker: Poker): void { |
33 | | - if (!poker) { |
| 40 | + async update(id: string, poker: Poker): Promise<Poker> { |
| 41 | + const pokerDoc = await this.pokerModel.findByIdAndUpdate(id, poker); |
| 42 | + if (!pokerDoc) { |
34 | 43 | throw new Error(`Poker not found: ${id}`); |
35 | 44 | } |
36 | | - } |
37 | 45 |
|
38 | | - update(id: string, poker: Poker): void { |
39 | | - this.pokerModel.updateOne( |
40 | | - { |
41 | | - _id: id, |
42 | | - }, |
43 | | - poker, |
44 | | - ); |
| 46 | + return Poker.fromDocument(pokerDoc); |
45 | 47 | } |
46 | 48 |
|
47 | 49 | async deleteAll(): Promise<void> { |
|
0 commit comments