From 6baceb296aaf4d880b0ff359fb3e45314b52d2a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Juli=C3=A1n=20L=C3=B3pez=20Ca=C3=B1a?= Date: Wed, 16 Oct 2024 09:33:41 +0200 Subject: [PATCH] feat: model improvements --- apps/your-burger-api/project.json | 12 ++ .../domain/burger-brand/burger-brand.ts | 4 +- .../src/app/burger-business/domain/burger.ts | 4 +- .../http/_shared/dto/burger-brand-item.dto.ts | 4 + .../infra/http/_shared/dto/burger-item.dto.ts | 14 +++ .../infra/http/_utils/index.ts | 2 + .../http/_utils/paginated-response.dto.ts | 6 + .../infra/http/_utils/paginated-response.ts | 25 ++++ .../infra/http/burger/burgers.controller.ts | 19 ++- .../burger-brand/burger-brand.schema.ts | 8 +- .../burger-place/burger-place.schema.ts | 2 +- .../infra/persistence/burger/burger.schema.ts | 16 ++- .../src/assets/your-burger.sqlite3 | Bin 126976 -> 131072 bytes .../.snapshot-your-burger.sqlite3.json | 32 +++++ ...09214546.ts => Migration20241015174703.ts} | 5 +- .../src/seeders/initial-data/burger-places.ts | 116 ++++++++++-------- .../initial-data/ingredient-categories.ts | 1 - .../src/seeders/seed-entities.ts | 4 +- 18 files changed, 205 insertions(+), 69 deletions(-) create mode 100644 apps/your-burger-api/src/app/burger-business/infra/http/_shared/dto/burger-brand-item.dto.ts create mode 100644 apps/your-burger-api/src/app/burger-business/infra/http/_shared/dto/burger-item.dto.ts create mode 100644 apps/your-burger-api/src/app/burger-business/infra/http/_utils/index.ts create mode 100644 apps/your-burger-api/src/app/burger-business/infra/http/_utils/paginated-response.dto.ts create mode 100644 apps/your-burger-api/src/app/burger-business/infra/http/_utils/paginated-response.ts rename apps/your-burger-api/src/migrations/{Migration20241009214546.ts => Migration20241015174703.ts} (89%) diff --git a/apps/your-burger-api/project.json b/apps/your-burger-api/project.json index 9fdfdf0..a170e52 100644 --- a/apps/your-burger-api/project.json +++ b/apps/your-burger-api/project.json @@ -40,6 +40,18 @@ "options": { "jestConfig": "apps/your-burger-api/jest.config.ts" } + }, + "load-database": { + "executor": "nx:run-commands", + "options": { + "cwd": "apps/your-burger-api", + "commands": [ + "npx mikro-orm migration:create", + "npx mikro-orm migration:up", + "npx mikro-orm seeder:run --class=InitialDataSeeder" + ], + "parallel": false + } } } } diff --git a/apps/your-burger-api/src/app/burger-business/domain/burger-brand/burger-brand.ts b/apps/your-burger-api/src/app/burger-business/domain/burger-brand/burger-brand.ts index 3ab96c9..c8f7e2f 100644 --- a/apps/your-burger-api/src/app/burger-business/domain/burger-brand/burger-brand.ts +++ b/apps/your-burger-api/src/app/burger-business/domain/burger-brand/burger-brand.ts @@ -1,8 +1,10 @@ +import { Burger } from '../burger'; import { BurgerPlace } from '../burger-place/burger-place'; export class BurgerBrand { id: number; name: string; slug: string; - burgerPlace: BurgerPlace; + place: BurgerPlace; + burger: Burger; } diff --git a/apps/your-burger-api/src/app/burger-business/domain/burger.ts b/apps/your-burger-api/src/app/burger-business/domain/burger.ts index 8daee46..f608d88 100644 --- a/apps/your-burger-api/src/app/burger-business/domain/burger.ts +++ b/apps/your-burger-api/src/app/burger-business/domain/burger.ts @@ -1,4 +1,5 @@ import { Allergen } from './allergen/allergen'; +import { BurgerBrand } from './burger-brand/burger-brand'; import { BurgerPlace } from './burger-place/burger-place'; import { Ingredient } from './ingredient/ingredient'; @@ -24,7 +25,8 @@ export class Burger implements BurgerProps { name: string; type: BurgerType; description: string; + place: BurgerPlace; + brand: BurgerBrand; ingredients?: Ingredient[]; allergens?: Allergen[]; - place: BurgerPlace; } diff --git a/apps/your-burger-api/src/app/burger-business/infra/http/_shared/dto/burger-brand-item.dto.ts b/apps/your-burger-api/src/app/burger-business/infra/http/_shared/dto/burger-brand-item.dto.ts new file mode 100644 index 0000000..9000385 --- /dev/null +++ b/apps/your-burger-api/src/app/burger-business/infra/http/_shared/dto/burger-brand-item.dto.ts @@ -0,0 +1,4 @@ +export class BurgerBrandItemDto { + name: string; + slug: string; +} diff --git a/apps/your-burger-api/src/app/burger-business/infra/http/_shared/dto/burger-item.dto.ts b/apps/your-burger-api/src/app/burger-business/infra/http/_shared/dto/burger-item.dto.ts new file mode 100644 index 0000000..a43a840 --- /dev/null +++ b/apps/your-burger-api/src/app/burger-business/infra/http/_shared/dto/burger-item.dto.ts @@ -0,0 +1,14 @@ +// import { BurgerType } from '../../../../domain/burger'; +// import { BurgerBrandItemDto } from './burger-brand-item.dto'; + +export class BurgerItemDto { + id: string; + name: string; + description: string; + // type: BurgerType; + // brand: BurgerBrandItemDto; + + constructor(partial: Partial) { + Object.assign(this, partial); + } +} diff --git a/apps/your-burger-api/src/app/burger-business/infra/http/_utils/index.ts b/apps/your-burger-api/src/app/burger-business/infra/http/_utils/index.ts new file mode 100644 index 0000000..a1d1a3e --- /dev/null +++ b/apps/your-burger-api/src/app/burger-business/infra/http/_utils/index.ts @@ -0,0 +1,2 @@ +export * from './paginated-response'; +export * from './paginated-response.dto'; diff --git a/apps/your-burger-api/src/app/burger-business/infra/http/_utils/paginated-response.dto.ts b/apps/your-burger-api/src/app/burger-business/infra/http/_utils/paginated-response.dto.ts new file mode 100644 index 0000000..4506e4e --- /dev/null +++ b/apps/your-burger-api/src/app/burger-business/infra/http/_utils/paginated-response.dto.ts @@ -0,0 +1,6 @@ +import { Type } from 'class-transformer'; + +export class PaginatedResponseDto { + @Type(() => Array) + data: T[]; +} diff --git a/apps/your-burger-api/src/app/burger-business/infra/http/_utils/paginated-response.ts b/apps/your-burger-api/src/app/burger-business/infra/http/_utils/paginated-response.ts new file mode 100644 index 0000000..769b19d --- /dev/null +++ b/apps/your-burger-api/src/app/burger-business/infra/http/_utils/paginated-response.ts @@ -0,0 +1,25 @@ +import { applyDecorators, Type } from '@nestjs/common'; +import { ApiExtraModels, ApiOkResponse, getSchemaPath } from '@nestjs/swagger'; +import { PaginatedResponseDto } from './paginated-response.dto'; + +export const ApiOkResponsePaginated = >( + dataDto: DataDto +) => + applyDecorators( + ApiExtraModels(PaginatedResponseDto, dataDto), + ApiOkResponse({ + schema: { + allOf: [ + { $ref: getSchemaPath(PaginatedResponseDto) }, + { + properties: { + data: { + type: 'array', + items: { $ref: getSchemaPath(dataDto) }, + }, + }, + }, + ], + }, + }) + ); diff --git a/apps/your-burger-api/src/app/burger-business/infra/http/burger/burgers.controller.ts b/apps/your-burger-api/src/app/burger-business/infra/http/burger/burgers.controller.ts index dd2e6fe..96c6a88 100644 --- a/apps/your-burger-api/src/app/burger-business/infra/http/burger/burgers.controller.ts +++ b/apps/your-burger-api/src/app/burger-business/infra/http/burger/burgers.controller.ts @@ -1,8 +1,9 @@ import { Controller, Get } from '@nestjs/common'; import { ApiTags } from '@nestjs/swagger'; import { API_TAGS } from '../../../../../open-api'; -import { Burger } from '../../../domain/burger'; import { BurgersRepository } from '../../../domain/burgers.repository'; +import { BurgerItemDto } from '../_shared/dto/burger-item.dto'; +import { ApiOkResponsePaginated, PaginatedResponseDto } from '../_utils'; @Controller('burgers') @ApiTags(API_TAGS.BURGERS) @@ -10,7 +11,19 @@ export class BurgersController { constructor(private readonly burgersRepository: BurgersRepository) {} @Get() - public async getBurgers(): Promise { - return this.burgersRepository.find(); + @ApiOkResponsePaginated(BurgerItemDto) + public async getBurgers(): Promise> { + const burgers = await this.burgersRepository.find(); + console.log(JSON.stringify(burgers)); + return { + data: burgers.map( + (burger) => + new BurgerItemDto({ + id: burger.id, + name: burger.name, + description: burger.description, + }) + ), + }; } } diff --git a/apps/your-burger-api/src/app/burger-business/infra/persistence/burger-brand/burger-brand.schema.ts b/apps/your-burger-api/src/app/burger-business/infra/persistence/burger-brand/burger-brand.schema.ts index 174efe7..747dd45 100644 --- a/apps/your-burger-api/src/app/burger-business/infra/persistence/burger-brand/burger-brand.schema.ts +++ b/apps/your-burger-api/src/app/burger-business/infra/persistence/burger-brand/burger-brand.schema.ts @@ -1,6 +1,7 @@ import { EntitySchema } from '@mikro-orm/core'; import { BurgerBrand } from '../../../domain/burger-brand/burger-brand'; import { BurgerPlace } from '../../../domain/burger-place/burger-place'; +import { Burger } from '../../../domain/burger'; export const BurgerBrandSchema = new EntitySchema({ class: BurgerBrand, @@ -9,10 +10,15 @@ export const BurgerBrandSchema = new EntitySchema({ id: { type: 'int', primary: true, autoincrement: true }, name: { type: 'string', nullable: false, unique: true }, slug: { type: 'string', nullable: false, unique: true }, - burgerPlace: { + place: { kind: '1:m', entity: () => BurgerPlace, mappedBy: (place) => place.brand, }, + burger: { + kind: '1:m', + entity: () => Burger, + mappedBy: (burger) => burger.brand, + }, }, }); diff --git a/apps/your-burger-api/src/app/burger-business/infra/persistence/burger-place/burger-place.schema.ts b/apps/your-burger-api/src/app/burger-business/infra/persistence/burger-place/burger-place.schema.ts index 5c0eb3d..00d795b 100644 --- a/apps/your-burger-api/src/app/burger-business/infra/persistence/burger-place/burger-place.schema.ts +++ b/apps/your-burger-api/src/app/burger-business/infra/persistence/burger-place/burger-place.schema.ts @@ -14,7 +14,7 @@ export const BurgerPlaceSchema = new EntitySchema({ brand: { kind: 'm:1', entity: () => BurgerBrand, - inversedBy: (brand) => brand.burgerPlace, + inversedBy: (brand) => brand.place, }, geo: { kind: '1:1', entity: () => GeoPoint }, burgers: { diff --git a/apps/your-burger-api/src/app/burger-business/infra/persistence/burger/burger.schema.ts b/apps/your-burger-api/src/app/burger-business/infra/persistence/burger/burger.schema.ts index 1049215..0e2d946 100644 --- a/apps/your-burger-api/src/app/burger-business/infra/persistence/burger/burger.schema.ts +++ b/apps/your-burger-api/src/app/burger-business/infra/persistence/burger/burger.schema.ts @@ -3,6 +3,7 @@ import { Burger, BurgerType } from '../../../domain/burger'; import { Allergen } from '../../../domain/allergen/allergen'; import { BurgerPlace } from '../../../domain/burger-place/burger-place'; import { Ingredient } from '../../../domain/ingredient/ingredient'; +import { BurgerBrand } from '../../../domain/burger-brand/burger-brand'; export const BurgerSchema = new EntitySchema({ class: Burger, @@ -12,6 +13,16 @@ export const BurgerSchema = new EntitySchema({ name: { type: 'string', nullable: false, unique: true }, description: { type: 'string' }, type: { enum: true, items: () => BurgerType, nullable: false }, + place: { + kind: 'm:1', + entity: () => BurgerPlace, + inversedBy: 'burgers', + }, + brand: { + kind: 'm:1', + entity: () => BurgerBrand, + inversedBy: 'burger', + }, ingredients: { kind: 'm:n', entity: () => Ingredient, @@ -22,10 +33,5 @@ export const BurgerSchema = new EntitySchema({ entity: () => Allergen, nullable: true, }, - place: { - kind: 'm:1', - entity: () => BurgerPlace, - inversedBy: 'burgers', - }, }, }); diff --git a/apps/your-burger-api/src/assets/your-burger.sqlite3 b/apps/your-burger-api/src/assets/your-burger.sqlite3 index 763648cfc2ae20a04074db72995cd999b2d0e944..12beca56a76257b6ff8c5d3309459f4d5f92446a 100644 GIT binary patch delta 1508 zcmc&!T}V@57~Z?>>>PL8IWy;GJN_KgLe108(QU^`{*@JVGp!^<&~0s+6f%{dE-DWS zhJk3`<`=riU06^zqT#vdq9W?1AgjCn5F)xr$S%5-&Xh>$W<=6=^Zh;V^S$sr&oj78 z4h~Y|g%)1-d*n`390YD6YPS)yWRj>gzBYF2j~h$i0Tj?J6UkwN;sRe4+7vXaWFl!qE2jak>X}yIBo$v3C^mq1z17$=0Y~(lW z1DpDN8fHM>XhS^FAmsGyARRsq6uPH39J#PjFwhz55B7$&y3Wm~+3P*rrSjN;0ACvT zg$b0QAo0dLbdez)90wyuQJv`{`jnB63n#&bM6v_l(-y=!9?hAZ=%l%RVT|mffSI>` zW;w|t$!=M!suU~Tf+Px(hnM6Uw_GDu&GR!?io2?-y5wp}@O6b`!5#KSgrF4mYRHhM z+U*O9KH0bMLCLp&{e!jj|@LcxevaC_vS z5Ryfo5b;LB)e&!(J0gcs+qdDgUYU8%mZU>?A3hYOMYv?LswN(urAMo-x**LC)YW=e zNso208Gx6`q>dqGb!-LmmZ`H$S)7?u^h>%eqn)->@2FGc(w|b+b)>|+$Dq_YP&;@b z7hQ*|usy;yl1WhT;55|zH*KRaGTTJA=v<8)%A*U^rz5bRx&u+L?xs{7OEl=%S8NZn z#-Pl{nV0D=^fB{jMiceWG)hH@uSA0;O($VcO7n4J3=RXIvK>yuzz=zNB?brZNDPW` z{Un%idmLQg#Mk0b3HexawjDo@0gEriwabD8WT|U$=mX$z6hl*HffhYfDt;9GPW%Xi zTVwozHR4=+=mzWpr+OweX=(FMzR9vFe>U>Qn6o8^lCbwL;)mH9KkE2RFu>vryeLe| MbK7H%+s$D838WggiU0rr delta 1207 zcmcJOPfQa*6vq3?wzG8EZh_L)7F!A))Uq(U-K~_w7V#hlO~u$K;R4;I2NI3kO>F&> z5R3#eCS$k=2MR`_*a}UIg3)V@L~j0z7Y-&yxp^VRZ8Y(q#x>Eo%*&gZ?|Yeizg&*Z zy+czWK4kf^ywmN6!9RV@b}iAn|V(Bu*%Yc8c4NZQ%~jr6h6E6rH1f>Gy|paGZy*3yBIK^^EeSXp$V2;3X%daGz!zOxLIB5QNl}%UjAccXu!!R=ved1p z-6}4&%wA~gQsSzj8Am$Kii#Y|h?%UWiwCq3NsFplR*&P-)0=*mnTy3`J7yvlAyTS$ zg@um%2|(#^ek_pA#AL%?mPMSwny5&+Dt77Fn23iZeME~!yJRgx20mRjWitC(*keZU zKD-H;5nRPkF{fDGPJ&7E@^%V7m{VOH0%%!)DR8M=zlBE|7NOnw(wX3Acz@jt_naH3 z9ppUhEA|xH_*K4)&u)c(6tD@fbN$w0x|1|>LDI(r{}{A0Y~`>HFlO~Jj{M$c_^ALpq{WuT35UesW2)pRD0?5!@HfXp9Hox6F5UtKhuP;Jt-EMZ4B< zK`~x`UWAlE+>u+*3cm7abu-xdOAdR!Gsn1p;&B4V3ucqdq0ZlsNG4I^>M~S9rV{&q J@>LiD*H@^FQF{OY diff --git a/apps/your-burger-api/src/migrations/.snapshot-your-burger.sqlite3.json b/apps/your-burger-api/src/migrations/.snapshot-your-burger.sqlite3.json index eee623f..7996736 100644 --- a/apps/your-burger-api/src/migrations/.snapshot-your-burger.sqlite3.json +++ b/apps/your-burger-api/src/migrations/.snapshot-your-burger.sqlite3.json @@ -375,6 +375,16 @@ "nullable": false, "length": 36, "mappedType": "text" + }, + "brand_id": { + "name": "brand_id", + "type": "integer", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": null, + "mappedType": "integer" } }, "name": "burgers", @@ -399,6 +409,16 @@ "primary": false, "unique": false }, + { + "columnNames": [ + "brand_id" + ], + "composite": false, + "keyName": "burgers_brand_id_index", + "constraint": false, + "primary": false, + "unique": false + }, { "keyName": "primary", "columnNames": [ @@ -423,6 +443,18 @@ ], "referencedTableName": "burger_places", "updateRule": "cascade" + }, + "burgers_brand_id_foreign": { + "constraintName": "burgers_brand_id_foreign", + "columnNames": [ + "brand_id" + ], + "localTableName": "burgers", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "burger_brands", + "updateRule": "cascade" } }, "nativeEnums": {} diff --git a/apps/your-burger-api/src/migrations/Migration20241009214546.ts b/apps/your-burger-api/src/migrations/Migration20241015174703.ts similarity index 89% rename from apps/your-burger-api/src/migrations/Migration20241009214546.ts rename to apps/your-burger-api/src/migrations/Migration20241015174703.ts index 17e4fd7..1a78d36 100644 --- a/apps/your-burger-api/src/migrations/Migration20241009214546.ts +++ b/apps/your-burger-api/src/migrations/Migration20241015174703.ts @@ -1,6 +1,6 @@ import { Migration } from '@mikro-orm/migrations'; -export class Migration20241009214546 extends Migration { +export class Migration20241015174703 extends Migration { override async up(): Promise { this.addSql(`create table \`allergens\` (\`id\` integer not null primary key autoincrement, \`name\` text not null, \`i18n_key\` text not null);`); @@ -17,9 +17,10 @@ export class Migration20241009214546 extends Migration { this.addSql(`create index \`burger_places_brand_id_index\` on \`burger_places\` (\`brand_id\`);`); this.addSql(`create unique index \`burger_places_geo_id_unique\` on \`burger_places\` (\`geo_id\`);`); - this.addSql(`create table \`burgers\` (\`id\` text not null, \`name\` text not null, \`description\` text not null, \`type\` text check (\`type\` in ('CLASSIC', 'SMASH', 'CHICKEN', 'VEGAN')) not null, \`place_id\` text not null, constraint \`burgers_place_id_foreign\` foreign key(\`place_id\`) references \`burger_places\`(\`id\`) on update cascade, primary key (\`id\`));`); + this.addSql(`create table \`burgers\` (\`id\` text not null, \`name\` text not null, \`description\` text not null, \`type\` text check (\`type\` in ('CLASSIC', 'SMASH', 'CHICKEN', 'VEGAN')) not null, \`place_id\` text not null, \`brand_id\` integer not null, constraint \`burgers_place_id_foreign\` foreign key(\`place_id\`) references \`burger_places\`(\`id\`) on update cascade, constraint \`burgers_brand_id_foreign\` foreign key(\`brand_id\`) references \`burger_brands\`(\`id\`) on update cascade, primary key (\`id\`));`); this.addSql(`create unique index \`burgers_name_unique\` on \`burgers\` (\`name\`);`); this.addSql(`create index \`burgers_place_id_index\` on \`burgers\` (\`place_id\`);`); + this.addSql(`create index \`burgers_brand_id_index\` on \`burgers\` (\`brand_id\`);`); this.addSql(`create table \`burgers_allergens\` (\`burger_id\` text not null, \`allergen_id\` integer not null, constraint \`burgers_allergens_burger_id_foreign\` foreign key(\`burger_id\`) references \`burgers\`(\`id\`) on delete cascade on update cascade, constraint \`burgers_allergens_allergen_id_foreign\` foreign key(\`allergen_id\`) references \`allergens\`(\`id\`) on delete cascade on update cascade, primary key (\`burger_id\`, \`allergen_id\`));`); this.addSql(`create index \`burgers_allergens_burger_id_index\` on \`burgers_allergens\` (\`burger_id\`);`); diff --git a/apps/your-burger-api/src/seeders/initial-data/burger-places.ts b/apps/your-burger-api/src/seeders/initial-data/burger-places.ts index 26169bd..3137399 100644 --- a/apps/your-burger-api/src/seeders/initial-data/burger-places.ts +++ b/apps/your-burger-api/src/seeders/initial-data/burger-places.ts @@ -21,75 +21,87 @@ export class BurgerPlacesDatabaseSeeder extends DatabaseSeeder { } async seed(): Promise { + const entities = readJson(this.sourcePath) as any[]; + const burgerPlaces: BurgerPlace[] = []; + + for (const entity of entities) { + const burgerPlace = await this.getBurgerPlaces(entity); + burgerPlaces.push(burgerPlace); + } + + try { + this.entityManager.persistAndFlush(burgerPlaces); + console.info(`${this.entityClass.name} seeded successfully`); + } catch (err) { + console.error(`Error seeding ${this.entityClass.name}s:`, err); + } + } + + private async getBurgerPlaces(entity: any): Promise { const burgerBrandsRepository = this.entityManager.getRepository(BurgerBrand); const geoPointsRepository = this.entityManager.getRepository(GeoPoint); const burgerPlacesRepository = this.entityManager.getRepository(BurgerPlace); - const burgersRepository = this.entityManager.getRepository(Burger); - const allergensRepository = this.entityManager.getRepository(Allergen); - const ingredientsRepository = this.entityManager.getRepository(Ingredient); - - const burgerPlaces: BurgerPlace[] = []; - - const entities = readJson(this.sourcePath) as any[]; + const geo = geoPointsRepository.create({ + latitude: entity.geo.latitude, + longitude: entity.geo.latitude, + street: entity.geo.street, + }); - for (const entity of entities) { - const geo = geoPointsRepository.create({ - latitude: entity.geo.latitude, - longitude: entity.geo.latitude, - street: entity.geo.street, - }); + const brand = await burgerBrandsRepository.findOne({ + slug: entity.brand, + }); - const brand = await burgerBrandsRepository.findOne({ - slug: entity.brand, - }); + const burgers = this.getBurgersForBurgerPlace(entity, brand); - const burgers: Burger[] = []; + const burgerPlaceName = `${brand.name} - ${geo.street}`; + const burgerPlace = burgerPlacesRepository.create({ + id: uuid(), + name: burgerPlaceName, + slug: slugify(burgerPlaceName), + brand, + burgers, + geo, + }); - for (const burgerRaw of entity.burgers) { - const ingredients = ingredientsRepository.find({ - name: { - $in: burgerRaw.ingredients, - }, - }); + return burgerPlace; + } - const allergens = allergensRepository.find({ - name: { - $in: burgerRaw.ingredients, - }, - }); + private getBurgersForBurgerPlace( + burgerPlace: any, + brand: BurgerBrand + ): Burger[] { + const burgersRepository = this.entityManager.getRepository(Burger); + const allergensRepository = this.entityManager.getRepository(Allergen); + const ingredientsRepository = this.entityManager.getRepository(Ingredient); + const burgers: Burger[] = []; - const burger = burgersRepository.create({ - id: uuid(), - type: burgerRaw.type, - name: burgerRaw.name, - description: burgerRaw.description, - allergens, - ingredients, - }); + for (const burgerRaw of burgerPlace.burgers) { + const ingredients = ingredientsRepository.find({ + name: { + $in: burgerRaw.ingredients, + }, + }); - burgers.push(burger); - } + const allergens = allergensRepository.find({ + name: { + $in: burgerRaw.ingredients, + }, + }); - const burgerPlaceName = `${brand.name} - ${geo.street}`; - const burgerPlace = burgerPlacesRepository.create({ + const burger = burgersRepository.create({ id: uuid(), - name: burgerPlaceName, - slug: slugify(burgerPlaceName), + type: burgerRaw.type, + name: burgerRaw.name, + description: burgerRaw.description, brand, - burgers, - geo, + allergens, + ingredients, }); - burgerPlaces.push(burgerPlace); - } - - try { - this.entityManager.persistAndFlush(burgerPlaces); - console.info(`${this.entityClass.name}s seeded successfully`); - } catch (err) { - console.error(`Error seeding ${this.entityClass.name}s:`, err); + burgers.push(burger); } + return burgers; } } diff --git a/apps/your-burger-api/src/seeders/initial-data/ingredient-categories.ts b/apps/your-burger-api/src/seeders/initial-data/ingredient-categories.ts index 8168ff1..461eb8b 100644 --- a/apps/your-burger-api/src/seeders/initial-data/ingredient-categories.ts +++ b/apps/your-burger-api/src/seeders/initial-data/ingredient-categories.ts @@ -1,5 +1,4 @@ import { EntityManager } from '@mikro-orm/core'; -import { Ingredient } from '../../app/burger-business/domain/ingredient/ingredient'; import { DATA_FILES } from '../config'; import { seedEntities } from '../seed-entities'; import { DatabaseSeeder } from '../seeder'; diff --git a/apps/your-burger-api/src/seeders/seed-entities.ts b/apps/your-burger-api/src/seeders/seed-entities.ts index d828482..a2c6a71 100644 --- a/apps/your-burger-api/src/seeders/seed-entities.ts +++ b/apps/your-burger-api/src/seeders/seed-entities.ts @@ -19,10 +19,10 @@ export async function seedEntities( try { em.persistAndFlush(entities); - console.info(`${EntityClass.name}s seeded successfully`); + console.info(`${EntityClass.name} seeded successfully`); resolve(); } catch (err) { - console.error(`Error seeding ${EntityClass.name}s:`, err); + console.error(`Error seeding ${EntityClass.name}:`, err); reject(err); } });