From 7a56b148f5611b4bbc448d7e9b1ecc0f56a30f1f Mon Sep 17 00:00:00 2001 From: vigdorov Date: Fri, 3 Jul 2020 21:50:30 +0300 Subject: [PATCH] deletee method --- src/store/store.controller.ts | 15 +++++++++++++-- src/store/store.service.ts | 13 ++++++++++++- store.http | 6 +++++- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/store/store.controller.ts b/src/store/store.controller.ts index 0663222..fcf4d49 100644 --- a/src/store/store.controller.ts +++ b/src/store/store.controller.ts @@ -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 {Store, StoreResponse, StoreRequest} from './store.schema'; import {ApiResponse} from '@nestjs/swagger'; @@ -21,7 +21,7 @@ export class StoreController { return this.storeService.findAll(); } - @Get(':key?') + @Get(':key') @Header(...ALLOW_ORIGIN_ALL) @ApiResponse({ status: 200, @@ -42,4 +42,15 @@ export class StoreController { async create(@Body() createStoreClass: StoreRequest): Promise { return this.storeService.create(createStoreClass); } + + @Delete(':key') + @Header(...ALLOW_ORIGIN_ALL) + @ApiResponse({ + status: 200, + description: 'Удаляет пару ключ-значение по ключу', + type: String, + }) + async removeOne(@Param() {key}: {key: string}): Promise { + return this.storeService.removeOne(key); + } } diff --git a/src/store/store.service.ts b/src/store/store.service.ts index e2b5977..7f4bab7 100644 --- a/src/store/store.service.ts +++ b/src/store/store.service.ts @@ -1,5 +1,5 @@ import {Model} from 'mongoose'; -import {Injectable} from '@nestjs/common'; +import {Injectable, NotFoundException} from '@nestjs/common'; import {InjectModel} from '@nestjs/mongoose'; import {Store, StoreRequest} from './store.schema'; @@ -26,4 +26,15 @@ export class StoreService { async findOne(key: string): Promise { return this.storeModel.findOne({key}); } + + async removeOne(key: string): Promise { + const searchStore = await this.findOne(key); + + if (searchStore) { + await searchStore.remove(); + return 'ok'; + } + + throw new NotFoundException(`Not Found key - ${key}`); + } } \ No newline at end of file diff --git a/store.http b/store.http index 1aac84a..bcffff4 100644 --- a/store.http +++ b/store.http @@ -15,4 +15,8 @@ content-type: application/json "name": "ivan", "age": 15 } -} \ No newline at end of file +} + +### + +DELETE http://localhost:4001/store/testApi HTTP/1.1 \ No newline at end of file