theater-service/src/modules/theater/infra/prisma/theater.prisma.repository.ts

47 lines
1.3 KiB
TypeScript

import { Injectable } from '@nestjs/common'
import { Pick } from '@prisma/client/runtime/client'
import { Theater } from '@prisma/generated/client'
import { PrismaService } from '@/infra/prisma/prisma.service'
import { TheaterEntity } from '@/modules/theater/domain/entities/theater.entity'
import { TheaterRepositoryPort } from '../../domain/ports/theater.repository.port'
@Injectable()
export class TheaterPrismaRepository implements TheaterRepositoryPort {
public constructor(private readonly prismaService: PrismaService) {}
public async create(
data: Pick<TheaterEntity, 'name' | 'address'>
): Promise<TheaterEntity> {
const t = await this.prismaService.theater.create({ data })
return this.toEntity(t)
}
public async findAll(): Promise<TheaterEntity[]> {
const items = await this.prismaService.theater.findMany({
orderBy: { name: 'asc' }
})
return items.map(t => this.toEntity(t))
}
public async findById(
id: TheaterEntity['id']
): Promise<TheaterEntity | null> {
const theater = await this.prismaService.theater.findUnique({
where: { id }
})
return theater ? this.toEntity(theater) : null
}
private toEntity(theater: Theater): TheaterEntity {
return new TheaterEntity(
theater.id,
theater.name,
theater.address,
theater.createdAt,
theater.updatedAt
)
}
}