forked from noxious/server
71 lines
1.9 KiB
TypeScript
71 lines
1.9 KiB
TypeScript
import type { UUID } from '#application/types'
|
|
|
|
import { BaseRepository } from '#application/base/baseRepository'
|
|
import { unduplicateArray } from '#application/utilities'
|
|
import { Map } from '#entities/map'
|
|
import { Tile } from '#entities/tile'
|
|
import MapService from '#services/mapService'
|
|
|
|
class TileRepository extends BaseRepository {
|
|
async getById(id: UUID) {
|
|
try {
|
|
const repository = this.getEntityManager().getRepository(Tile)
|
|
const result = await repository.findOne({ id })
|
|
if (result) result.setEntityManager(this.getEntityManager())
|
|
|
|
return result
|
|
} catch (error: any) {
|
|
return null
|
|
}
|
|
}
|
|
|
|
async getByIds(ids: UUID[]) {
|
|
try {
|
|
const repository = this.getEntityManager().getRepository(Tile)
|
|
const results = await repository.find({
|
|
id: ids
|
|
})
|
|
for (const result of results) result.setEntityManager(this.getEntityManager())
|
|
|
|
return results
|
|
} catch (error: any) {
|
|
return []
|
|
}
|
|
}
|
|
|
|
async getAll() {
|
|
try {
|
|
const repository = this.getEntityManager().getRepository(Tile)
|
|
const results = await repository.findAll()
|
|
for (const result of results) result.setEntityManager(this.getEntityManager())
|
|
|
|
return results
|
|
} catch (error: any) {
|
|
return []
|
|
}
|
|
}
|
|
|
|
async getByMapId(mapId: UUID) {
|
|
try {
|
|
const mapRepository = this.getEntityManager().getRepository(Map)
|
|
const tileRepository = this.getEntityManager().getRepository(Tile)
|
|
|
|
const map = await mapRepository.findOne({ id: mapId })
|
|
if (!map) return []
|
|
|
|
const mapTileArray = unduplicateArray(MapService.flattenMapArray(JSON.parse(JSON.stringify(map.tiles))))
|
|
|
|
const results = await tileRepository.find({
|
|
id: mapTileArray
|
|
})
|
|
for (const result of results) result.setEntityManager(this.getEntityManager())
|
|
|
|
return results
|
|
} catch (error: any) {
|
|
return []
|
|
}
|
|
}
|
|
}
|
|
|
|
export default TileRepository
|