2018-02-24 04:03:27 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2018-02-18 01:29:25 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
|
|
namespace YTManager.Controllers {
|
|
|
|
|
[Produces("application/json")]
|
|
|
|
|
[Route("api/Channels")]
|
|
|
|
|
public class ChannelsController : Controller {
|
2018-02-24 04:03:27 +00:00
|
|
|
|
// 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;
|
2018-02-24 04:14:37 +00:00
|
|
|
|
public string ID;
|
2018-02-24 04:03:27 +00:00
|
|
|
|
public List<string> Video_IDs;
|
|
|
|
|
|
|
|
|
|
public Channel_ForAPI(Models.Channel c) {
|
|
|
|
|
Title = c.Title;
|
|
|
|
|
Description = c.Description;
|
2018-02-24 04:14:37 +00:00
|
|
|
|
ID = c.YoutubeID;
|
2018-02-24 06:10:12 +00:00
|
|
|
|
Video_IDs = c.Videos.Select(v => v.YoutubeID).ToList();
|
2018-02-24 04:03:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-18 01:29:25 +00:00
|
|
|
|
// DB context used for all these calls.
|
|
|
|
|
private readonly MediaDB db;
|
|
|
|
|
|
2018-02-24 04:03:27 +00:00
|
|
|
|
// Maximum number of channels to return per query.
|
|
|
|
|
private readonly int max_per_query = 20;
|
|
|
|
|
|
2018-02-18 01:29:25 +00:00
|
|
|
|
// Constructor to fetch the db context.
|
|
|
|
|
public ChannelsController(MediaDB context) => db = context;
|
|
|
|
|
|
2018-02-24 04:03:27 +00:00
|
|
|
|
// Returns the most recently added channels.
|
2018-02-18 01:29:25 +00:00
|
|
|
|
[HttpGet]
|
2018-02-20 05:29:23 +00:00
|
|
|
|
public async Task<IActionResult> Get() {
|
2018-02-24 04:03:27 +00:00
|
|
|
|
// Get all the relevant channels.
|
|
|
|
|
var chanels = await db.Channels
|
2018-02-24 06:10:12 +00:00
|
|
|
|
.Include(c => c.Videos)
|
2018-02-24 04:03:27 +00:00
|
|
|
|
.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);
|
2018-02-18 01:29:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|