From f86597686f792bd3a274d6cd847b901fc3cb7843 Mon Sep 17 00:00:00 2001 From: hak8or Date: Sat, 17 Feb 2018 20:29:25 -0500 Subject: [PATCH] Renaming and formatting --- .../{AdminController.cs => Admin.cs} | 0 YTManager/Controllers/Channels.cs | 91 +++++++++++++ YTManager/Controllers/ValuesController.cs | 76 ----------- YTManager/Controllers/Videos.cs | 80 +++++++++++ YTManager/Controllers/VideosController.cs | 126 ------------------ YTManager/Models/Channel.cs | 6 +- YTManager/Models/Video.cs | 14 +- YTManager/Tasks/FetchVideos.cs | 7 +- 8 files changed, 184 insertions(+), 216 deletions(-) rename YTManager/Controllers/{AdminController.cs => Admin.cs} (100%) create mode 100644 YTManager/Controllers/Channels.cs delete mode 100644 YTManager/Controllers/ValuesController.cs create mode 100644 YTManager/Controllers/Videos.cs delete mode 100644 YTManager/Controllers/VideosController.cs diff --git a/YTManager/Controllers/AdminController.cs b/YTManager/Controllers/Admin.cs similarity index 100% rename from YTManager/Controllers/AdminController.cs rename to YTManager/Controllers/Admin.cs diff --git a/YTManager/Controllers/Channels.cs b/YTManager/Controllers/Channels.cs new file mode 100644 index 0000000..c373bc6 --- /dev/null +++ b/YTManager/Controllers/Channels.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json.Linq; +using Microsoft.EntityFrameworkCore; + +using YTManager; +using YTManager.Models; + +namespace YTManager.Controllers { + [Produces("application/json")] + [Route("api/Channels")] + public class ChannelsController : Controller { + // DB context used for all these calls. + private readonly MediaDB db; + + // Constructor to fetch the db context. + public ChannelsController(MediaDB context) => db = context; + + // GET api/Channels + [HttpGet] + public async Task> Get() { + return await db.Channels.OrderByDescending(c => c.AddedtoDB).ToListAsync(); + } + + // GET api/Channels/5 + [HttpGet("{YTchannelID}")] + public Task Get([FromQuery] string YTchannelID) { + return db.Channels.SingleOrDefaultAsync(c => c.YTChannelID == YTchannelID); + } + + // GET: api/Channels/sdfs6DFS65f/Videos (using YouTube channel ID) + [HttpGet("{YTChannelID}/Videos")] + public async Task GetVideos([FromRoute] string YTchannelID) { + if (!ModelState.IsValid) + return BadRequest(ModelState); + + // Attempt to get the video from the database. + var channel = await db.Channels.SingleOrDefaultAsync(m => m.YTChannelID == YTchannelID); + + // If the channel wasn't found then send back not found. + if (channel == null) + return NotFound(); + + // Send back the videos from the channel. + return Ok(db.Entry(channel).Collection(c => c.Videos).LoadAsync()); + } + + // POST api/Channels + [HttpPost] + public async Task Post([FromBody] Channel channel) { + // Check if we were able to parse. + if (!ModelState.IsValid) return BadRequest(ModelState); + + // Verify the channel doesn't already exist. + if (db.Channels.Any(c => c.YTChannelID == channel.YTChannelID)) + return BadRequest(); + + // Seems good, so add it. + db.Channels.Add(channel); + await db.SaveChangesAsync(); + + // Get all new videos for the channel. + Hangfire.RecurringJob.Trigger(Startup.periodicupdatejobID); + + // And all is well. + return Ok(); + } + + // DELETE api/Channels/5 + [HttpDelete("{YTChannelID}")] + public async Task Delete([FromQuery] string YTChannelID) { + // Check if we were able to parse. + if (!ModelState.IsValid) return BadRequest(ModelState); + + // Attempt to find the channel. + var channel = await db.Channels.SingleOrDefaultAsync(c => c.YTChannelID == YTChannelID); + + // Check if such an entry exists already. + if (channel == null) + return NotFound(); + + // Remove. + db.Channels.Remove(channel); + await db.SaveChangesAsync(); + return Ok(channel); + } + } +} diff --git a/YTManager/Controllers/ValuesController.cs b/YTManager/Controllers/ValuesController.cs deleted file mode 100644 index 194949c..0000000 --- a/YTManager/Controllers/ValuesController.cs +++ /dev/null @@ -1,76 +0,0 @@ -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/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/Channels - [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(); - - // Get all new videos for the channel. - Hangfire.RecurringJob.Trigger(Startup.periodicupdatejobID); - - // And all is well. - return Ok(); - } - - // DELETE api/Channels/5 - [HttpDelete("{id}")] - public void Delete(string id) - { - _context.Remove(_context.Channels.Single(m => m.YTChannelID == id)); - _context.SaveChanges(); - } - } -} diff --git a/YTManager/Controllers/Videos.cs b/YTManager/Controllers/Videos.cs new file mode 100644 index 0000000..301f6f5 --- /dev/null +++ b/YTManager/Controllers/Videos.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using YTManager; +using YTManager.Models; + +namespace YTManager.Controllers { + [Produces("application/json")] + [Route("api/Videos")] + public class VideosController : Controller { + // DB context used for all these calls. + private readonly MediaDB db; + + // Constructor to fetch the db context. + public VideosController(MediaDB context) => db = context; + + // GET: api/Videos + [HttpGet] + public async Task> GetVideos() { + return await db.Videos.OrderByDescending(i => i.AddedtoDB).ToListAsync(); + } + + // GET: api/Videos/5 + [HttpGet("{id}")] + public async Task GetVideo([FromRoute] long id){ + // Check if we were able to parse. + if (!ModelState.IsValid) return BadRequest(ModelState); + + // Attempt to get the video from the database. + var video = await db.Videos.SingleOrDefaultAsync(m => m.key == id); + + // If the video wasn't found then send back not foud. + if (video == null) + return NotFound(); + else + return Ok(video); + } + + // POST: api/Videos + [HttpPost] + public async Task PostVideo([FromBody] Video video){ + // Check if we were able to parse. + if (!ModelState.IsValid) return BadRequest(ModelState); + + // Check if such a database exists already. + if (await db.Videos.AnyAsync(d => d.YTVideoID == video.YTVideoID)) + return BadRequest(); + + // Add our video to the database and tell db to update. + db.Videos.Add(video); + await db.SaveChangesAsync(); + + // Say that the creation was succesfull. + return Ok(); + } + + // DELETE: api/Videos/alfkeo4f5 + [HttpDelete("{ytid}")] + public async Task DeleteVideo([FromRoute] string YTVideoID){ + // Check if we were able to parse. + if (!ModelState.IsValid) return BadRequest(ModelState); + + // Attempt to find the video. + var video = await db.Videos.SingleOrDefaultAsync(m => m.YTVideoID == YTVideoID); + + // Check if such a database exists already. + if (video == null) + return NotFound(); + + // Remove. + db.Videos.Remove(video); + await db.SaveChangesAsync(); + return Ok(video); + } + } +} \ No newline at end of file diff --git a/YTManager/Controllers/VideosController.cs b/YTManager/Controllers/VideosController.cs deleted file mode 100644 index 430775e..0000000 --- a/YTManager/Controllers/VideosController.cs +++ /dev/null @@ -1,126 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using Microsoft.EntityFrameworkCore; -using YTManager; -using YTManager.Models; - -namespace YTManager.Controllers -{ - [Produces("application/json")] - [Route("api/Videos")] - public class VideosController : Controller - { - private readonly MediaDB _context; - - public VideosController(MediaDB context) - { - _context = context; - } - - // GET: api/Videos - [HttpGet] - public IEnumerable