126 lines
3.1 KiB
C#
126 lines
3.1 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
|
|
{
|
|
private readonly MediaDB _context;
|
|
|
|
public VideosController(MediaDB context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
// GET: api/Videos
|
|
[HttpGet]
|
|
public IEnumerable<Video> GetVideos()
|
|
{
|
|
return _context.Videos;
|
|
}
|
|
|
|
// GET: api/Videos/5
|
|
[HttpGet("{id}")]
|
|
public async Task<IActionResult> GetVideo([FromRoute] long id)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState);
|
|
}
|
|
|
|
var video = await _context.Videos.SingleOrDefaultAsync(m => m.VideoId == id);
|
|
|
|
if (video == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return Ok(video);
|
|
}
|
|
|
|
// PUT: api/Videos/5
|
|
[HttpPut("{id}")]
|
|
public async Task<IActionResult> PutVideo([FromRoute] long id, [FromBody] Video video)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState);
|
|
}
|
|
|
|
if (id != video.VideoId)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
|
|
_context.Entry(video).State = EntityState.Modified;
|
|
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
if (!VideoExists(id))
|
|
{
|
|
return NotFound();
|
|
}
|
|
else
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
// POST: api/Videos
|
|
[HttpPost]
|
|
public async Task<IActionResult> PostVideo([FromBody] Video video)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState);
|
|
}
|
|
|
|
_context.Videos.Add(video);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return CreatedAtAction("GetVideo", new { id = video.VideoId }, video);
|
|
}
|
|
|
|
// DELETE: api/Videos/5
|
|
[HttpDelete("{id}")]
|
|
public async Task<IActionResult> DeleteVideo([FromRoute] long id)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState);
|
|
}
|
|
|
|
var video = await _context.Videos.SingleOrDefaultAsync(m => m.VideoId == id);
|
|
if (video == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
_context.Videos.Remove(video);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return Ok(video);
|
|
}
|
|
|
|
private bool VideoExists(long id)
|
|
{
|
|
return _context.Videos.Any(e => e.VideoId == id);
|
|
}
|
|
}
|
|
} |