diff --git a/src/Yavsc/ApiControllers/LiveApiController.cs b/src/Yavsc/ApiControllers/LiveApiController.cs new file mode 100644 index 00000000..aa2b041b --- /dev/null +++ b/src/Yavsc/ApiControllers/LiveApiController.cs @@ -0,0 +1,148 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNet.Http; +using Microsoft.AspNet.Mvc; +using Microsoft.Data.Entity; +using Yavsc.Models; +using Yavsc.Models.Streaming; + +namespace Yavsc.Controllers +{ + [Produces("application/json")] + [Route("api/LiveApi")] + public class LiveApiController : Controller + { + private ApplicationDbContext _context; + + public LiveApiController(ApplicationDbContext context) + { + _context = context; + } + + // GET: api/LiveApi + [HttpGet] + public IEnumerable GetLiveFlow() + { + return _context.LiveFlow; + } + + // GET: api/LiveApi/5 + [HttpGet("{id}", Name = "GetLiveFlow")] + public async Task GetLiveFlow([FromRoute] long id) + { + if (!ModelState.IsValid) + { + return HttpBadRequest(ModelState); + } + + LiveFlow liveFlow = await _context.LiveFlow.SingleAsync(m => m.Id == id); + + if (liveFlow == null) + { + return HttpNotFound(); + } + + return Ok(liveFlow); + } + + // PUT: api/LiveApi/5 + [HttpPut("{id}")] + public async Task PutLiveFlow([FromRoute] long id, [FromBody] LiveFlow liveFlow) + { + if (!ModelState.IsValid) + { + return HttpBadRequest(ModelState); + } + + if (id != liveFlow.Id) + { + return HttpBadRequest(); + } + + _context.Entry(liveFlow).State = EntityState.Modified; + + try + { + await _context.SaveChangesAsync(); + } + catch (DbUpdateConcurrencyException) + { + if (!LiveFlowExists(id)) + { + return HttpNotFound(); + } + else + { + throw; + } + } + + return new HttpStatusCodeResult(StatusCodes.Status204NoContent); + } + + // POST: api/LiveApi + [HttpPost] + public async Task PostLiveFlow([FromBody] LiveFlow liveFlow) + { + if (!ModelState.IsValid) + { + return HttpBadRequest(ModelState); + } + + _context.LiveFlow.Add(liveFlow); + try + { + await _context.SaveChangesAsync(); + } + catch (DbUpdateException) + { + if (LiveFlowExists(liveFlow.Id)) + { + return new HttpStatusCodeResult(StatusCodes.Status409Conflict); + } + else + { + throw; + } + } + + return CreatedAtRoute("GetLiveFlow", new { id = liveFlow.Id }, liveFlow); + } + + // DELETE: api/LiveApi/5 + [HttpDelete("{id}")] + public async Task DeleteLiveFlow([FromRoute] long id) + { + if (!ModelState.IsValid) + { + return HttpBadRequest(ModelState); + } + + LiveFlow liveFlow = await _context.LiveFlow.SingleAsync(m => m.Id == id); + if (liveFlow == null) + { + return HttpNotFound(); + } + + _context.LiveFlow.Remove(liveFlow); + await _context.SaveChangesAsync(); + + return Ok(liveFlow); + } + + protected override void Dispose(bool disposing) + { + if (disposing) + { + _context.Dispose(); + } + base.Dispose(disposing); + } + + private bool LiveFlowExists(long id) + { + return _context.LiveFlow.Count(e => e.Id == id) > 0; + } + } +} \ No newline at end of file