2018-02-20 06:20:53 +00:00
|
|
|
|
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/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]
|
2018-02-20 05:29:23 +00:00
|
|
|
|
public async Task<IActionResult> GetVideos() {
|
|
|
|
|
return Ok(await db.Videos.OrderByDescending(i => i.AddedtoDB).ToListAsync());
|
2018-02-18 01:29:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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.
|
2018-02-20 04:57:14 +00:00
|
|
|
|
var video = await db.Videos.SingleOrDefaultAsync(m => m.PrimaryKey == id);
|
2018-02-18 01:29:25 +00:00
|
|
|
|
|
|
|
|
|
// If the video wasn't found then send back not foud.
|
|
|
|
|
if (video == null)
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
2018-02-20 05:29:23 +00:00
|
|
|
|
// Otherwise send back the video.
|
2018-02-18 01:29:25 +00:00
|
|
|
|
return Ok(video);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|