1
0
forked from noxious/server
noxious_server/src/repositories/characterRepository.ts

61 lines
2.2 KiB
TypeScript

import type { UUID } from '#application/types'
import { BaseRepository } from '#application/base/baseRepository'
import { Character } from '#entities/character'
class CharacterRepository extends BaseRepository {
async getByUserId(userId: UUID): Promise<Character[]> {
try {
const repository = this.getEntityManager().getRepository(Character)
const results = await repository.find({ user: userId })
for (const result of results) result.setEntityManager(this.getEntityManager())
return results
} catch (error: any) {
this.logger.error(`Failed to get character by user ID: ${error instanceof Error ? error.message : String(error)}`)
return []
}
}
async getByUserAndId(userId: UUID, characterId: UUID): Promise<Character | null> {
try {
const repository = this.getEntityManager().getRepository(Character)
const result = await repository.findOne({ user: userId, id: characterId })
if (result) result.setEntityManager(this.getEntityManager())
return result
} catch (error: any) {
this.logger.error(`Failed to get character by user ID and character ID: ${error instanceof Error ? error.message : String(error)}`)
return null
}
}
async getById(id: UUID, populate?: any): Promise<Character | null> {
try {
const repository = this.getEntityManager().getRepository(Character)
const result = await repository.findOne({ id }, { populate })
if (result) result.setEntityManager(this.getEntityManager())
return result
} catch (error: any) {
this.logger.error(`Failed to get character by ID: ${error instanceof Error ? error.message : String(error)}`)
return null
}
}
async getByName(name: string, populate?: any): Promise<Character | null> {
try {
const repository = this.getEntityManager().getRepository(Character)
const result = await repository.findOne({ name }, { populate })
if (result) result.setEntityManager(this.getEntityManager())
return result
} catch (error: any) {
this.logger.error(`Failed to get character by name: ${error instanceof Error ? error.message : String(error)}`)
return null
}
}
}
export default CharacterRepository