Fixed creating objects when updating map

This commit is contained in:
2024-07-07 19:04:19 +02:00
parent 4fcb9693b6
commit 37764847a9
2 changed files with 28 additions and 13 deletions

View File

@ -1,4 +1,4 @@
import { Zone } from '@prisma/client';
import { Zone, ZoneObject } from '@prisma/client'
import prisma from '../utilities/Prisma'; // Import the global Prisma instance
class ZoneRepository {
@ -27,7 +27,11 @@ class ZoneRepository {
id: id
},
include: {
zoneObjects: true
zoneObjects: {
include: {
object: true
}
}
}
});
} catch (error: any) {
@ -52,17 +56,28 @@ class ZoneRepository {
}
}
async update(id: number, name: string, width: number, height: number, tiles: string[][]): Promise<Zone> {
async update(id: number, name: string, width: number, height: number, tiles: string[][], objects: ZoneObject[]): Promise<Zone> {
try {
console.log(tiles);
return await prisma.zone.update({
where: {
id: id
},
data: {
name: name,
width: width,
height: height,
tiles: tiles
name,
width,
height,
tiles,
zoneObjects: {
deleteMany: {
zoneId: id // Ensure only objects related to the zone are deleted
},
create: objects.map(obj => ({
objectId: obj.objectId,
position_x: obj.position_x,
position_y: obj.position_y
}))
}
}
});
} catch (error: any) {