-
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
13 changed files
with
170 additions
and
123 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
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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { ITeamRepository } from '../repository-interface/team-repository'; | ||
import { ITeamQueryService } from '../../usecase/team/query-service-interface/team-query-service'; | ||
|
||
export class UniqueTeamNameValidator { | ||
constructor(private teamRepository: ITeamRepository) {} | ||
constructor(private teamQueryService: ITeamQueryService) {} | ||
|
||
async exists(teamName: string): Promise<boolean> { | ||
const teamDto = await this.teamRepository.findByName(teamName); | ||
const teamDto = await this.teamQueryService.findByName(teamName); | ||
return !!teamDto; | ||
} | ||
} |
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,56 @@ | ||
import { PrismaClient } from '@prisma/client'; | ||
import { | ||
ITeamQueryService, | ||
TeamDto, | ||
} from '../../../usecase/team/query-service-interface/team-query-service'; | ||
|
||
export class TeamQueryService implements ITeamQueryService { | ||
constructor(private prismaClient: PrismaClient) {} | ||
|
||
async findAll(): Promise<TeamDto[]> { | ||
const teams = await this.prismaClient.team.findMany(); | ||
return teams.map( | ||
(team) => | ||
new TeamDto({ | ||
id: team.id, | ||
name: team.name, | ||
createdAt: team.createdAt, | ||
updatedAt: team.updatedAt, | ||
}), | ||
); | ||
} | ||
|
||
async findById(id: string): Promise<TeamDto | undefined> { | ||
const team = await this.prismaClient.team.findUnique({ | ||
where: { | ||
id, | ||
}, | ||
}); | ||
|
||
if (!team) return undefined; | ||
|
||
return new TeamDto({ | ||
id: team.id, | ||
name: team.name, | ||
createdAt: team.createdAt, | ||
updatedAt: team.updatedAt, | ||
}); | ||
} | ||
|
||
async findByName(name: string): Promise<TeamDto | undefined> { | ||
const team = await this.prismaClient.team.findFirst({ | ||
where: { | ||
name, | ||
}, | ||
}); | ||
|
||
if (!team) return undefined; | ||
|
||
return new TeamDto({ | ||
id: team.id, | ||
name: team.name, | ||
createdAt: team.createdAt, | ||
updatedAt: team.updatedAt, | ||
}); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
backend/src/infra/db/repository/team/team-repository-integration.spec.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,42 @@ | ||
import { TeamRepository } from './team-repository'; | ||
import { Team } from '../../../../domain/team/team'; | ||
import { TeamId } from '../../../../domain/team/team-id'; | ||
import { TeamName } from '../../../../domain/team/team-name'; | ||
import { prisma } from '../../../../../testUtils/prisma'; | ||
|
||
describe('TeamRepository', () => { | ||
const teamRepository = new TeamRepository(prisma); | ||
|
||
afterEach(async () => { | ||
await prisma.team.deleteMany({}); | ||
}); | ||
|
||
describe('update', () => { | ||
it('正常系 チームの名前を更新できること', async () => { | ||
// Arrange | ||
const id = '1'; | ||
|
||
await prisma.team.create({ | ||
data: { | ||
id, | ||
name: '001', | ||
createdAt: '2023-01-01T09:00:00Z', | ||
updatedAt: '2023-01-01T09:00:00Z', | ||
}, | ||
}); | ||
|
||
const team = Team.rebuild({ | ||
id: TeamId.rebuild(id), | ||
name: TeamName.rebuild('002'), | ||
createdAt: new Date('2023-01-01T09:00:00Z'), | ||
updatedAt: new Date('2023-01-01T09:00:00Z'), | ||
}); | ||
|
||
// Act | ||
const actual = await teamRepository.update(team); | ||
|
||
// Assert | ||
expect(actual).toBe(true); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,26 +1,26 @@ | ||
import { GetTeamsUsecase } from './get-teams-usecase'; | ||
import { TeamRepository } from '../../infra/db/repository/team/team-repository'; | ||
import { TeamQueryService } from '../../infra/db/query-service/team-query-service'; | ||
import { PrismaClient } from '@prisma/client'; | ||
|
||
jest.mock('@prisma/client'); | ||
jest.mock('../../infra/db/repository/team/team-repository'); | ||
jest.mock('../../infra/db/query-service/team-query-service'); | ||
|
||
describe('GetTeamsUsecase', () => { | ||
let mockRepository: jest.MockedObjectDeep<TeamRepository>; | ||
let mockQueryService: jest.MockedObjectDeep<TeamQueryService>; | ||
|
||
beforeAll(() => { | ||
const prisma = new PrismaClient(); | ||
mockRepository = jest.mocked(new TeamRepository(prisma)); | ||
mockQueryService = jest.mocked(new TeamQueryService(prisma)); | ||
}); | ||
|
||
describe('do', () => { | ||
it('正常系 TeamRepository.findAllを1回コールすること', async () => { | ||
it('正常系 TeamQueryService.findAllを1回コールすること', async () => { | ||
// Act | ||
const usecase = new GetTeamsUsecase(mockRepository); | ||
const usecase = new GetTeamsUsecase(mockQueryService); | ||
await usecase.do(); | ||
|
||
// Assert | ||
expect(mockRepository.findAll).toHaveBeenCalledTimes(1); | ||
expect(mockQueryService.findAll).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
}); |
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,12 +1,12 @@ | ||
import { | ||
ITeamRepository, | ||
ITeamQueryService, | ||
TeamDto, | ||
} from '../../domain/repository-interface/team-repository'; | ||
} from './query-service-interface/team-query-service'; | ||
|
||
export class GetTeamsUsecase { | ||
constructor(private teamRepository: ITeamRepository) {} | ||
constructor(private teamQueryService: ITeamQueryService) {} | ||
|
||
async do(): Promise<TeamDto[]> { | ||
return await this.teamRepository.findAll(); | ||
return await this.teamQueryService.findAll(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
backend/src/usecase/team/query-service-interface/team-query-service.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,25 @@ | ||
export class TeamDto { | ||
public readonly id: string; | ||
public readonly name: string; | ||
public readonly createdAt: Date; | ||
public readonly updatedAt: Date; | ||
|
||
constructor(props: { | ||
id: string; | ||
name: string; | ||
createdAt: Date; | ||
updatedAt: Date; | ||
}) { | ||
const { id, name, createdAt, updatedAt } = props; | ||
this.id = id; | ||
this.name = name; | ||
this.createdAt = createdAt; | ||
this.updatedAt = updatedAt; | ||
} | ||
} | ||
|
||
export interface ITeamQueryService { | ||
findAll(): Promise<TeamDto[]>; | ||
findById(id: string): Promise<TeamDto | undefined>; | ||
findByName(name: string): Promise<TeamDto | undefined>; | ||
} |
Oops, something went wrong.