forked from noxious/server
61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
import type { UUID } from '#application/types'
|
|
|
|
import { BaseRepository } from '#application/base/baseRepository'
|
|
import { Chat } from '#entities/chat'
|
|
|
|
class ChatRepository extends BaseRepository {
|
|
async getById(id: UUID): Promise<Chat[]> {
|
|
try {
|
|
const repository = this.getEntityManager().getRepository(Chat)
|
|
const results = await repository.find({ id })
|
|
for (const result of results) result.setEntityManager(this.getEntityManager())
|
|
|
|
return results
|
|
} catch (error: any) {
|
|
this.logger.error(`Failed to get chat by ID: ${error instanceof Error ? error.message : String(error)}`)
|
|
return []
|
|
}
|
|
}
|
|
|
|
async getAll(): Promise<Chat[]> {
|
|
try {
|
|
const repository = this.getEntityManager().getRepository(Chat)
|
|
const results = await repository.findAll()
|
|
for (const result of results) result.setEntityManager(this.getEntityManager())
|
|
|
|
return results
|
|
} catch (error: any) {
|
|
this.logger.error(`Failed to get all chats: ${error instanceof Error ? error.message : String(error)}`)
|
|
return []
|
|
}
|
|
}
|
|
|
|
async getByCharacterId(characterId: UUID): Promise<Chat[]> {
|
|
try {
|
|
const repository = this.getEntityManager().getRepository(Chat)
|
|
const results = await repository.find({ character: characterId })
|
|
for (const result of results) result.setEntityManager(this.getEntityManager())
|
|
|
|
return results
|
|
} catch (error: any) {
|
|
this.logger.error(`Failed to get chats by character ID: ${error instanceof Error ? error.message : String(error)}`)
|
|
return []
|
|
}
|
|
}
|
|
|
|
async getByMapId(mapId: UUID): Promise<Chat[]> {
|
|
try {
|
|
const repository = this.getEntityManager().getRepository(Chat)
|
|
const results = await repository.find({ map: mapId })
|
|
for (const result of results) result.setEntityManager(this.getEntityManager())
|
|
|
|
return results
|
|
} catch (error: any) {
|
|
this.logger.error(`Failed to get chats by map ID: ${error instanceof Error ? error.message : String(error)}`)
|
|
return []
|
|
}
|
|
}
|
|
}
|
|
|
|
export default ChatRepository
|