Renaming and formatting
This commit is contained in:
91
YTManager/Controllers/Channels.cs
Normal file
91
YTManager/Controllers/Channels.cs
Normal file
@ -0,0 +1,91 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
using YTManager;
|
||||
using YTManager.Models;
|
||||
|
||||
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<IEnumerable<Models.Channel>> Get() {
|
||||
return await db.Channels.OrderByDescending(c => c.AddedtoDB).ToListAsync();
|
||||
}
|
||||
|
||||
// GET api/Channels/5
|
||||
[HttpGet("{YTchannelID}")]
|
||||
public Task<Models.Channel> Get([FromQuery] string YTchannelID) {
|
||||
return db.Channels.SingleOrDefaultAsync(c => c.YTChannelID == YTchannelID);
|
||||
}
|
||||
|
||||
// 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.YTChannelID == 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
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Post([FromBody] Channel channel) {
|
||||
// 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.YTChannelID == channel.YTChannelID))
|
||||
return BadRequest();
|
||||
|
||||
// Seems good, so add it.
|
||||
db.Channels.Add(channel);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
// Get all new videos for the channel.
|
||||
Hangfire.RecurringJob.Trigger(Startup.periodicupdatejobID);
|
||||
|
||||
// And all is well.
|
||||
return Ok();
|
||||
}
|
||||
|
||||
// DELETE api/Channels/5
|
||||
[HttpDelete("{YTChannelID}")]
|
||||
public async Task<IActionResult> Delete([FromQuery] 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.SingleOrDefaultAsync(c => c.YTChannelID == YTChannelID);
|
||||
|
||||
// Check if such an entry exists already.
|
||||
if (channel == null)
|
||||
return NotFound();
|
||||
|
||||
// Remove.
|
||||
db.Channels.Remove(channel);
|
||||
await db.SaveChangesAsync();
|
||||
return Ok(channel);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user