vreksam/WebApi/Controllers/FoorumController.cs

97 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Controllers;
using BL.DTOs;
using BL.Interfaces;
namespace WebApi.Controllers
{
public class FoorumController : ApiController
{
private IFoorumService _foorumService;
public FoorumController(IFoorumService foorumService)
{
_foorumService = foorumService;
}
// GET api/<controller>
public IEnumerable<FoorumDTO> Get()
{
return _foorumService.GetAll();
}
// GET api/<controller>/5
public IHttpActionResult Get(int id)
{
var f = _foorumService.Get(id);
if (f == null)
{
return NotFound();
}
return Ok(f);
}
// POST api/<controller>
public IHttpActionResult Post([FromBody]FoorumDTO foorum)
{
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();
}
// PUT api/<controller>/5
public IHttpActionResult Put(int id, [FromBody]FoorumDTO foorum)
{
foorum.FoorumId = id;
var updatedFoorum = _foorumService.Update(foorum);
if (updatedFoorum == null)
{
return NotFound();
}
return Ok(updatedFoorum);
}
// DELETE api/<controller>/5
public IHttpActionResult Delete(int id, [FromUri]bool permanent=false)
{
FoorumDTO f = null;
if (permanent)
{
f =_foorumService.Delete(id);
}
else
{
f = _foorumService.Hide(id);
}
if (f == null)
{
return NotFound();
}
return Ok(f);
}
public IHttpActionResult PostPost(int id, [FromBody] PostDTO post)
{
var p = _foorumService.AddPost(id, post);
if (p == null)
{
return NotFound();
}
return Ok(p);
}
}
}