From 958c9adf9674f5f355f4764773567403bf7da72b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Juli=C3=A1n=20L=C3=B3pez=20Ca=C3=B1a?= Date: Fri, 11 Oct 2024 09:23:14 +0200 Subject: [PATCH] feat: add `IngredientCategoriesController` --- apps/your-burger-api/project.json | 12 ++++++++++++ .../burger-business/burger-business.module.ts | 2 ++ .../domain/ingredient/ingredients.repository.ts | 2 ++ .../ingredient-categories.controller.ts | 16 ++++++++++++++++ .../burger/burgers-mikro-orm.repository.ts | 4 +++- .../ingredient-mikro-orm.repository.ts | 16 +++++++++++++--- apps/your-burger-api/src/main.ts | 6 ++++-- package-lock.json | 7 +++++++ package.json | 1 + 9 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 apps/your-burger-api/src/app/burger-business/infra/http/ingredient/ingredient-categories.controller.ts diff --git a/apps/your-burger-api/project.json b/apps/your-burger-api/project.json index a7f8dfb..9fdfdf0 100644 --- a/apps/your-burger-api/project.json +++ b/apps/your-burger-api/project.json @@ -22,6 +22,18 @@ } } }, + "build": { + "options": { + "transformers": [ + { + "name": "@nestjs/swagger/plugin", + "options": { + "introspectComments": true + } + } + ] + } + }, "test": { "executor": "@nx/jest:jest", "outputs": ["{workspaceRoot}/coverage/{projectRoot}"], diff --git a/apps/your-burger-api/src/app/burger-business/burger-business.module.ts b/apps/your-burger-api/src/app/burger-business/burger-business.module.ts index 018e385..23fb3df 100644 --- a/apps/your-burger-api/src/app/burger-business/burger-business.module.ts +++ b/apps/your-burger-api/src/app/burger-business/burger-business.module.ts @@ -14,12 +14,14 @@ import { BurgerPlacesMikroOrmRepository } from './infra/persistence/burger-place import { BurgersRepository } from './domain/burgers.repository'; import { BurgersMikroOrmRepository } from './infra/persistence/burger/burgers-mikro-orm.repository'; import { BurgersController } from './infra/http/burger/burgers.controller'; +import { IngredientCategoriesController } from './infra/http/ingredient/ingredient-categories.controller'; @Module({ controllers: [ BurgersController, AllergensController, IngredientsController, + IngredientCategoriesController, BurgerPlacesController, BurgerBrandsController, ], diff --git a/apps/your-burger-api/src/app/burger-business/domain/ingredient/ingredients.repository.ts b/apps/your-burger-api/src/app/burger-business/domain/ingredient/ingredients.repository.ts index a192e16..f147f0c 100644 --- a/apps/your-burger-api/src/app/burger-business/domain/ingredient/ingredients.repository.ts +++ b/apps/your-burger-api/src/app/burger-business/domain/ingredient/ingredients.repository.ts @@ -1,6 +1,8 @@ import { Ingredient } from './ingredient'; +import { IngredientCategory } from './ingredient-category'; export abstract class IngredientsRepository { abstract find(): Promise; + abstract findCategories(): Promise; abstract create(ingredient: Ingredient): Promise; } diff --git a/apps/your-burger-api/src/app/burger-business/infra/http/ingredient/ingredient-categories.controller.ts b/apps/your-burger-api/src/app/burger-business/infra/http/ingredient/ingredient-categories.controller.ts new file mode 100644 index 0000000..d590c95 --- /dev/null +++ b/apps/your-burger-api/src/app/burger-business/infra/http/ingredient/ingredient-categories.controller.ts @@ -0,0 +1,16 @@ +import { Controller, Get } from '@nestjs/common'; +import { IngredientsRepository } from '../../../domain/ingredient/ingredients.repository'; +import { ApiTags } from '@nestjs/swagger'; +import { API_TAGS } from '../../../../../open-api'; +import { IngredientCategory } from '../../../domain/ingredient/ingredient-category'; + +@Controller('ingredients/categories') +@ApiTags(API_TAGS.INGREDIENTS) +export class IngredientCategoriesController { + constructor(private readonly ingredientsRepository: IngredientsRepository) {} + + @Get() + public async getIngredients(): Promise { + return this.ingredientsRepository.findCategories(); + } +} diff --git a/apps/your-burger-api/src/app/burger-business/infra/persistence/burger/burgers-mikro-orm.repository.ts b/apps/your-burger-api/src/app/burger-business/infra/persistence/burger/burgers-mikro-orm.repository.ts index 55583c4..e56231f 100644 --- a/apps/your-burger-api/src/app/burger-business/infra/persistence/burger/burgers-mikro-orm.repository.ts +++ b/apps/your-burger-api/src/app/burger-business/infra/persistence/burger/burgers-mikro-orm.repository.ts @@ -10,7 +10,9 @@ export class BurgersMikroOrmRepository implements BurgersRepository { constructor(private readonly em: EntityManager) {} async find(): Promise { - return await this.repository.findAll(); + return await this.repository.findAll({ + populate: ['place', 'ingredients', 'allergens'], + }); } async create(burger: Burger): Promise { diff --git a/apps/your-burger-api/src/app/burger-business/infra/persistence/ingredient/ingredient-mikro-orm.repository.ts b/apps/your-burger-api/src/app/burger-business/infra/persistence/ingredient/ingredient-mikro-orm.repository.ts index 105469a..7e5c805 100644 --- a/apps/your-burger-api/src/app/burger-business/infra/persistence/ingredient/ingredient-mikro-orm.repository.ts +++ b/apps/your-burger-api/src/app/burger-business/infra/persistence/ingredient/ingredient-mikro-orm.repository.ts @@ -3,17 +3,27 @@ import { Injectable } from '@nestjs/common'; import { Ingredient } from '../../../domain/ingredient/ingredient'; import { IngredientSchema } from './ingredient.schema'; import { IngredientsRepository } from '../../../domain/ingredient/ingredients.repository'; +import { IngredientCategory } from '../../../domain/ingredient/ingredient-category'; +import { IngredientCategorySchema } from './ingredient-category.schema'; @Injectable() export class IngredientsMikroOrmRepository implements IngredientsRepository { - private repository = this.em.getRepository(IngredientSchema); + private ingredientRepository = this.em.getRepository(IngredientSchema); + private ingredientCategoriesRepository = this.em.getRepository( + IngredientCategorySchema + ); + constructor(private readonly em: EntityManager) {} async find(): Promise { - return await this.repository.findAll(); + return await this.ingredientRepository.findAll(); } async create(ingredient: Ingredient): Promise { - return this.repository.create(ingredient); + return this.ingredientRepository.create(ingredient); + } + + async findCategories(): Promise { + return this.ingredientCategoriesRepository.findAll(); } } diff --git a/apps/your-burger-api/src/main.ts b/apps/your-burger-api/src/main.ts index d29b73f..ac9e35e 100644 --- a/apps/your-burger-api/src/main.ts +++ b/apps/your-burger-api/src/main.ts @@ -3,8 +3,8 @@ * This is only a minimal backend to get started. */ -import { Logger } from '@nestjs/common'; -import { NestFactory } from '@nestjs/core'; +import { ClassSerializerInterceptor, Logger } from '@nestjs/common'; +import { NestFactory, Reflector } from '@nestjs/core'; import { AppModule } from './app/app.module'; import { setupOpenApi } from './open-api'; @@ -14,6 +14,8 @@ async function bootstrap() { const globalPrefix = 'api'; app.setGlobalPrefix(globalPrefix); const port = process.env.PORT || 3000; + app.useGlobalInterceptors(new ClassSerializerInterceptor(Reflector)); + setupOpenApi(app); await app.listen(port); diff --git a/package-lock.json b/package-lock.json index fbd60a6..5cab358 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,6 +26,7 @@ "@nestjs/platform-express": "^10.0.2", "@nestjs/swagger": "^7.4.2", "axios": "^1.6.0", + "class-transformer": "^0.5.1", "reflect-metadata": "^0.1.13", "rxjs": "~7.8.0", "uuid": "^10.0.0" @@ -12029,6 +12030,12 @@ "dev": true, "license": "MIT" }, + "node_modules/class-transformer": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/class-transformer/-/class-transformer-0.5.1.tgz", + "integrity": "sha512-SQa1Ws6hUbfC98vKGxZH3KFY0Y1lm5Zm0SY8XX9zbK7FJCyVEac3ATW0RIpwzW+oOfmHE5PMPufDG9hCfoEOMw==", + "license": "MIT" + }, "node_modules/clean-stack": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/clean-stack/-/clean-stack-2.2.0.tgz", diff --git a/package.json b/package.json index 4a059a6..aba8111 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "@nestjs/platform-express": "^10.0.2", "@nestjs/swagger": "^7.4.2", "axios": "^1.6.0", + "class-transformer": "^0.5.1", "reflect-metadata": "^0.1.13", "rxjs": "~7.8.0", "uuid": "^10.0.0"