Beginings of api rework with channel inclusion
This commit is contained in:
@ -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);
|
||||
|
Reference in New Issue
Block a user