BackEnd/YTManager/Controllers/Videos.cs

44 lines
1.4 KiB
C#

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<IActionResult> GetVideos() {
return Ok(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.PrimaryKey == id);
// If the video wasn't found then send back not foud.
if (video == null)
return NotFound();
// Otherwise send back the video.
return Ok(video);
}
}
}