Skip to content

Commit

Permalink
fix: get base of space id
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Aug 18, 2024
1 parent 20f80bf commit 9b2b5ea
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import {
BaseNameShouldBeUnique,
injectBaseRepository,
WithBaseName,
WithBaseSpaceId,
type IBaseRepository,
} from "@undb/base"
import { CreateBaseCommand } from "@undb/commands"
import { mustGetCurrentSpaceId } from "@undb/context/server"
import { commandHandler } from "@undb/cqrs"
import { singleton } from "@undb/di"
import { applyRules, type ICommandHandler } from "@undb/domain"
Expand All @@ -25,7 +27,7 @@ export class CreateBaseCommandHandler implements ICommandHandler<CreateBaseComma
async execute(command: CreateBaseCommand): Promise<any> {
this.logger.debug(command)

const nameSpec = new WithBaseName(BaseName.from(command.name))
const nameSpec = new WithBaseName(BaseName.from(command.name)).and(new WithBaseSpaceId(mustGetCurrentSpaceId()))
const exists = (await this.repository.findOne(nameSpec)).into(null)

applyRules(new BaseNameShouldBeUnique(!!exists))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { BaseName, BaseNameShouldBeUnique, injectBaseRepository, WithBaseName, type IBaseRepository } from "@undb/base"
import {
BaseName,
BaseNameShouldBeUnique,
injectBaseRepository,
WithBaseName,
WithBaseSpaceId,
type IBaseRepository,
} from "@undb/base"
import { UpdateBaseCommand } from "@undb/commands"
import { mustGetCurrentSpaceId } from "@undb/context/server"
import { commandHandler } from "@undb/cqrs"
import { singleton } from "@undb/di"
import { applyRules, type ICommandHandler } from "@undb/domain"
Expand All @@ -21,7 +29,7 @@ export class UpdateBaseCommandHandler implements ICommandHandler<UpdateBaseComma
const base = (await this.repository.findOneById(command.id)).unwrap()

if (command.name) {
const nameSpec = new WithBaseName(BaseName.from(command.name))
const nameSpec = new WithBaseName(BaseName.from(command.name)).and(new WithBaseSpaceId(mustGetCurrentSpaceId()))
const exists = (await this.repository.findOne(nameSpec)).into(null)

applyRules(new BaseNameShouldBeUnique(!!exists))
Expand Down
3 changes: 0 additions & 3 deletions packages/persistence/src/base/base.filter-visitor.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import type { Base, IBaseSpecVisitor, WithBaseId, WithBaseName, WithBaseQ, WithBaseSpaceId } from "@undb/base"
import type { WithBaseOption } from "@undb/base/src/specifications/base-option.specification"
import type { DuplicatedBaseSpecification } from "@undb/base/src/specifications/base.specification"
import { mustGetCurrentSpaceId } from "@undb/context/server"
import type { ExpressionBuilder } from "kysely"
import { AbstractQBVisitor } from "../abstract-qb.visitor"
import type { Database } from "../db"

export class BaseFilterVisitor extends AbstractQBVisitor<Base> implements IBaseSpecVisitor {
constructor(protected readonly eb: ExpressionBuilder<Database, "undb_base">) {
super(eb)
const spaceId = mustGetCurrentSpaceId()
this.addCond(this.eb.eb("space_id", "=", spaceId))
}

withOption(v: WithBaseOption): void {
Expand Down
5 changes: 3 additions & 2 deletions packages/persistence/src/base/base.repository.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {
WithBaseId,
WithBaseSpaceId,
injectBaseOutboxService,
type Base,
type IBaseOutboxService,
type IBaseRepository,
type IBaseSpecification,
} from "@undb/base"
import { executionContext } from "@undb/context/server"
import { executionContext, mustGetCurrentSpaceId } from "@undb/context/server"
import { inject, singleton } from "@undb/di"
import { None, Some, type Option } from "@undb/domain"
import { getCurrentTransaction } from "../ctx"
Expand Down Expand Up @@ -55,7 +56,7 @@ export class BaseRepository implements IBaseRepository {
return base ? Some(this.mapper.toDo(base)) : None
}
async findOneById(id: string): Promise<Option<Base>> {
const spec = WithBaseId.fromString(id)
const spec = WithBaseId.fromString(id).and(new WithBaseSpaceId(mustGetCurrentSpaceId()))

const base = await (getCurrentTransaction() ?? this.qb)
.selectFrom("undb_base")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { injectBaseQueryRepository, type IBaseDTO, type IBaseQueryRepository } from "@undb/base"
import { injectBaseQueryRepository, WithBaseSpaceId, type IBaseDTO, type IBaseQueryRepository } from "@undb/base"
import { mustGetCurrentSpaceId } from "@undb/context/server"
import { queryHandler } from "@undb/cqrs"
import { singleton } from "@undb/di"
import { None, type IQueryHandler } from "@undb/domain"
import { Some, type IQueryHandler } from "@undb/domain"
import { GetBasesQuery } from "@undb/queries"

@queryHandler(GetBasesQuery)
Expand All @@ -13,6 +14,7 @@ export class GetBasesQueryHandler implements IQueryHandler<GetBasesQuery, any> {
) {}

async execute(query: GetBasesQuery): Promise<IBaseDTO[]> {
return this.repo.find(None)
const spec = new WithBaseSpaceId(mustGetCurrentSpaceId())
return this.repo.find(Some(spec))
}
}

0 comments on commit 9b2b5ea

Please sign in to comment.