forked from undb-io/undb
-
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
Showing
16 changed files
with
292 additions
and
5 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
23 changes: 23 additions & 0 deletions
23
packages/command-handlers/src/handlers/enable-share.command-handler.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,23 @@ | ||
import { EnableShareCommand } from "@undb/commands" | ||
import { commandHandler } from "@undb/cqrs" | ||
import { singleton } from "@undb/di" | ||
import type { ICommandHandler } from "@undb/domain" | ||
import { createLogger } from "@undb/logger" | ||
import { injectShareService, type IShareService } from "@undb/share" | ||
|
||
@commandHandler(EnableShareCommand) | ||
@singleton() | ||
export class EnableShareCommandHandler implements ICommandHandler<EnableShareCommand, any> { | ||
private readonly logger = createLogger(EnableShareCommandHandler.name) | ||
|
||
constructor( | ||
@injectShareService() | ||
private readonly service: IShareService, | ||
) {} | ||
|
||
async execute(command: EnableShareCommand): Promise<any> { | ||
this.logger.debug(command) | ||
|
||
await this.service.enableShare(command) | ||
} | ||
} |
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,18 @@ | ||
import { Command, type CommandProps } from "@undb/domain" | ||
import { enableShareDTO, type IShareTarget } from "@undb/share" | ||
import { z } from "@undb/zod" | ||
|
||
export const enableShareCommand = enableShareDTO | ||
|
||
export type IEnableShareCommand = z.infer<typeof enableShareCommand> | ||
|
||
export class EnableShareCommand extends Command implements IEnableShareCommand { | ||
public readonly target: IShareTarget | ||
public readonly enabled: boolean | ||
|
||
constructor(props: CommandProps<IEnableShareCommand>) { | ||
super(props) | ||
this.enabled = props.enabled | ||
this.target = props.target | ||
} | ||
} |
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
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
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
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,2 @@ | ||
export * from "./share.query-repository" | ||
export * from "./share.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,19 @@ | ||
import type { IShareSpecVisitor, Share, WithShareEnabled, WithShareForm, WithShareId, WithShareView } from "@undb/share" | ||
import { and, eq } from "drizzle-orm" | ||
import { AbstractDBFilterVisitor } from "../abstract-db.visitor" | ||
import { shareTable } from "../tables" | ||
|
||
export class ShareFilterVisitor extends AbstractDBFilterVisitor<Share> implements IShareSpecVisitor { | ||
idEqual(s: WithShareId): void { | ||
throw new Error("Method not implemented.") | ||
} | ||
targetView(s: WithShareView): void { | ||
this.addCond(and(eq(shareTable.targetType, "view"), eq(shareTable.targetId, s.viewId))) | ||
} | ||
targetForm(s: WithShareForm): void { | ||
this.addCond(and(eq(shareTable.targetType, "form"), eq(shareTable.targetId, s.formId))) | ||
} | ||
enabled(s: WithShareEnabled): void { | ||
throw new Error("Method not implemented.") | ||
} | ||
} |
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,36 @@ | ||
import { singleton } from "@undb/di" | ||
import type { Mapper } from "@undb/domain" | ||
import { ShareFactory, type IShareDTO, type IShareTarget, type Share as ShareDo } from "@undb/share" | ||
import type { Share } from "../tables" | ||
|
||
@singleton() | ||
export class ShareMapper implements Mapper<ShareDo, Share, IShareDTO> { | ||
toDo(entity: Share): ShareDo { | ||
return ShareFactory.fromJSON({ | ||
id: entity.id, | ||
target: { | ||
type: entity.targetType, | ||
id: entity.targetId, | ||
} as IShareTarget, | ||
enabled: entity.enabled, | ||
}) | ||
} | ||
toEntity(domain: ShareDo): Share { | ||
return { | ||
id: domain.id.value, | ||
targetId: domain.target.id, | ||
targetType: domain.target.type, | ||
enabled: domain.enabled, | ||
} | ||
} | ||
toDTO(entity: Share): IShareDTO { | ||
return { | ||
id: entity.id, | ||
target: { | ||
id: entity.targetId, | ||
type: entity.targetType, | ||
} as IShareTarget, | ||
enabled: entity.enabled, | ||
} | ||
} | ||
} |
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,42 @@ | ||
import { inject, singleton } from "@undb/di" | ||
import { None, Some, type Option } from "@undb/domain" | ||
import type { IShareDTO, IShareQueryRepository, ShareSpecification } from "@undb/share" | ||
import type { Database } from "../db" | ||
import { injectDb } from "../db.provider" | ||
import { shareTable } from "../tables" | ||
import { ShareFilterVisitor } from "./share.filter-visitor" | ||
import { ShareMapper } from "./share.mapper" | ||
|
||
@singleton() | ||
export class ShareQueryRepository implements IShareQueryRepository { | ||
constructor( | ||
@injectDb() | ||
private readonly db: Database, | ||
@inject(ShareMapper) | ||
private readonly mapper: ShareMapper, | ||
) {} | ||
async findOneById(id: string): Promise<Option<IShareDTO>> { | ||
throw new Error("Method not implemented.") | ||
} | ||
|
||
async findOne(spec: ShareSpecification): Promise<Option<IShareDTO>> { | ||
const qb = this.db.select().from(shareTable).$dynamic() | ||
const visitor = new ShareFilterVisitor() | ||
spec.accept(visitor) | ||
|
||
const results = await qb.where(visitor.cond).limit(1) | ||
if (results.length === 0) { | ||
return None | ||
} | ||
|
||
const [share] = results | ||
if (!share) { | ||
return None | ||
} | ||
|
||
return Some(this.mapper.toDTO(share)) | ||
} | ||
async find(): Promise<IShareDTO[]> { | ||
throw new Error("Method not implemented.") | ||
} | ||
} |
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,39 @@ | ||
import { inject, singleton } from "@undb/di" | ||
import type { Option } from "@undb/domain" | ||
import type { IShareRepository, Share, ShareSpecification } from "@undb/share" | ||
import type { Database } from "../db" | ||
import { injectDb } from "../db.provider" | ||
import { shareTable } from "../tables" | ||
import { ShareMapper } from "./share.mapper" | ||
|
||
@singleton() | ||
export class ShareRepository implements IShareRepository { | ||
constructor( | ||
@injectDb() | ||
private readonly db: Database, | ||
@inject(ShareMapper) | ||
private readonly mapper: ShareMapper, | ||
) {} | ||
insert(share: Share): Promise<void> { | ||
throw new Error("Method not implemented.") | ||
} | ||
async updateOneById(share: Share, spec: ShareSpecification): Promise<void> { | ||
const entity = this.mapper.toEntity(share) | ||
await this.db | ||
.insert(shareTable) | ||
.values(entity) | ||
.onConflictDoUpdate({ target: [shareTable.targetId, shareTable.targetType], set: { enabled: share.enabled } }) | ||
} | ||
findOneById(id: string): Promise<Option<Share>> { | ||
throw new Error("Method not implemented.") | ||
} | ||
findOne(spec: ShareSpecification): Promise<Option<Share>> { | ||
throw new Error("Method not implemented.") | ||
} | ||
find(spec: ShareSpecification): Promise<Share[]> { | ||
throw new Error("Method not implemented.") | ||
} | ||
deleteOneById(id: string): Promise<void> { | ||
throw new Error("Method not implemented.") | ||
} | ||
} |
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
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,51 @@ | ||
import { inject, singleton } from "@undb/di" | ||
import { Option } from "@undb/domain" | ||
import type { IEnableShareDTO, IShareDTO } from "../dto" | ||
import type { IShareTarget } from "../share-target.vo" | ||
import { ShareFactory } from "../share.factory" | ||
import { | ||
injectShareQueryRepository, | ||
injectShareRepository, | ||
type IShareQueryRepository, | ||
type IShareRepository, | ||
} from "../share.repository" | ||
import { WithShareId, withShare } from "../specifications" | ||
|
||
export interface IShareService { | ||
enableShare(dto: IEnableShareDTO): Promise<void> | ||
getShareByTarget(target: IShareTarget): Promise<Option<IShareDTO>> | ||
} | ||
|
||
export const SHARE_SERVICE = Symbol.for("SHARE_SERVICE") | ||
export const injectShareService = () => inject(SHARE_SERVICE) | ||
|
||
@singleton() | ||
export class ShareService implements IShareService { | ||
constructor( | ||
@injectShareRepository() | ||
private readonly repo: IShareRepository, | ||
@injectShareQueryRepository() | ||
private readonly queryRepo: IShareQueryRepository, | ||
) {} | ||
|
||
async enableShare(dto: IEnableShareDTO): Promise<void> { | ||
const spec = withShare(dto.target.type, dto.target.id) | ||
|
||
let share = (await this.repo.findOne(spec)).into(undefined) | ||
if (!share) { | ||
share = ShareFactory.create(WithShareId.create(), spec) | ||
} | ||
|
||
const s = share.$enable(dto) | ||
|
||
if (s.isSome()) { | ||
await this.repo.updateOneById(share, s.unwrap()) | ||
} | ||
} | ||
|
||
async getShareByTarget(target: IShareTarget): Promise<Option<IShareDTO>> { | ||
const spec = withShare(target.type, target.id) | ||
|
||
return this.queryRepo.findOne(spec) | ||
} | ||
} |
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
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
Oops, something went wrong.