95 lines
3.3 KiB
C#
95 lines
3.3 KiB
C#
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
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<IActionResult> Get() {
|
|
return Ok(await db.Channels.OrderByDescending(c => c.AddedtoDB).ToListAsync());
|
|
}
|
|
|
|
// GET api/Channels/5
|
|
[HttpGet("{YTchannelID}")]
|
|
public async Task<IActionResult> Get([FromQuery] string YTchannelID) {
|
|
var channel = await db.Channels.SingleOrDefaultAsync(c => c.YoutubeID == YTchannelID);
|
|
if (channel == null)
|
|
return NotFound();
|
|
else
|
|
return Ok(channel);
|
|
}
|
|
|
|
// GET: api/Channels/sdfs6DFS65f/Videos (using YouTube channel ID)
|
|
[HttpGet("{YTChannelID}/Videos")]
|
|
public async Task<IActionResult> 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.YoutubeID == 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/sdfs6DFS65f
|
|
[HttpPost("{YTchannelID}")]
|
|
public async Task<IActionResult> Post([FromRoute] string YTchannelID) {
|
|
// 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.YoutubeID == YTchannelID))
|
|
return BadRequest();
|
|
|
|
// Get the channel info.
|
|
var ch = await Tasks.FetchVideos.Get_YTChannel(YTchannelID);
|
|
|
|
// Seems good, so add it.
|
|
db.Channels.Add(ch);
|
|
await db.SaveChangesAsync();
|
|
|
|
// And all is well.
|
|
return Ok();
|
|
}
|
|
|
|
// DELETE api/Channels/5
|
|
[HttpDelete("{YTChannelID}")]
|
|
public async Task<IActionResult> Delete([FromRoute] 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.Include(c => c.Videos).SingleOrDefaultAsync(c => c.YoutubeID == YTChannelID);
|
|
|
|
// Check if such an entry exists already.
|
|
if (channel == null)
|
|
return NotFound();
|
|
|
|
// Remove all the channels videos.
|
|
channel.Videos.ForEach(v => db.Videos.Remove(v));
|
|
|
|
// Remove the channel itself.
|
|
db.Channels.Remove(channel);
|
|
|
|
// Save all the changes.
|
|
await db.SaveChangesAsync();
|
|
return Ok(channel);
|
|
}
|
|
}
|
|
}
|