Return proper http status codes on delete

This commit is contained in:
Arti Zirk 2017-06-12 16:23:33 +03:00
parent 41991a68dd
commit 1249950059
3 changed files with 30 additions and 9 deletions

View File

@ -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<FoorumDTO> SearchFoorumTitle(string query);

View File

@ -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<FoorumDTO> SearchFoorumAuthor(string query)

View File

@ -63,16 +63,25 @@ namespace WebApi.Controllers
}
// DELETE api/<controller>/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);
}
}
}