From 038d363b00bfb70a8556b2b4c88000ada18577fc Mon Sep 17 00:00:00 2001 From: hak8or Date: Sat, 24 Feb 2018 01:10:12 -0500 Subject: [PATCH] Huge API rewrite --- YTManager/Controllers/Channels.cs | 3 +- YTManager/Controllers/Raw/Private_Channel.cs | 141 ++++++++++++++++++ YTManager/Controllers/Raw/Private_Video.cs | 129 ++++++++++++++++ YTManager/Controllers/Videos.cs | 13 +- .../20180220053952_added_refreshed.cs | 25 ---- ....cs => 20180224051602_initial.Designer.cs} | 4 +- ...migration.cs => 20180224051602_initial.cs} | 3 +- ...5707_fix_channel_relationship.Designer.cs} | 24 ++- ...20180224055707_fix_channel_relationship.cs | 82 ++++++++++ YTManager/Migrations/MediaDBModelSnapshot.cs | 18 ++- YTManager/Models/Video.cs | 4 +- YTManager/Tasks/FetchVideos.cs | 7 +- YTManager/wwwroot/Admin/index.css | 64 ++++++++ .../wwwroot/{admin.html => Admin/index.html} | 2 +- .../wwwroot/{admin.js => Admin/index.js} | 4 +- YTManager/wwwroot/index.css | 26 ---- 16 files changed, 472 insertions(+), 77 deletions(-) create mode 100644 YTManager/Controllers/Raw/Private_Channel.cs create mode 100644 YTManager/Controllers/Raw/Private_Video.cs delete mode 100644 YTManager/Migrations/20180220053952_added_refreshed.cs rename YTManager/Migrations/{20180220053952_added_refreshed.Designer.cs => 20180224051602_initial.Designer.cs} (97%) rename YTManager/Migrations/{20180220032847_initiailmigration.cs => 20180224051602_initial.cs} (96%) rename YTManager/Migrations/{20180220032847_initiailmigration.Designer.cs => 20180224055707_fix_channel_relationship.Designer.cs} (78%) create mode 100644 YTManager/Migrations/20180224055707_fix_channel_relationship.cs create mode 100644 YTManager/wwwroot/Admin/index.css rename YTManager/wwwroot/{admin.html => Admin/index.html} (97%) rename YTManager/wwwroot/{admin.js => Admin/index.js} (99%) diff --git a/YTManager/Controllers/Channels.cs b/YTManager/Controllers/Channels.cs index 5f1ee83..2c67f64 100644 --- a/YTManager/Controllers/Channels.cs +++ b/YTManager/Controllers/Channels.cs @@ -20,7 +20,7 @@ namespace YTManager.Controllers { Title = c.Title; Description = c.Description; ID = c.YoutubeID; - Video_IDs = c.Videos?.Select(v => v.YoutubeID).ToList(); + Video_IDs = c.Videos.Select(v => v.YoutubeID).ToList(); } } @@ -38,6 +38,7 @@ namespace YTManager.Controllers { public async Task Get() { // Get all the relevant channels. var chanels = await db.Channels + .Include(c => c.Videos) .OrderByDescending(i => i.AddedtoDB) .Take(max_per_query) .ToListAsync(); diff --git a/YTManager/Controllers/Raw/Private_Channel.cs b/YTManager/Controllers/Raw/Private_Channel.cs new file mode 100644 index 0000000..09ad568 --- /dev/null +++ b/YTManager/Controllers/Raw/Private_Channel.cs @@ -0,0 +1,141 @@ +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.Private +{ + [Produces("application/json")] + [Route("api_raw/Channels")] + public class Private_Channel : Controller + { + private readonly MediaDB db; + + public Private_Channel(MediaDB context) + { + db = context; + } + + [HttpGet] + public IEnumerable GetChannels() + { + return db.Channels; + } + + [HttpGet("{id}")] + public async Task GetChannel([FromRoute] long id) + { + if (!ModelState.IsValid) + { + return BadRequest(ModelState); + } + + var channel = await db + .Channels + .Include(c => c.Videos) + .SingleOrDefaultAsync(m => m.PrimaryKey == id); + + if (channel == null) + { + return NotFound(); + } + + return Ok(channel); + } + + [HttpPut("{id}")] + public async Task PutChannel([FromRoute] long id, [FromBody] Channel channel) + { + if (!ModelState.IsValid) + { + return BadRequest(ModelState); + } + + if (id != channel.PrimaryKey) + { + return BadRequest(); + } + + db.Entry(channel).State = EntityState.Modified; + + try + { + await db.SaveChangesAsync(); + } + catch (DbUpdateConcurrencyException) + { + if (!ChannelExists(id)) + { + return NotFound(); + } + else + { + throw; + } + } + + return NoContent(); + } + + [HttpPost("{channelid}")] + public async Task PostChannel([FromRoute] string channelid) { + // Only add it to the databse if it's not in there already. + if (db.Channels.Any(c => c.YoutubeID == channelid)) + return BadRequest(); + + // Get the channel contents from youtube. + var channel = await Tasks.FetchVideos.Get_YTChannel(channelid); + + // Add it to the databse. + await db.Channels.AddAsync(channel); + await db.SaveChangesAsync(); + + // Say all went ok. + return Ok(channel); + } + + [HttpPost] + public async Task PostChannel([FromBody] Channel channel) + { + if (!ModelState.IsValid) + { + return BadRequest(ModelState); + } + + db.Channels.Add(channel); + await db.SaveChangesAsync(); + + return CreatedAtAction("GetChannel", new { id = channel.PrimaryKey }, channel); + } + + [HttpDelete("{id}")] + public async Task DeleteChannel([FromRoute] long id) + { + if (!ModelState.IsValid) + { + return BadRequest(ModelState); + } + + var channel = await db.Channels.SingleOrDefaultAsync(m => m.PrimaryKey == id); + if (channel == null) + { + return NotFound(); + } + + db.Channels.Remove(channel); + await db.SaveChangesAsync(); + + return Ok(channel); + } + + private bool ChannelExists(long id) + { + return db.Channels.Any(e => e.PrimaryKey == id); + } + } +} \ No newline at end of file diff --git a/YTManager/Controllers/Raw/Private_Video.cs b/YTManager/Controllers/Raw/Private_Video.cs new file mode 100644 index 0000000..d4c0d9d --- /dev/null +++ b/YTManager/Controllers/Raw/Private_Video.cs @@ -0,0 +1,129 @@ +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_raw/Videos")] + public class Private_Videos : Controller + { + private readonly MediaDB _context; + + public Private_Videos(MediaDB context) + { + _context = context; + } + + // GET: api/Videos + [HttpGet] + public IEnumerable