forked from noxious/server
Added populate attributes to functions
This commit is contained in:
@ -22,7 +22,7 @@ export default class CharacterConnectEvent extends BaseEvent {
|
||||
return
|
||||
}
|
||||
|
||||
const character = await CharacterRepository.getByUserAndId(this.socket.userId!, data.characterId)
|
||||
const character = await CharacterRepository.getByUserAndId(this.socket.userId!, data.characterId, ['characterHair', 'characterHair'])
|
||||
|
||||
if (!character) {
|
||||
this.emitError('Character not found or does not belong to this user')
|
||||
|
@ -9,7 +9,11 @@ export default class CharacterListEvent extends BaseEvent {
|
||||
|
||||
private async handleEvent(data: any): Promise<void> {
|
||||
try {
|
||||
let characters: Character[] = await CharacterRepository.getByUserId(this.socket.userId!, ['characterType', 'characterHair'])
|
||||
let characters: Character[] = await CharacterRepository.getByUserId(this.socket.userId!, [
|
||||
'characterType',
|
||||
'characterHair'
|
||||
])
|
||||
|
||||
this.socket.emit('character:list', characters)
|
||||
} catch (error: any) {
|
||||
this.logger.error('character:list error', error.message)
|
||||
|
@ -1,23 +1,22 @@
|
||||
import { BaseRepository } from '#application/base/baseRepository'
|
||||
import { UUID } from '#application/types'
|
||||
import { Character } from '#entities/character'
|
||||
import { LoadHint, Populate } from '@mikro-orm/core'
|
||||
|
||||
class CharacterRepository extends BaseRepository {
|
||||
async getByUserId(userId: UUID, populate?: LoadHint<Character, '*'>): Promise<Character[]> {
|
||||
async getByUserId(userId: UUID, populate?: any): Promise<Character[]> {
|
||||
try {
|
||||
const repository = this.em.getRepository(Character)
|
||||
return await repository.find({ user: userId }, { populate: populate as Populate<Character> })
|
||||
return await repository.find({ user: userId }, { populate })
|
||||
} 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> {
|
||||
async getByUserAndId(userId: UUID, characterId: UUID, populate?: any): Promise<Character | null> {
|
||||
try {
|
||||
const repository = this.em.getRepository(Character)
|
||||
return await repository.findOne({ user: userId, id: characterId })
|
||||
return await repository.findOne({ user: userId, id: characterId }, { populate })
|
||||
} 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
|
||||
|
Reference in New Issue
Block a user