2017-06-12 12:46:00 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Web.Http;
|
2017-06-12 15:26:34 +03:00
|
|
|
|
using System.Web.Http.Controllers;
|
2017-06-12 13:37:48 +03:00
|
|
|
|
using BL.DTOs;
|
|
|
|
|
using BL.Interfaces;
|
2017-06-12 12:46:00 +03:00
|
|
|
|
|
|
|
|
|
namespace WebApi.Controllers
|
|
|
|
|
{
|
|
|
|
|
public class FoorumController : ApiController
|
|
|
|
|
{
|
2017-06-12 13:37:48 +03:00
|
|
|
|
private IFoorumService _foorumService;
|
|
|
|
|
public FoorumController(IFoorumService foorumService)
|
|
|
|
|
{
|
|
|
|
|
_foorumService = foorumService;
|
|
|
|
|
}
|
2017-06-12 12:46:00 +03:00
|
|
|
|
// GET api/<controller>
|
2017-06-12 13:37:48 +03:00
|
|
|
|
public IEnumerable<FoorumDTO> Get()
|
2017-06-12 12:46:00 +03:00
|
|
|
|
{
|
2017-06-12 13:37:48 +03:00
|
|
|
|
return _foorumService.GetAll();
|
2017-06-12 12:46:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET api/<controller>/5
|
2017-06-12 14:43:31 +03:00
|
|
|
|
public IHttpActionResult Get(int id)
|
2017-06-12 12:46:00 +03:00
|
|
|
|
{
|
2017-06-12 14:43:31 +03:00
|
|
|
|
var f = _foorumService.Get(id);
|
|
|
|
|
if (f == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
return Ok(f);
|
2017-06-12 12:46:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// POST api/<controller>
|
2017-06-12 14:16:38 +03:00
|
|
|
|
public IHttpActionResult Post([FromBody]FoorumDTO foorum)
|
2017-06-12 12:46:00 +03:00
|
|
|
|
{
|
2017-06-12 14:16:38 +03:00
|
|
|
|
if (foorum == null)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var ret = _foorumService.Add(foorum);
|
|
|
|
|
if (ret != null)
|
|
|
|
|
{
|
|
|
|
|
return CreatedAtRoute("DefaultApi", new {controller = "Foorum", id = ret.FoorumId}, ret);
|
|
|
|
|
}
|
|
|
|
|
return BadRequest();
|
2017-06-12 12:46:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PUT api/<controller>/5
|
2017-06-12 15:46:53 +03:00
|
|
|
|
public IHttpActionResult Put(int id, [FromBody]FoorumDTO foorum)
|
2017-06-12 12:46:00 +03:00
|
|
|
|
{
|
2017-06-12 15:46:53 +03:00
|
|
|
|
foorum.FoorumId = id;
|
2017-06-12 16:15:34 +03:00
|
|
|
|
var updatedFoorum = _foorumService.Update(foorum);
|
|
|
|
|
if (updatedFoorum == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
return Ok(updatedFoorum);
|
2017-06-12 12:46:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DELETE api/<controller>/5
|
2017-06-12 16:23:33 +03:00
|
|
|
|
public IHttpActionResult Delete(int id, [FromUri]bool permanent=false)
|
2017-06-12 12:46:00 +03:00
|
|
|
|
{
|
2017-06-12 16:23:33 +03:00
|
|
|
|
FoorumDTO f = null;
|
|
|
|
|
|
2017-06-12 16:15:34 +03:00
|
|
|
|
if (permanent)
|
|
|
|
|
{
|
2017-06-12 16:23:33 +03:00
|
|
|
|
f =_foorumService.Delete(id);
|
2017-06-12 16:15:34 +03:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-06-12 16:23:33 +03:00
|
|
|
|
f = _foorumService.Hide(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (f == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Ok(f);
|
2017-06-12 12:46:00 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|