using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; using Microsoft.EntityFrameworkCore; namespace YTManager.Controllers { [Produces("application/json")] [Route("api/Admin")] public class AdminController : Controller { // POST api/Admin/refreshytvids [HttpPost("refreshytvids")] public void Post() { Hangfire.BackgroundJob.Enqueue(() => YTManager.Tasks.FetchVideos.run()); } } [Produces("application/json")] [Route("api/Channels")] public class ChannelsController : Controller { private readonly MediaDB _context; public ChannelsController(MediaDB context) { _context = context; } // GET api/Channels [HttpGet] public IEnumerable Get() { return _context.Channels.ToList(); } // GET api/Channels/5 [HttpGet("{id}")] public Models.Channel Get(int id) { return _context.Channels.Single(m => m.ChannelId == id); } // GET: api/Channels/sdfs6DFS65f/Videos (using YouTube channel ID) [HttpGet("{id}/Videos")] public async Task GetVideos([FromRoute] string YTchannelID) { if (!ModelState.IsValid) return BadRequest(ModelState); // Verify the channel exists. var Chan = await _context.Channels.SingleOrDefaultAsync(c => c.YTChannelID == YTchannelID); if (Chan == null) return NotFound(); // Send back the found stuff. return Ok(_context.Entry(Chan).Collection(c => c.Videos).LoadAsync()); } // POST api/values [HttpPost] public IActionResult Post([FromBody]JObject value) { // Get the channel out of our json body. Models.Channel posted = value.ToObject(); // Verify items aren't empty. if ((posted.YTChannelID.Length == 0) || (posted.Description.Length == 0) || (posted.Title.Length == 0)) return BadRequest(); // Verify the channel doesn't already exist. if (_context.Channels.Any(c => c.YTChannelID == posted.YTChannelID)) return BadRequest(); // Seems good, so add it. _context.Channels.Add(posted); _context.SaveChanges(); return Ok(); } // PUT api/values/5 [HttpPut("{id}")] public void Put(int id, [FromBody]JObject value) { Models.Channel posted = value.ToObject(); posted.ChannelId = id; // Ensure an id is attached _context.Channels.Update(posted); _context.SaveChanges(); } // DELETE api/values/5 [HttpDelete("{id}")] public void Delete(int id) { _context.Remove(_context.Channels.Single(m => m.ChannelId == id)); _context.SaveChanges(); } } }