Skip to content

Commit

Permalink
feat: finish group infra
Browse files Browse the repository at this point in the history
  • Loading branch information
ToxicToast committed Jun 2, 2024
1 parent 1904551 commit fb925b8
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 6 deletions.
1 change: 0 additions & 1 deletion libs/group-infrastructure/src/lib/dao/group.dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export interface GroupDAO {
updated_at: Nullable<Date>;
deleted_at: Nullable<Date>;
isActive: boolean;
isBanned: boolean;
isUpdated: boolean;
isDeleted: boolean;
}
40 changes: 40 additions & 0 deletions libs/group-infrastructure/src/lib/typeorm/mappers/group.mapper.ts
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);
}
}
2 changes: 1 addition & 1 deletion libs/group-infrastructure/src/lib/typeorm/mappers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {};
export * from './group.mapper';
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],
},
];
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'],
},
];
3 changes: 2 additions & 1 deletion libs/group-infrastructure/src/lib/typeorm/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export {};
export * from './base.provider';
export * from './group.provider';
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;
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {};
export * from './group.repository';
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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {};
export * from './group.service';
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class UserRepository implements DomainRepository {
const entity = await this.repository.findOne({
withDeleted: true,
where: { id },
// relations: ['groups'],
relations: ['groups'],
});
if (entity) {
return this.mapper.toDomain(entity);
Expand Down

0 comments on commit fb925b8

Please sign in to comment.