From 9cd3431e53ba09d80bbea7e01efaa4ae66d56b29 Mon Sep 17 00:00:00 2001 From: bodyduardU Date: Wed, 8 Mar 2023 16:04:30 -0500 Subject: [PATCH] Providers_Module_test --- nestjs/src/app.module.ts | 6 ++-- nestjs/src/cats/cats.controller.ts | 45 +++++++++++--------------- nestjs/src/cats/cats.module.ts | 11 +++++++ nestjs/src/cats/cats.service.spec.ts | 18 +++++++++++ nestjs/src/cats/cats.service.ts | 15 +++++++++ nestjs/src/cats/create-cat.dto.ts | 5 +++ nestjs/src/interfaces/cat.interface.ts | 5 +++ 7 files changed, 77 insertions(+), 28 deletions(-) create mode 100644 nestjs/src/cats/cats.module.ts create mode 100644 nestjs/src/cats/cats.service.spec.ts create mode 100644 nestjs/src/cats/cats.service.ts create mode 100644 nestjs/src/cats/create-cat.dto.ts create mode 100644 nestjs/src/interfaces/cat.interface.ts diff --git a/nestjs/src/app.module.ts b/nestjs/src/app.module.ts index 7bc3188..ea4b424 100644 --- a/nestjs/src/app.module.ts +++ b/nestjs/src/app.module.ts @@ -2,10 +2,12 @@ import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { CatsController } from './cats/cats.controller'; +import { CatsService } from './cats/cats.service'; +import { CatsModule } from './cats/cats.module'; @Module({ - imports: [], - controllers: [AppController, CatsController], + imports: [CatsModule], + controllers: [AppController], providers: [AppService], }) export class AppModule {} diff --git a/nestjs/src/cats/cats.controller.ts b/nestjs/src/cats/cats.controller.ts index 44bb4cb..29a7a15 100644 --- a/nestjs/src/cats/cats.controller.ts +++ b/nestjs/src/cats/cats.controller.ts @@ -1,35 +1,28 @@ -import { Controller, Get, Req, Res, Post, HttpStatus } from '@nestjs/common'; -import { HttpCode, Param } from '@nestjs/common/decorators'; -// import { Request } from '@nestjs/common/decorators'; +import { + Controller, + Get, + Req, + Res, + Post, + Body, + HttpStatus, +} from '@nestjs/common'; + import { Request } from 'express'; +import { CatsService } from './cats.service'; +import { CreateCatDto } from './create-cat.dto'; +import { Cat } from 'src/interfaces/cat.interface'; + @Controller('cats') export class CatsController { + constructor(private catsService: CatsService) {} @Post() - create(): string { - return 'This action asd'; + async create(@Body() createCatDto: CreateCatDto) { + this.catsService.create(createCatDto); } @Get() - findAll(@Res({ passthrough: true }) res: Response) { - // res.status(HttpStatus.OK); - return ['123']; - } - - @Get(':id') - findOne(@Param('id') id: string): string { - return `this is ${id}`; + async findAll(): Promise { + return this.catsService.findAll(); } - // @Get(':id') - // findOne(@Param() params): string { - // console.log(params.id); - // return `123 ${params.id}`; - // } - // @Get('') - // findAll(@Req() request: Request): string { - // return 'asdasd'; - // } - // @Get() - // find(): string { - // return 'ok'; - // } } diff --git a/nestjs/src/cats/cats.module.ts b/nestjs/src/cats/cats.module.ts new file mode 100644 index 0000000..90dee6a --- /dev/null +++ b/nestjs/src/cats/cats.module.ts @@ -0,0 +1,11 @@ +import { Module } from '@nestjs/common'; +import { CatsController } from './cats.controller'; +import { CatsService } from './cats.service'; + +@Module({ + controllers: [CatsController], + providers: [CatsService], +}) +export class CatsModule { + constructor(private catsService: CatsService) {} +} diff --git a/nestjs/src/cats/cats.service.spec.ts b/nestjs/src/cats/cats.service.spec.ts new file mode 100644 index 0000000..0e9bf2b --- /dev/null +++ b/nestjs/src/cats/cats.service.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { CatsService } from './cats.service'; + +describe('CatsService', () => { + let service: CatsService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [CatsService], + }).compile(); + + service = module.get(CatsService); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); diff --git a/nestjs/src/cats/cats.service.ts b/nestjs/src/cats/cats.service.ts new file mode 100644 index 0000000..c33aa92 --- /dev/null +++ b/nestjs/src/cats/cats.service.ts @@ -0,0 +1,15 @@ +import { Injectable } from '@nestjs/common'; +import { Cat } from 'src/interfaces/cat.interface'; + +@Injectable() +export class CatsService { + private readonly cats: Cat[] = []; + + create(cat: Cat) { + this.cats.push(cat); + } + + findAll(): Cat[] { + return this.cats; + } +} diff --git a/nestjs/src/cats/create-cat.dto.ts b/nestjs/src/cats/create-cat.dto.ts new file mode 100644 index 0000000..492e2d9 --- /dev/null +++ b/nestjs/src/cats/create-cat.dto.ts @@ -0,0 +1,5 @@ +export class CreateCatDto { + name: string; + age: number; + breed: string; +} diff --git a/nestjs/src/interfaces/cat.interface.ts b/nestjs/src/interfaces/cat.interface.ts new file mode 100644 index 0000000..961c973 --- /dev/null +++ b/nestjs/src/interfaces/cat.interface.ts @@ -0,0 +1,5 @@ +export interface Cat { + name: string; + age: number; + breed: string; +}