Skip to content

Commit

Permalink
feat: finish group domain
Browse files Browse the repository at this point in the history
  • Loading branch information
ToxicToast committed Jun 2, 2024
1 parent e34ff50 commit 614a9ec
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 4 deletions.
61 changes: 61 additions & 0 deletions libs/group-domain/src/lib/factories/group.factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Factory } from '@toxictoast/azkaban-base-domain';
import { GroupAggregate, GroupAnemic, GroupData } from '@azkaban/group-domain';

export class GroupFactory
implements Factory<GroupAnemic, GroupAggregate, GroupData>
{
reconstitute(data: GroupAnemic): GroupAggregate {
const { id, title, slug, active, created_at, updated_at, deleted_at } =
data;

return new GroupAggregate(
id,
title,
slug,
active,
created_at,
updated_at,
deleted_at,
);
}

constitute(data: GroupAggregate): GroupAnemic {
const {
id,
title,
slug,
active,
created_at,
updated_at,
deleted_at,
isActive,
isUpdated,
isDeleted,
} = data.toAnemic();
return {
id,
title,
slug,
active,
created_at,
updated_at,
deleted_at,
isActive,
isUpdated,
isDeleted,
};
}

createDomain(data: GroupData): GroupAggregate {
const { title } = data;
return new GroupAggregate(
null,
title,
null,
false,
new Date(),
new Date(),
null,
);
}
}
2 changes: 1 addition & 1 deletion libs/group-domain/src/lib/factories/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {};
export * from './group.factory';
4 changes: 4 additions & 0 deletions libs/group-domain/src/lib/repositories/group.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { GroupAnemic } from '../anemics';
import { Repository } from '@toxictoast/azkaban-base-domain';

export type GroupRepository = Repository<GroupAnemic>;
2 changes: 1 addition & 1 deletion libs/group-domain/src/lib/repositories/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {};
export * from './group.repository';
132 changes: 132 additions & 0 deletions libs/group-domain/src/lib/services/group.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { GroupFactory } from '../factories';
import { GroupRepository } from '../repositories';
import { GroupAnemic } from '../anemics';
import { Result } from '@toxictoast/azkaban-base-domain';
import { Optional } from '@toxictoast/azkaban-base-types';
import { GroupData } from '../data';

export class GroupService {
private readonly factory: GroupFactory = new GroupFactory();

constructor(private readonly repository: GroupRepository) {}

private async save(anemic: GroupAnemic): Promise<Result<GroupAnemic>> {
try {
const result = await this.repository.save(anemic);
return Result.ok<GroupAnemic>(result);
} catch (error) {
return Result.fail<GroupAnemic>(error);
}
}

async getGroups(
limit?: Optional<number>,
offset?: Optional<number>,
): Promise<Result<Array<GroupAnemic>>> {
try {
const result = await this.repository.findList(limit, offset);
return Result.ok<Array<GroupAnemic>>(result);
} catch (error) {
return Result.fail<Array<GroupAnemic>>(error);
}
}

async getGroupById(id: string): Promise<Result<GroupAnemic>> {
try {
const result = await this.repository.findById(id);
if (result !== null) {
return Result.ok<GroupAnemic>(result);
}
return Result.fail<GroupAnemic>('Group not found'); // TODO: Add error code
} catch (error) {
return Result.fail<GroupAnemic>(error);
}
}

async createGroup(data: GroupData): Promise<Result<GroupAnemic>> {
try {
const aggregate = this.factory.createDomain(data);
return await this.save(aggregate.toAnemic());
} catch (error) {
return Result.fail<GroupAnemic>(error);
}
}

async deleteGroup(id: string): Promise<Result<GroupAnemic>> {
try {
const group = await this.getGroupById(id);
if (group.isSuccess) {
const groupValue = group.value;
const aggregate = this.factory.reconstitute(groupValue);
aggregate.delete();
return await this.save(aggregate.toAnemic());
}
return Result.fail<GroupAnemic>('Group not found'); // TODO: Add error code
} catch (error) {
return Result.fail<GroupAnemic>(error);
}
}

async restoreGroup(id: string): Promise<Result<GroupAnemic>> {
try {
const group = await this.getGroupById(id);
if (group.isSuccess) {
const groupValue = group.value;
const aggregate = this.factory.reconstitute(groupValue);
aggregate.restore();
return await this.save(aggregate.toAnemic());
}
return Result.fail<GroupAnemic>('Group not found'); // TODO: Add error code
} catch (error) {
return Result.fail<GroupAnemic>(error);
}
}

async updateTitle(id: string, title: string): Promise<Result<GroupAnemic>> {
try {
const group = await this.getGroupById(id);
if (group.isSuccess) {
const groupValue = group.value;
const aggregate = this.factory.reconstitute(groupValue);
aggregate.updateTitle(title);
return await this.save(aggregate.toAnemic());
}
return Result.fail<GroupAnemic>('Group not found'); // TODO: Add error code
} catch (error) {
return Result.fail<GroupAnemic>(error);
}
}

async updateSlug(id: string, slug: string): Promise<Result<GroupAnemic>> {
try {
const group = await this.getGroupById(id);
if (group.isSuccess) {
const groupValue = group.value;
const aggregate = this.factory.reconstitute(groupValue);
aggregate.updateSlug(slug);
return await this.save(aggregate.toAnemic());
}
return Result.fail<GroupAnemic>('Group not found'); // TODO: Add error code
} catch (error) {
return Result.fail<GroupAnemic>(error);
}
}

async updateActive(
id: string,
active: boolean,
): Promise<Result<GroupAnemic>> {
try {
const group = await this.getGroupById(id);
if (group.isSuccess) {
const groupValue = group.value;
const aggregate = this.factory.reconstitute(groupValue);
aggregate.updateActive(active);
return await this.save(aggregate.toAnemic());
}
return Result.fail<GroupAnemic>('Group not found'); // TODO: Add error code
} catch (error) {
return Result.fail<GroupAnemic>(error);
}
}
}
2 changes: 1 addition & 1 deletion libs/group-domain/src/lib/services/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {};
export * from './group.service';
2 changes: 1 addition & 1 deletion libs/user-domain/src/lib/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class UserService {
if (result !== null) {
return Result.ok<UserAnemic>(result);
}
return Result.fail<UserAnemic>(UserErrorCodes.NOT_ACTIVE);
return Result.fail<UserAnemic>(UserErrorCodes.NOT_FOUND);
} catch (error) {
return Result.fail<UserAnemic>(error);
}
Expand Down

0 comments on commit 614a9ec

Please sign in to comment.