wip first chat
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { CoffeesController } from './coffees.controller';
|
||||
|
||||
describe('CoffeesController', () => {
|
||||
let controller: CoffeesController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [CoffeesController],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<CoffeesController>(CoffeesController);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
});
|
||||
45
tests_hugo/nest_course/src/coffees/coffees.controller.ts
Normal file
45
tests_hugo/nest_course/src/coffees/coffees.controller.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Param,
|
||||
Post,
|
||||
Body,
|
||||
HttpCode,
|
||||
HttpStatus,
|
||||
Patch,
|
||||
Delete,
|
||||
Query
|
||||
} from '@nestjs/common';
|
||||
import { CoffeesService } from './coffees.service';
|
||||
|
||||
@Controller('coffees')
|
||||
export class CoffeesController {
|
||||
constructor(private readonly coffeesService: CoffeesService) {}
|
||||
|
||||
@Get()
|
||||
findAll(@Query() paginationQuery) {
|
||||
// const { limit, offset } = paginationQuery;
|
||||
return this.coffeesService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.coffeesService.findOne(id);
|
||||
}
|
||||
|
||||
@Post()
|
||||
//@HttpCode(HttpStatus.GONE)
|
||||
create(@Body() body) {
|
||||
return this.coffeesService.create(body);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() body) {
|
||||
return this.coffeesService.update(id, body);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.coffeesService.remove(id);
|
||||
}
|
||||
}
|
||||
18
tests_hugo/nest_course/src/coffees/coffees.service.spec.ts
Normal file
18
tests_hugo/nest_course/src/coffees/coffees.service.spec.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { CoffeesService } from './coffees.service';
|
||||
|
||||
describe('CoffeesService', () => {
|
||||
let service: CoffeesService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [CoffeesService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<CoffeesService>(CoffeesService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
42
tests_hugo/nest_course/src/coffees/coffees.service.ts
Normal file
42
tests_hugo/nest_course/src/coffees/coffees.service.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Coffee } from './entities/coffee.entity';
|
||||
|
||||
@Injectable()
|
||||
export class CoffeesService {
|
||||
|
||||
private coffees: Coffee[] = [
|
||||
{
|
||||
id: 1,
|
||||
name: 'arabica',
|
||||
brand: 'illy',
|
||||
flavors: ['chocolate', 'vanilla'],
|
||||
}
|
||||
];
|
||||
|
||||
findAll() {
|
||||
return this.coffees;
|
||||
}
|
||||
|
||||
findOne(id: string) {
|
||||
return this.coffees.find(item => item.id === +id);
|
||||
}
|
||||
|
||||
create(createCoffeeDto: any) {
|
||||
this.coffees.push(createCoffeeDto);
|
||||
}
|
||||
|
||||
update(id: string, updateCoffeeDto: any) {
|
||||
const existingCoffee = this.findOne(id);
|
||||
if (existingCoffee) {
|
||||
// update the existing entity
|
||||
}
|
||||
}
|
||||
|
||||
remove(id: string) {
|
||||
const coffeeIndex = this.coffees.findIndex(item => item.id === +id);
|
||||
if (coffeeIndex >= 0) {
|
||||
this.coffees.splice(coffeeIndex, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
export class Coffee {
|
||||
id: number;
|
||||
name: string;
|
||||
brand: string;
|
||||
flavors: string[];
|
||||
}
|
||||
Reference in New Issue
Block a user