forked from noxious/server
38 lines
1018 B
TypeScript
38 lines
1018 B
TypeScript
import prisma from '../utilities/prisma'
|
|
import { gameLogger } from '../utilities/logger'
|
|
import { World } from '@prisma/client'
|
|
import WorldRepository from '../repositories/worldRepository'
|
|
|
|
class WorldService {
|
|
async update(worldData: Partial<World>): Promise<boolean> {
|
|
try {
|
|
const currentWorld = await WorldRepository.getFirst()
|
|
if (!currentWorld) {
|
|
// If no world exists, create first record
|
|
await prisma.world.create({
|
|
data: {
|
|
...worldData,
|
|
date: worldData.date || new Date()
|
|
}
|
|
})
|
|
return true
|
|
}
|
|
|
|
// Update existing world using its date as unique identifier
|
|
await prisma.world.update({
|
|
where: {
|
|
date: currentWorld.date
|
|
},
|
|
data: worldData
|
|
})
|
|
|
|
return true
|
|
} catch (error: any) {
|
|
gameLogger.error(`Failed to update world: ${error instanceof Error ? error.message : String(error)}`)
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
|
|
export default new WorldService()
|