2017-09-01 08:55:02 +00:00
|
|
|
|
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<Models.Channel> 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<IActionResult> 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<Models.Channel>();
|
|
|
|
|
|
|
|
|
|
// 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();
|
2017-09-03 07:00:16 +00:00
|
|
|
|
|
|
|
|
|
// Get all new videos for the channel.
|
|
|
|
|
Hangfire.RecurringJob.Trigger(Startup.periodicupdatejobID);
|
|
|
|
|
|
|
|
|
|
// And all is well.
|
2017-09-01 08:55:02 +00:00
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PUT api/values/5
|
|
|
|
|
[HttpPut("{id}")]
|
|
|
|
|
public void Put(int id, [FromBody]JObject value)
|
|
|
|
|
{
|
|
|
|
|
Models.Channel posted = value.ToObject<Models.Channel>();
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|