diff --git a/src/modules/restaurants/dto/create-restaurant.dto.ts b/src/modules/restaurants/dto/create-restaurant.dto.ts index 7d41d62..edba163 100644 --- a/src/modules/restaurants/dto/create-restaurant.dto.ts +++ b/src/modules/restaurants/dto/create-restaurant.dto.ts @@ -36,12 +36,14 @@ export class CreateRestaurantDto { longitude?: number; @ApiPropertyOptional({ - example: 'POLYGON((51.389 35.6892, 51.390 35.6892, 51.390 35.6902, 51.389 35.6902, 51.389 35.6892))', - description: 'محدوده سرویس‌دهی به صورت PostGIS Polygon (WKT format)', + example: { type: 'Polygon', coordinates: [[[51.389, 35.6892], [51.390, 35.6892], [51.390, 35.6902], [51.389, 35.6902], [51.389, 35.6892]]] }, + description: 'محدوده سرویس‌دهی به صورت GeoJSON Polygon', }) @IsOptional() - @IsString() - serviceArea?: string; + serviceArea?: { + type: 'Polygon'; + coordinates: number[][][]; + }; @ApiPropertyOptional({ example: 2020, description: 'سال تأسیس' }) @IsOptional() diff --git a/src/modules/restaurants/dto/restaurant-specification.dto.ts b/src/modules/restaurants/dto/restaurant-specification.dto.ts index 6fd8133..4c5ff3b 100644 --- a/src/modules/restaurants/dto/restaurant-specification.dto.ts +++ b/src/modules/restaurants/dto/restaurant-specification.dto.ts @@ -26,10 +26,13 @@ export class RestaurantSpecificationDto { longitude?: number; @ApiPropertyOptional({ - example: 'POLYGON((51.389 35.6892, 51.390 35.6892, 51.390 35.6902, 51.389 35.6902, 51.389 35.6892))', - description: 'Service area as PostGIS Polygon (WKT format)', + example: { type: 'Polygon', coordinates: [[[51.389, 35.6892], [51.390, 35.6892], [51.390, 35.6902], [51.389, 35.6902], [51.389, 35.6892]]] }, + description: 'Service area as GeoJSON Polygon', }) - serviceArea?: string; + serviceArea?: { + type: 'Polygon'; + coordinates: number[][][]; + }; @ApiPropertyOptional({ example: 2020, description: 'Established year' }) establishedYear?: number; diff --git a/src/modules/restaurants/entities/restaurant.entity.ts b/src/modules/restaurants/entities/restaurant.entity.ts index 4700d5e..ca02330 100644 --- a/src/modules/restaurants/entities/restaurant.entity.ts +++ b/src/modules/restaurants/entities/restaurant.entity.ts @@ -1,6 +1,5 @@ import { Entity, Property } from '@mikro-orm/core'; import { BaseEntity } from '../../../common/entities/base.entity'; -import { PolygonType } from '../types/polygun.type'; @Entity({ tableName: 'restaurants' }) export class Restaurant extends BaseEntity { @@ -27,8 +26,11 @@ export class Restaurant extends BaseEntity { @Property({ type: 'decimal', precision: 10, scale: 7, nullable: true }) longitude?: number; - @Property({ type: PolygonType, nullable: true }) - serviceArea?: string; + @Property({ type: 'json', nullable: true }) + serviceArea?: { + type: 'Polygon'; + coordinates: number[][][]; + }; // --- وضعیت‌ها --- @Property({ default: true }) diff --git a/src/modules/restaurants/types/polygun.type.ts b/src/modules/restaurants/types/polygun.type.ts deleted file mode 100644 index 955c3c8..0000000 --- a/src/modules/restaurants/types/polygun.type.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { Type } from '@mikro-orm/core'; -import type { Platform, TransformContext } from '@mikro-orm/core'; - -export class PolygonType extends Type { - getColumnType() { - return 'geometry(Polygon, 4326)'; - } - - convertToDatabaseValue( - value: string | null | undefined, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - _platform: Platform, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - _context?: TransformContext, - ): string | null { - if (value === null || value === undefined) { - return null; - } - // Convert WKT string to PostGIS geometry - // The value should be in WKT format: "POLYGON((lon1 lat1, lon2 lat2, ...))" - // PostgreSQL will handle the conversion using ST_GeomFromText when inserting - return value; - } - - convertToJSValue( - value: string | null | undefined, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - _platform: Platform, - ): string | null { - if (value === null || value === undefined) { - return null; - } - // PostgreSQL returns geometry as hex-encoded EWKB or we need to use ST_AsText - // For now, assuming the value is already in WKT format or will be converted via SQL - // If needed, you can use raw SQL in queries: SELECT ST_AsText(service_area) as service_area - return value; - } - - toJSON(value: string | null | undefined, platform: Platform): string | null { - return this.convertToJSValue(value, platform); - } -} diff --git a/src/seeders/DatabaseSeeder.ts b/src/seeders/DatabaseSeeder.ts index 3718295..6837af6 100644 --- a/src/seeders/DatabaseSeeder.ts +++ b/src/seeders/DatabaseSeeder.ts @@ -113,7 +113,10 @@ export class DatabaseSeeder extends Seeder { slug: 'zhivan', isActive: true, phone: '09123456789', - serviceArea: 'POLYGON((51.389 35.6892, 51.390 35.6892, 51.390 35.6902, 51.389 35.6902, 51.389 35.6892))', + serviceArea: { + type: 'Polygon', + coordinates: [[[51.389, 35.6892], [51.390, 35.6892], [51.390, 35.6902], [51.389, 35.6902], [51.389, 35.6892]]], + }, }); em.persist(restaurant); } @@ -125,7 +128,10 @@ export class DatabaseSeeder extends Seeder { slug: 'boote', isActive: true, phone: '09123456790', - serviceArea: 'POLYGON((51.389 35.6892, 51.390 35.6892, 51.390 35.6902, 51.389 35.6902, 51.389 35.6892))', + serviceArea: { + type: 'Polygon', + coordinates: [[[51.389, 35.6892], [51.390, 35.6892], [51.390, 35.6902], [51.389, 35.6902], [51.389, 35.6892]]], + }, }); em.persist(restaurant); }