This commit is contained in:
2025-11-22 17:02:53 +03:30
parent 852c5f722b
commit f3607ad407
5 changed files with 25 additions and 54 deletions
@@ -36,12 +36,14 @@ export class CreateRestaurantDto {
longitude?: number; longitude?: number;
@ApiPropertyOptional({ @ApiPropertyOptional({
example: 'POLYGON((51.389 35.6892, 51.390 35.6892, 51.390 35.6902, 51.389 35.6902, 51.389 35.6892))', 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: 'محدوده سرویس‌دهی به صورت PostGIS Polygon (WKT format)', description: 'محدوده سرویس‌دهی به صورت GeoJSON Polygon',
}) })
@IsOptional() @IsOptional()
@IsString() serviceArea?: {
serviceArea?: string; type: 'Polygon';
coordinates: number[][][];
};
@ApiPropertyOptional({ example: 2020, description: 'سال تأسیس' }) @ApiPropertyOptional({ example: 2020, description: 'سال تأسیس' })
@IsOptional() @IsOptional()
@@ -26,10 +26,13 @@ export class RestaurantSpecificationDto {
longitude?: number; longitude?: number;
@ApiPropertyOptional({ @ApiPropertyOptional({
example: 'POLYGON((51.389 35.6892, 51.390 35.6892, 51.390 35.6902, 51.389 35.6902, 51.389 35.6892))', 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 PostGIS Polygon (WKT format)', description: 'Service area as GeoJSON Polygon',
}) })
serviceArea?: string; serviceArea?: {
type: 'Polygon';
coordinates: number[][][];
};
@ApiPropertyOptional({ example: 2020, description: 'Established year' }) @ApiPropertyOptional({ example: 2020, description: 'Established year' })
establishedYear?: number; establishedYear?: number;
@@ -1,6 +1,5 @@
import { Entity, Property } from '@mikro-orm/core'; import { Entity, Property } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity'; import { BaseEntity } from '../../../common/entities/base.entity';
import { PolygonType } from '../types/polygun.type';
@Entity({ tableName: 'restaurants' }) @Entity({ tableName: 'restaurants' })
export class Restaurant extends BaseEntity { export class Restaurant extends BaseEntity {
@@ -27,8 +26,11 @@ export class Restaurant extends BaseEntity {
@Property({ type: 'decimal', precision: 10, scale: 7, nullable: true }) @Property({ type: 'decimal', precision: 10, scale: 7, nullable: true })
longitude?: number; longitude?: number;
@Property({ type: PolygonType, nullable: true }) @Property({ type: 'json', nullable: true })
serviceArea?: string; serviceArea?: {
type: 'Polygon';
coordinates: number[][][];
};
// --- وضعیت‌ها --- // --- وضعیت‌ها ---
@Property({ default: true }) @Property({ default: true })
@@ -1,42 +0,0 @@
import { Type } from '@mikro-orm/core';
import type { Platform, TransformContext } from '@mikro-orm/core';
export class PolygonType extends Type<string | null, string | null> {
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);
}
}
+8 -2
View File
@@ -113,7 +113,10 @@ export class DatabaseSeeder extends Seeder {
slug: 'zhivan', slug: 'zhivan',
isActive: true, isActive: true,
phone: '09123456789', 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); em.persist(restaurant);
} }
@@ -125,7 +128,10 @@ export class DatabaseSeeder extends Seeder {
slug: 'boote', slug: 'boote',
isActive: true, isActive: true,
phone: '09123456790', 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); em.persist(restaurant);
} }