deletee method

This commit is contained in:
vigdorov
2020-07-03 21:50:30 +03:00
parent 6ca9059dc4
commit 7a56b148f5
3 changed files with 30 additions and 4 deletions

View File

@ -1,4 +1,4 @@
import { Controller, Get, Post, Body, Param, Header } from '@nestjs/common'; import { Controller, Get, Post, Body, Param, Header, Delete } from '@nestjs/common';
import {StoreService} from './store.service'; import {StoreService} from './store.service';
import {Store, StoreResponse, StoreRequest} from './store.schema'; import {Store, StoreResponse, StoreRequest} from './store.schema';
import {ApiResponse} from '@nestjs/swagger'; import {ApiResponse} from '@nestjs/swagger';
@ -21,7 +21,7 @@ export class StoreController {
return this.storeService.findAll(); return this.storeService.findAll();
} }
@Get(':key?') @Get(':key')
@Header(...ALLOW_ORIGIN_ALL) @Header(...ALLOW_ORIGIN_ALL)
@ApiResponse({ @ApiResponse({
status: 200, status: 200,
@ -42,4 +42,15 @@ export class StoreController {
async create(@Body() createStoreClass: StoreRequest): Promise<Store> { async create(@Body() createStoreClass: StoreRequest): Promise<Store> {
return this.storeService.create(createStoreClass); return this.storeService.create(createStoreClass);
} }
@Delete(':key')
@Header(...ALLOW_ORIGIN_ALL)
@ApiResponse({
status: 200,
description: 'Удаляет пару ключ-значение по ключу',
type: String,
})
async removeOne(@Param() {key}: {key: string}): Promise<string> {
return this.storeService.removeOne(key);
}
} }

View File

@ -1,5 +1,5 @@
import {Model} from 'mongoose'; import {Model} from 'mongoose';
import {Injectable} from '@nestjs/common'; import {Injectable, NotFoundException} from '@nestjs/common';
import {InjectModel} from '@nestjs/mongoose'; import {InjectModel} from '@nestjs/mongoose';
import {Store, StoreRequest} from './store.schema'; import {Store, StoreRequest} from './store.schema';
@ -26,4 +26,15 @@ export class StoreService {
async findOne(key: string): Promise<Store> { async findOne(key: string): Promise<Store> {
return this.storeModel.findOne({key}); return this.storeModel.findOne({key});
} }
async removeOne(key: string): Promise<string> {
const searchStore = await this.findOne(key);
if (searchStore) {
await searchStore.remove();
return 'ok';
}
throw new NotFoundException(`Not Found key - ${key}`);
}
} }

View File

@ -16,3 +16,7 @@ content-type: application/json
"age": 15 "age": 15
} }
} }
###
DELETE http://localhost:4001/store/testApi HTTP/1.1