Skip to content

Commit

Permalink
feat: model improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
rjlopezdev committed Oct 16, 2024
1 parent 3747f75 commit 6baceb2
Show file tree
Hide file tree
Showing 18 changed files with 205 additions and 69 deletions.
12 changes: 12 additions & 0 deletions apps/your-burger-api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class BurgerBrandItemDto {
name: string;
slug: string;
}
Original file line number Diff line number Diff line change
@@ -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<BurgerItemDto>) {
Object.assign(this, partial);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './paginated-response';
export * from './paginated-response.dto';
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Type } from 'class-transformer';

export class PaginatedResponseDto<T> {
@Type(() => Array<T>)
data: T[];
}
Original file line number Diff line number Diff line change
@@ -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 extends Type<unknown>>(
dataDto: DataDto
) =>
applyDecorators(
ApiExtraModels(PaginatedResponseDto, dataDto),
ApiOkResponse({
schema: {
allOf: [
{ $ref: getSchemaPath(PaginatedResponseDto) },
{
properties: {
data: {
type: 'array',
items: { $ref: getSchemaPath(dataDto) },
},
},
},
],
},
})
);
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
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)
export class BurgersController {
constructor(private readonly burgersRepository: BurgersRepository) {}

@Get()
public async getBurgers(): Promise<Burger[]> {
return this.burgersRepository.find();
@ApiOkResponsePaginated(BurgerItemDto)
public async getBurgers(): Promise<PaginatedResponseDto<BurgerItemDto>> {
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,
})
),
};
}
}
Original file line number Diff line number Diff line change
@@ -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<BurgerBrand>({
class: BurgerBrand,
Expand All @@ -9,10 +10,15 @@ export const BurgerBrandSchema = new EntitySchema<BurgerBrand>({
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,
},
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const BurgerPlaceSchema = new EntitySchema<BurgerPlace>({
brand: {
kind: 'm:1',
entity: () => BurgerBrand,
inversedBy: (brand) => brand.burgerPlace,
inversedBy: (brand) => brand.place,
},
geo: { kind: '1:1', entity: () => GeoPoint },
burgers: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Burger>({
class: Burger,
Expand All @@ -12,6 +13,16 @@ export const BurgerSchema = new EntitySchema<Burger>({
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,
Expand All @@ -22,10 +33,5 @@ export const BurgerSchema = new EntitySchema<Burger>({
entity: () => Allergen,
nullable: true,
},
place: {
kind: 'm:1',
entity: () => BurgerPlace,
inversedBy: 'burgers',
},
},
});
Binary file modified apps/your-burger-api/src/assets/your-burger.sqlite3
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": [
Expand All @@ -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": {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Migration } from '@mikro-orm/migrations';

export class Migration20241009214546 extends Migration {
export class Migration20241015174703 extends Migration {

override async up(): Promise<void> {
this.addSql(`create table \`allergens\` (\`id\` integer not null primary key autoincrement, \`name\` text not null, \`i18n_key\` text not null);`);
Expand All @@ -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\`);`);
Expand Down
Loading

0 comments on commit 6baceb2

Please sign in to comment.