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