-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgift-card.view-state-repository.ts
55 lines (51 loc) · 1.5 KB
/
gift-card.view-state-repository.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { GiftCardSummary } from './gift-card.event-handler';
import { Injectable } from '@nestjs/common';
import { ViewStateRepository } from '@fraktalio/fmodel-ts';
import { GiftCardEvent } from '../api/gift-card.events';
import { Column, Entity, PrimaryGeneratedColumn, Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
/**
* *** ADAPTER LAYER ***
* ___
* Database entity
*/
@Entity()
export class GiftCardSummaryEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column()
initialAmount: number;
@Column()
remainingAmount: number;
@Column({ default: true })
isActive: boolean;
}
/**
* *** ADAPTER LAYER ***
* ___
* Database `view store` implementation
*/
@Injectable()
export class GiftCardViewStateRepository
implements ViewStateRepository<GiftCardEvent, GiftCardSummary | null>
{
constructor(
@InjectRepository(GiftCardSummaryEntity)
private readonly giftCardRepository: Repository<GiftCardSummaryEntity>,
) {}
async fetchState(e: GiftCardEvent): Promise<GiftCardSummary | null> {
return await this.giftCardRepository.findOneBy({ id: e.id });
}
async save(s: GiftCardSummary | null): Promise<GiftCardSummary | null> {
if (s !== null) {
return await this.giftCardRepository.save(s);
}
return s;
}
async findAll(): Promise<GiftCardSummary[]> {
return await this.giftCardRepository.find();
}
async findById(id: string): Promise<GiftCardSummary | null> {
return await this.giftCardRepository.findOneBy({ id: id });
}
}