using System.Collections.Generic; using System.Linq; using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNet.Http; using Microsoft.AspNet.Mvc; using Microsoft.Data.Entity; using Yavsc.Models; using Yavsc.Models.Blog; namespace Yavsc.Controllers { [Produces("application/json")] [Route("api/blogcomments")] public class CommentsApiController : Controller { private ApplicationDbContext _context; public CommentsApiController(ApplicationDbContext context) { _context = context; } // GET: api/CommentsApi [HttpGet] public IEnumerable GetComment() { return _context.Comment; } // GET: api/CommentsApi/5 [HttpGet("{id}", Name = "GetComment")] public async Task GetComment([FromRoute] long id) { if (!ModelState.IsValid) { return HttpBadRequest(ModelState); } Comment comment = await _context.Comment.SingleAsync(m => m.Id == id); if (comment == null) { return HttpNotFound(); } return Ok(comment); } // PUT: api/CommentsApi/5 [HttpPut("{id}")] public async Task PutComment([FromRoute] long id, [FromBody] Comment comment) { if (!ModelState.IsValid) { return HttpBadRequest(ModelState); } if (id != comment.Id) { return HttpBadRequest(); } _context.Entry(comment).State = EntityState.Modified; try { await _context.SaveChangesAsync(User.GetUserId()); } catch (DbUpdateConcurrencyException) { if (!CommentExists(id)) { return HttpNotFound(); } else { throw; } } return new HttpStatusCodeResult(StatusCodes.Status204NoContent); } // POST: api/CommentsApi [HttpPost] public async Task PostComment([FromBody] Comment comment) { if (!ModelState.IsValid) { return new BadRequestObjectResult(ModelState); } _context.Comment.Add(comment); try { await _context.SaveChangesAsync(User.GetUserId()); } catch (DbUpdateException) { if (CommentExists(comment.Id)) { return new HttpStatusCodeResult(StatusCodes.Status409Conflict); } else { throw; } } return CreatedAtRoute("GetComment", new { id = comment.Id }, comment); } // DELETE: api/CommentsApi/5 [HttpDelete("{id}")] public async Task DeleteComment([FromRoute] long id) { if (!ModelState.IsValid) { return HttpBadRequest(ModelState); } Comment comment = await _context.Comment.SingleAsync(m => m.Id == id); if (comment == null) { return HttpNotFound(); } _context.Comment.Remove(comment); await _context.SaveChangesAsync(User.GetUserId()); return Ok(comment); } protected override void Dispose(bool disposing) { if (disposing) { _context.Dispose(); } base.Dispose(disposing); } private bool CommentExists(long id) { return _context.Comment.Count(e => e.Id == id) > 0; } } }