BackEnd/YTManager/Controllers/ValuesController.cs

77 lines
2.5 KiB
C#

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/Channels
[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();
// 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();
}
}
}