-
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
e34ff50
commit 614a9ec
Showing
7 changed files
with
201 additions
and
4 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
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, | ||
); | ||
} | ||
} |
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.factory'; |
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,4 @@ | ||
import { GroupAnemic } from '../anemics'; | ||
import { Repository } from '@toxictoast/azkaban-base-domain'; | ||
|
||
export type GroupRepository = Repository<GroupAnemic>; |
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'; |
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,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); | ||
} | ||
} | ||
} |
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