-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1904551
commit fb925b8
Showing
11 changed files
with
237 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
40 changes: 40 additions & 0 deletions
40
libs/group-infrastructure/src/lib/typeorm/mappers/group.mapper.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,40 @@ | ||
import { Mapper } from '@toxictoast/azkaban-base-domain'; | ||
import { GroupDAO } from '../../dao'; | ||
import { GroupEntity } from '../entities'; | ||
import { GroupFactory } from '@azkaban/group-domain'; | ||
|
||
export class GroupMapper implements Mapper<GroupDAO, GroupEntity> { | ||
private readonly domainFactory: GroupFactory = new GroupFactory(); | ||
|
||
toEntity(data: GroupDAO): GroupEntity { | ||
const { id, title, slug, active, created_at, updated_at, deleted_at } = | ||
data; | ||
const entity = new GroupEntity(); | ||
entity.id = id; | ||
entity.title = title; | ||
entity.slug = slug; | ||
entity.active = active; | ||
entity.created_at = created_at; | ||
entity.updated_at = updated_at; | ||
entity.deleted_at = deleted_at; | ||
return entity; | ||
} | ||
|
||
toDomain(data: GroupEntity): GroupDAO { | ||
const { id, title, slug, active, created_at, updated_at, deleted_at } = | ||
data; | ||
const aggregate = this.domainFactory.reconstitute({ | ||
id, | ||
title, | ||
slug, | ||
active, | ||
created_at, | ||
updated_at, | ||
deleted_at, | ||
isActive: active && !!deleted_at, | ||
isUpdated: !!updated_at, | ||
isDeleted: !!deleted_at, | ||
}); | ||
return this.domainFactory.constitute(aggregate); | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export {}; | ||
export * from './group.mapper'; |
37 changes: 37 additions & 0 deletions
37
libs/group-infrastructure/src/lib/typeorm/providers/base.provider.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,37 @@ | ||
import { buildDataSource } from '@toxictoast/azkaban-base-helpers'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { EntitySchema, MixedList } from 'typeorm'; | ||
import { GroupEntity } from '../entities'; | ||
|
||
export const datasourceProvider = [ | ||
{ | ||
provide: 'DATA_SOURCE', | ||
useFactory: (configService: ConfigService) => { | ||
const environment = configService.get<string>('NODE_ENV', 'dev'); | ||
const type = configService.get<'postgres' | 'mariadb'>('DATABASE_TYPE'); | ||
const hostname = configService.get<string>('DATABASE_HOST'); | ||
const port = configService.get<number>('DATABASE_PORT'); | ||
const username = configService.get<string>('DATABASE_USERNAME'); | ||
const password = configService.get<string>('DATABASE_PASSWORD'); | ||
const database = configService.get<string>('DATABASE_TABLE'); | ||
// | ||
const entities = [GroupEntity] as unknown as MixedList< | ||
string | EntitySchema | ||
>; | ||
// | ||
return buildDataSource( | ||
{ | ||
environment, | ||
type, | ||
hostname, | ||
port, | ||
username, | ||
password, | ||
database, | ||
}, | ||
entities, | ||
); | ||
}, | ||
inject: [ConfigService], | ||
}, | ||
]; |
11 changes: 11 additions & 0 deletions
11
libs/group-infrastructure/src/lib/typeorm/providers/group.provider.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,11 @@ | ||
import { GroupEntity } from '../entities'; | ||
|
||
export const groupProvider = [ | ||
{ | ||
provide: 'GROUP_REPOSITORY', | ||
useFactory: (dataSource) => { | ||
return dataSource.getRepository(GroupEntity); | ||
}, | ||
inject: ['DATA_SOURCE'], | ||
}, | ||
]; |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export {}; | ||
export * from './base.provider'; | ||
export * from './group.provider'; |
47 changes: 47 additions & 0 deletions
47
libs/group-infrastructure/src/lib/typeorm/repositories/group.repository.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,47 @@ | ||
import { | ||
GroupRepository as DomainRepository, | ||
GroupAnemic, | ||
} from '@azkaban/group-domain'; | ||
import { GroupMapper } from '../mappers'; | ||
import { Repository } from 'typeorm'; | ||
import { GroupEntity } from '../entities'; | ||
|
||
export class GroupRepository implements DomainRepository { | ||
private readonly mapper: GroupMapper = new GroupMapper(); | ||
|
||
constructor(private readonly repository: Repository<GroupEntity>) {} | ||
|
||
async findList(limit?: number, offset?: number): Promise<GroupAnemic[]> { | ||
const entities = await this.repository.find({ | ||
take: limit, | ||
skip: offset, | ||
withDeleted: true, | ||
}); | ||
return entities.map((entity) => this.mapper.toDomain(entity)); | ||
} | ||
|
||
async findById(id: string): Promise<GroupAnemic> { | ||
const entity = await this.repository.findOne({ | ||
withDeleted: true, | ||
where: { id }, | ||
}); | ||
if (entity) { | ||
return this.mapper.toDomain(entity); | ||
} | ||
return null; | ||
} | ||
|
||
async delete(id: string): Promise<GroupAnemic> { | ||
await this.repository.softDelete(id); | ||
return await this.findById(id); | ||
} | ||
|
||
async save(data: GroupAnemic): Promise<GroupAnemic> { | ||
const entity = this.mapper.toEntity(data); | ||
const saved = await this.repository.save(entity); | ||
if (saved) { | ||
return this.mapper.toDomain(saved); | ||
} | ||
return null; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
libs/group-infrastructure/src/lib/typeorm/repositories/index.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 |
---|---|---|
@@ -1 +1 @@ | ||
export {}; | ||
export * from './group.repository'; |
96 changes: 96 additions & 0 deletions
96
libs/group-infrastructure/src/lib/typeorm/services/group.service.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,96 @@ | ||
import { GroupService as DomainService } from '@azkaban/group-domain'; | ||
import { GroupRepository } from '../repositories'; | ||
import { Optional } from '@toxictoast/azkaban-base-types'; | ||
import { GroupDAO } from '../../dao'; | ||
import { BadRequestException, NotFoundException } from '@nestjs/common'; | ||
import { CreateGroupDTO } from '../../dto'; | ||
|
||
export class GroupService { | ||
private readonly domainService: DomainService; | ||
|
||
constructor(private readonly repository: GroupRepository) { | ||
this.domainService = new DomainService(repository); | ||
} | ||
|
||
async getGroupList( | ||
limit?: Optional<number>, | ||
offset?: Optional<number>, | ||
): Promise<Array<GroupDAO>> { | ||
const result = await this.domainService.getGroups(limit, offset); | ||
if (result.isSuccess) { | ||
return result.value; | ||
} else { | ||
return []; | ||
} | ||
} | ||
|
||
async getGroupById(id: string): Promise<GroupDAO> { | ||
const result = await this.domainService.getGroupById(id); | ||
if (result.isSuccess) { | ||
return result.value; | ||
} else { | ||
const errorMessage = result.errorValue; | ||
throw new NotFoundException(errorMessage); | ||
} | ||
} | ||
|
||
async createGroup(data: CreateGroupDTO): Promise<GroupDAO> { | ||
const result = await this.domainService.createGroup(data); | ||
if (result.isSuccess) { | ||
return result.value; | ||
} else { | ||
const errorMessage = result.errorValue; | ||
throw new BadRequestException(errorMessage); | ||
} | ||
} | ||
|
||
async updateTitle(id: string, title: string): Promise<GroupDAO> { | ||
const result = await this.domainService.updateTitle(id, title); | ||
if (result.isSuccess) { | ||
return result.value; | ||
} else { | ||
const errorMessage = result.errorValue; | ||
throw new BadRequestException(errorMessage); | ||
} | ||
} | ||
|
||
async updateSlug(id: string, slug: string): Promise<GroupDAO> { | ||
const result = await this.domainService.updateSlug(id, slug); | ||
if (result.isSuccess) { | ||
return result.value; | ||
} else { | ||
const errorMessage = result.errorValue; | ||
throw new BadRequestException(errorMessage); | ||
} | ||
} | ||
|
||
async updateActive(id: string, active: boolean): Promise<GroupDAO> { | ||
const result = await this.domainService.updateActive(id, active); | ||
if (result.isSuccess) { | ||
return result.value; | ||
} else { | ||
const errorMessage = result.errorValue; | ||
throw new BadRequestException(errorMessage); | ||
} | ||
} | ||
|
||
async deleteGroup(id: string): Promise<GroupDAO> { | ||
const result = await this.domainService.deleteGroup(id); | ||
if (result.isSuccess) { | ||
return result.value; | ||
} else { | ||
const errorMessage = result.errorValue; | ||
throw new NotFoundException(errorMessage); | ||
} | ||
} | ||
|
||
async restoreGroup(id: string): Promise<GroupDAO> { | ||
const result = await this.domainService.restoreGroup(id); | ||
if (result.isSuccess) { | ||
return result.value; | ||
} else { | ||
const errorMessage = result.errorValue; | ||
throw new NotFoundException(errorMessage); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export {}; | ||
export * from './group.service'; |
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