From 124995005929a7625a317edc13a82fd84e24dac9 Mon Sep 17 00:00:00 2001 From: Arti Zirk Date: Mon, 12 Jun 2017 16:23:33 +0300 Subject: [PATCH] Return proper http status codes on delete --- BL/Interfaces/IFoorumService.cs | 2 +- BL/Services/FoorumService.cs | 20 ++++++++++++++++---- WebApi/Controllers/FoorumController.cs | 17 +++++++++++++---- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/BL/Interfaces/IFoorumService.cs b/BL/Interfaces/IFoorumService.cs index faed0d5..f6071e6 100644 --- a/BL/Interfaces/IFoorumService.cs +++ b/BL/Interfaces/IFoorumService.cs @@ -14,7 +14,7 @@ namespace BL.Interfaces FoorumDTO Add(FoorumDTO foorum); FoorumDTO Update(FoorumDTO foorum); void Hide(int id, bool hide=true); - void Delete(int id); + FoorumDTO Delete(int id); void AddPost(int id, PostDTO post); IEnumerable SearchFoorumTitle(string query); diff --git a/BL/Services/FoorumService.cs b/BL/Services/FoorumService.cs index 889915f..104943f 100644 --- a/BL/Services/FoorumService.cs +++ b/BL/Services/FoorumService.cs @@ -30,11 +30,17 @@ namespace BL.Services throw new NotImplementedException(); } - public void Delete(int id) + public FoorumDTO Delete(int id) { var f = _foorumRepository.GetById(id); + if (f == null) + { + return null; + } _foorumRepository.Delete(f); _foorumRepository.SaveChanges(); + + return _factory.Create(f); } public FoorumDTO Get(int id) @@ -48,16 +54,22 @@ namespace BL.Services foreach (var f in _foorumRepository.GetAll()) { yield return _factory.Create(f); - } - + } } - public void Hide(int id, bool hide=true) + public FoorumDTO Hide(int id, bool hide=true) { var f = _foorumRepository.GetById(id); + if (f == null) + { + return null; + } + f.Visible = !hide; _foorumRepository.Update(f); _foorumRepository.SaveChanges(); + + return _factory.Create(f); } public IEnumerable SearchFoorumAuthor(string query) diff --git a/WebApi/Controllers/FoorumController.cs b/WebApi/Controllers/FoorumController.cs index 1325160..dd1eaba 100644 --- a/WebApi/Controllers/FoorumController.cs +++ b/WebApi/Controllers/FoorumController.cs @@ -63,16 +63,25 @@ namespace WebApi.Controllers } // DELETE api//5 - public void Delete(int id, [FromUri]bool permanent=false) + public IHttpActionResult Delete(int id, [FromUri]bool permanent=false) { + FoorumDTO f = null; + if (permanent) { - _foorumService.Delete(id); + f =_foorumService.Delete(id); } else { - _foorumService.Hide(id); - } + f = _foorumService.Hide(id); + } + + if (f == null) + { + return NotFound(); + } + + return Ok(f); } } } \ No newline at end of file