Beginings of api rework with channel inclusion
This commit is contained in:
parent
01bc50f9f2
commit
f708503072
@ -1,4 +1,5 @@
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
@ -7,19 +8,50 @@ namespace YTManager.Controllers {
|
||||
[Produces("application/json")]
|
||||
[Route("api/Channels")]
|
||||
public class ChannelsController : Controller {
|
||||
// Custom return type for API accesses. Done this way to ensure we
|
||||
// always return the expected data regardless of the underlying model.
|
||||
struct Channel_ForAPI {
|
||||
public string Title;
|
||||
public string Description;
|
||||
public string YTID;
|
||||
public List<string> Video_IDs;
|
||||
|
||||
public Channel_ForAPI(Models.Channel c) {
|
||||
Title = c.Title;
|
||||
Description = c.Description;
|
||||
YTID = c.YoutubeID;
|
||||
Video_IDs = c.Videos?.Select(v => v.YoutubeID).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
// DB context used for all these calls.
|
||||
private readonly MediaDB db;
|
||||
|
||||
// Maximum number of channels to return per query.
|
||||
private readonly int max_per_query = 20;
|
||||
|
||||
// Constructor to fetch the db context.
|
||||
public ChannelsController(MediaDB context) => db = context;
|
||||
|
||||
// GET api/Channels
|
||||
// Returns the most recently added channels.
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Get() {
|
||||
return Ok(await db.Channels.OrderByDescending(c => c.AddedtoDB).ToListAsync());
|
||||
// Get all the relevant channels.
|
||||
var chanels = await db.Channels
|
||||
.OrderByDescending(i => i.AddedtoDB)
|
||||
.Take(max_per_query)
|
||||
.ToListAsync();
|
||||
|
||||
// Convert them to what we will send out.
|
||||
var converted = chanels
|
||||
.Select(ch => new Channel_ForAPI(ch))
|
||||
.ToList();
|
||||
|
||||
// Convert all the videos to what we will send back.
|
||||
return Ok(converted);
|
||||
}
|
||||
|
||||
// GET api/Channels/5
|
||||
// GET api/Channels/sdfs6DFS65f
|
||||
[HttpGet("{YTchannelID}")]
|
||||
public async Task<IActionResult> Get([FromQuery] string YTchannelID) {
|
||||
var channel = await db.Channels.SingleOrDefaultAsync(c => c.YoutubeID == YTchannelID);
|
||||
|
@ -7,33 +7,79 @@ namespace YTManager.Controllers {
|
||||
[Produces("application/json")]
|
||||
[Route("api/Videos")]
|
||||
public class VideosController : Controller {
|
||||
// Custom return type for API accesses. Done this way to ensure we
|
||||
// always return the expected data regardless of the underlying model.
|
||||
struct Video_ForAPI {
|
||||
// Title of the video.
|
||||
public string Title;
|
||||
|
||||
// Description of the video.
|
||||
public string Description;
|
||||
|
||||
// Youtube ID of the video.
|
||||
public string ID;
|
||||
|
||||
// Thumbnail URL.
|
||||
public string Thumbnail;
|
||||
|
||||
// Channel youtube ID.
|
||||
public string Channel;
|
||||
|
||||
// Populate this struct using a model video.
|
||||
public Video_ForAPI(Models.Video video) {
|
||||
Title = video.Title;
|
||||
Description = video.Description;
|
||||
ID = video.YoutubeID;
|
||||
Thumbnail = video.ThumbnailURL;
|
||||
Channel = video.channel?.YoutubeID;
|
||||
}
|
||||
}
|
||||
|
||||
// Maximum number of entries to return per query.
|
||||
private readonly int max_per_query = 20;
|
||||
|
||||
// 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
|
||||
// Returns the most recent videos.
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetVideos() {
|
||||
return Ok(await db.Videos.OrderByDescending(i => i.AddedtoDB).ToListAsync());
|
||||
// Get all the relevant videos.
|
||||
var vids = await db.Videos
|
||||
.OrderByDescending(i => i.AddedtoDB)
|
||||
.Take(max_per_query)
|
||||
.ToListAsync();
|
||||
|
||||
// Convert them to what we will send out.
|
||||
var converted = vids
|
||||
.Select(v => new Video_ForAPI(v))
|
||||
.ToList();
|
||||
|
||||
// Convert all the videos to what we will send back.
|
||||
return Ok(converted);
|
||||
}
|
||||
|
||||
// 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);
|
||||
// Returns the most recent videos of a channel.
|
||||
[HttpGet("fromchannel/{channelID}")]
|
||||
public async Task<IActionResult> Get_Channel_Videos([FromRoute] string channelID) {
|
||||
// Get all the relevant videos.
|
||||
var vids = await db.Videos
|
||||
.Include(v => v.channel)
|
||||
.Where(v => v.channel.YoutubeID == channelID)
|
||||
.OrderByDescending(i => i.AddedtoDB)
|
||||
.Take(max_per_query)
|
||||
.ToListAsync();
|
||||
|
||||
// Attempt to get the video from the database.
|
||||
var video = await db.Videos.SingleOrDefaultAsync(m => m.PrimaryKey == id);
|
||||
// Convert them to what we will send out.
|
||||
var converted = vids
|
||||
.Select(v => new Video_ForAPI(v))
|
||||
.ToList();
|
||||
|
||||
// If the video wasn't found then send back not foud.
|
||||
if (video == null)
|
||||
return NotFound();
|
||||
|
||||
// Otherwise send back the video.
|
||||
return Ok(video);
|
||||
// Convert all the videos to what we will send back.
|
||||
return Ok(converted);
|
||||
}
|
||||
}
|
||||
}
|
@ -32,6 +32,10 @@ namespace YTManager.Models {
|
||||
[Required]
|
||||
public DateTime AddedtoDB { get; set; }
|
||||
|
||||
// What channel this video comes from.
|
||||
[Required]
|
||||
public Channel channel;
|
||||
|
||||
// Tag this video applies to.
|
||||
[Required]
|
||||
public List<Tag> Tags;
|
||||
|
@ -99,7 +99,12 @@ namespace YTManager.Tasks {
|
||||
var Videos = await Get_YTVideos(channel.YoutubeID);
|
||||
|
||||
// Get all the videos which haven't been put into this channels videos.
|
||||
var newvids = Videos.Where(nv => !channel.Videos.Any(cv => cv.YoutubeID == nv.YoutubeID));
|
||||
var newvids = Videos
|
||||
.Where(nv => !channel.Videos.Any(cv => cv.YoutubeID == nv.YoutubeID));
|
||||
|
||||
// Say what channel all the videos came from.
|
||||
foreach (var v in newvids)
|
||||
v.channel = channel;
|
||||
|
||||
// Say the channel has been refreshed.
|
||||
channel.Refreshed = DateTime.Now;
|
||||
|
Loading…
Reference in New Issue
Block a user