Skip to content

Commit

Permalink
feat: add IngredientCategoriesController
Browse files Browse the repository at this point in the history
  • Loading branch information
rjlopezdev committed Oct 11, 2024
1 parent e7e25ad commit 958c9ad
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 6 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 @@ -22,6 +22,18 @@
}
}
},
"build": {
"options": {
"transformers": [
{
"name": "@nestjs/swagger/plugin",
"options": {
"introspectComments": true
}
}
]
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Ingredient } from './ingredient';
import { IngredientCategory } from './ingredient-category';

export abstract class IngredientsRepository {
abstract find(): Promise<Ingredient[]>;
abstract findCategories(): Promise<IngredientCategory[]>;
abstract create(ingredient: Ingredient): Promise<Ingredient>;
}
Original file line number Diff line number Diff line change
@@ -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<IngredientCategory[]> {
return this.ingredientsRepository.findCategories();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export class BurgersMikroOrmRepository implements BurgersRepository {
constructor(private readonly em: EntityManager) {}

async find(): Promise<Burger[]> {
return await this.repository.findAll();
return await this.repository.findAll({
populate: ['place', 'ingredients', 'allergens'],
});
}

async create(burger: Burger): Promise<Burger> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Ingredient[]> {
return await this.repository.findAll();
return await this.ingredientRepository.findAll();
}

async create(ingredient: Ingredient): Promise<Ingredient> {
return this.repository.create(ingredient);
return this.ingredientRepository.create(ingredient);
}

async findCategories(): Promise<IngredientCategory[]> {
return this.ingredientCategoriesRepository.findAll();
}
}
6 changes: 4 additions & 2 deletions apps/your-burger-api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 958c9ad

Please sign in to comment.