Renaming and formatting

This commit is contained in:
hak8or 2018-02-17 20:29:25 -05:00
parent b44eedfc2a
commit f86597686f
8 changed files with 184 additions and 216 deletions

View 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);
}
}
}

View File

@ -1,76 +0,0 @@
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();
}
}
}

View File

@ -0,0 +1,80 @@
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/Videos")]
public class VideosController : Controller {
// DB context used for all these calls.
private readonly MediaDB db;
// Constructor to fetch the db context.
public VideosController(MediaDB context) => db = context;
// GET: api/Videos
[HttpGet]
public async Task<List<Video>> GetVideos() {
return await db.Videos.OrderByDescending(i => i.AddedtoDB).ToListAsync();
}
// GET: api/Videos/5
[HttpGet("{id}")]
public async Task<IActionResult> GetVideo([FromRoute] long id){
// Check if we were able to parse.
if (!ModelState.IsValid) return BadRequest(ModelState);
// Attempt to get the video from the database.
var video = await db.Videos.SingleOrDefaultAsync(m => m.key == id);
// If the video wasn't found then send back not foud.
if (video == null)
return NotFound();
else
return Ok(video);
}
// POST: api/Videos
[HttpPost]
public async Task<IActionResult> PostVideo([FromBody] Video video){
// Check if we were able to parse.
if (!ModelState.IsValid) return BadRequest(ModelState);
// Check if such a database exists already.
if (await db.Videos.AnyAsync(d => d.YTVideoID == video.YTVideoID))
return BadRequest();
// Add our video to the database and tell db to update.
db.Videos.Add(video);
await db.SaveChangesAsync();
// Say that the creation was succesfull.
return Ok();
}
// DELETE: api/Videos/alfkeo4f5
[HttpDelete("{ytid}")]
public async Task<IActionResult> DeleteVideo([FromRoute] string YTVideoID){
// Check if we were able to parse.
if (!ModelState.IsValid) return BadRequest(ModelState);
// Attempt to find the video.
var video = await db.Videos.SingleOrDefaultAsync(m => m.YTVideoID == YTVideoID);
// Check if such a database exists already.
if (video == null)
return NotFound();
// Remove.
db.Videos.Remove(video);
await db.SaveChangesAsync();
return Ok(video);
}
}
}

View File

@ -1,126 +0,0 @@
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/Videos")]
public class VideosController : Controller
{
private readonly MediaDB _context;
public VideosController(MediaDB context)
{
_context = context;
}
// GET: api/Videos
[HttpGet]
public IEnumerable<Video> GetVideos()
{
return _context.Videos.OrderByDescending(i => i.Uploaded);
}
// GET: api/Videos/5
[HttpGet("{id}")]
public async Task<IActionResult> GetVideo([FromRoute] long id)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var video = await _context.Videos.SingleOrDefaultAsync(m => m.VideoId == id);
if (video == null)
{
return NotFound();
}
return Ok(video);
}
// PUT: api/Videos/5
[HttpPut("{id}")]
public async Task<IActionResult> PutVideo([FromRoute] long id, [FromBody] Video video)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != video.VideoId)
{
return BadRequest();
}
_context.Entry(video).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!VideoExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Videos
[HttpPost]
public async Task<IActionResult> PostVideo([FromBody] Video video)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
_context.Videos.Add(video);
await _context.SaveChangesAsync();
return CreatedAtAction("GetVideo", new { id = video.VideoId }, video);
}
// DELETE: api/Videos/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteVideo([FromRoute] long id)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var video = await _context.Videos.SingleOrDefaultAsync(m => m.VideoId == id);
if (video == null)
{
return NotFound();
}
_context.Videos.Remove(video);
await _context.SaveChangesAsync();
return Ok(video);
}
private bool VideoExists(long id)
{
return _context.Videos.Any(e => e.VideoId == id);
}
}
}

View File

@ -8,7 +8,7 @@ namespace YTManager.Models {
public class Channel {
// Uniquie ID for this media type
[Key]
public long ChannelId { get; set; }
public long key { get; set; }
// Title of the media
[Required]
@ -26,6 +26,10 @@ namespace YTManager.Models {
[Required]
public string YTChannelID { get; set; }
// Added to this manager.
[Required]
public DateTime AddedtoDB { get; set; }
// Videos this channel has.
public List<Video> Videos { get; set; }
}

View File

@ -8,7 +8,7 @@ namespace YTManager.Models {
public class Video {
// Uniquie ID for this media type
[Key]
public long VideoId { get; set; }
public long key { get; set; }
// Title of the media
[Required]
@ -22,24 +22,20 @@ namespace YTManager.Models {
[Required]
public string YTVideoID { get; set; }
// Channel this video belongs to.
[Required]
public string YTChannelID { get; set; }
// Thumbnail link
[Required]
public string ThumbnailURL { get; set; }
// Date video was uploaded to YT
[Required]
public DateTime Uploaded { get; set; }
public DateTime AddedToYT { get; set; }
// Date added to database
[Required]
public DateTime AddedtoDB { get; set; }
// Refer back to what channel this video belongs to.
public long ChannelId { get; set; }
public Channel Channel { get; set; }
// Channel this video comes from.
[Required]
public Channel channel;
}
}

View File

@ -38,7 +38,7 @@ namespace YTManager.Tasks
query.MaxResults = 50;
var response = query.Execute();
// Get all videos which aren't already in the DB.
// Get all videos which aren't already in the DB based on the ytid.
var notindb = response.Items
.Where(i => i.Id.Kind == "youtube#video")
.Where(i => !dbcontext.Videos.Any(dbvid => dbvid.YTVideoID == i.Id.VideoId))
@ -47,10 +47,9 @@ namespace YTManager.Tasks
Title = newvid.Snippet.Title,
Description = newvid.Snippet.Description,
YTVideoID = newvid.Id.VideoId,
Uploaded = newvid.Snippet.PublishedAt.GetValueOrDefault(),
AddedToYT = newvid.Snippet.PublishedAt.GetValueOrDefault(),
AddedtoDB = DateTime.Now,
YTChannelID = newvid.Snippet.ChannelId,
Channel = ch,
channel = ch,
ThumbnailURL = newvid.Snippet.Thumbnails.Medium.Url
}).ToList();