From ec3ba220e1f71c36e4c0824093e74fa258c73886 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Mon, 5 Sep 2016 11:51:47 +0200 Subject: [PATCH] implements a tags api controller --- Yavsc/ApiControllers/TagsApiController.cs | 146 ++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 Yavsc/ApiControllers/TagsApiController.cs diff --git a/Yavsc/ApiControllers/TagsApiController.cs b/Yavsc/ApiControllers/TagsApiController.cs new file mode 100644 index 00000000..088a22cd --- /dev/null +++ b/Yavsc/ApiControllers/TagsApiController.cs @@ -0,0 +1,146 @@ +using System.Collections.Generic; +using System.Linq; +using Microsoft.AspNet.Http; +using Microsoft.AspNet.Mvc; +using Microsoft.Data.Entity; +using Yavsc.Models; + +namespace Yavsc.Controllers +{ + [Produces("application/json")] + [Route("api/TagsApi")] + public class TagsApiController : Controller + { + private ApplicationDbContext _context; + + public TagsApiController(ApplicationDbContext context) + { + _context = context; + } + + // GET: api/TagsApi + [HttpGet] + public IEnumerable GetTag() + { + return _context.Tags; + } + + // GET: api/TagsApi/5 + [HttpGet("{id}", Name = "GetTag")] + public IActionResult GetTag([FromRoute] long id) + { + if (!ModelState.IsValid) + { + return HttpBadRequest(ModelState); + } + + Tag tag = _context.Tags.Single(m => m.Id == id); + + if (tag == null) + { + return HttpNotFound(); + } + + return Ok(tag); + } + + // PUT: api/TagsApi/5 + [HttpPut("{id}")] + public IActionResult PutTag(long id, [FromBody] Tag tag) + { + if (!ModelState.IsValid) + { + return HttpBadRequest(ModelState); + } + + if (id != tag.Id) + { + return HttpBadRequest(); + } + + _context.Entry(tag).State = EntityState.Modified; + + try + { + _context.SaveChanges(); + } + catch (DbUpdateConcurrencyException) + { + if (!TagExists(id)) + { + return HttpNotFound(); + } + else + { + throw; + } + } + + return new HttpStatusCodeResult(StatusCodes.Status204NoContent); + } + + // POST: api/TagsApi + [HttpPost] + public IActionResult PostTag([FromBody] Tag tag) + { + if (!ModelState.IsValid) + { + return HttpBadRequest(ModelState); + } + + _context.Tags.Add(tag); + try + { + _context.SaveChanges(); + } + catch (DbUpdateException) + { + if (TagExists(tag.Id)) + { + return new HttpStatusCodeResult(StatusCodes.Status409Conflict); + } + else + { + throw; + } + } + + return CreatedAtRoute("GetTag", new { id = tag.Id }, tag); + } + + // DELETE: api/TagsApi/5 + [HttpDelete("{id}")] + public IActionResult DeleteTag(long id) + { + if (!ModelState.IsValid) + { + return HttpBadRequest(ModelState); + } + + Tag tag = _context.Tags.Single(m => m.Id == id); + if (tag == null) + { + return HttpNotFound(); + } + + _context.Tags.Remove(tag); + _context.SaveChanges(); + + return Ok(tag); + } + + protected override void Dispose(bool disposing) + { + if (disposing) + { + _context.Dispose(); + } + base.Dispose(disposing); + } + + private bool TagExists(long id) + { + return _context.Tags.Count(e => e.Id == id) > 0; + } + } +} \ No newline at end of file