Add project files.

This commit is contained in:
2017-09-01 04:55:02 -04:00
parent 70d055282c
commit ae782025f6
22 changed files with 1244 additions and 0 deletions

View File

@ -0,0 +1,95 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using Microsoft.EntityFrameworkCore;
namespace YTManager.Controllers {
[Produces("application/json")]
[Route("api/Admin")]
public class AdminController : Controller {
// POST api/Admin/refreshytvids
[HttpPost("refreshytvids")]
public void Post() {
Hangfire.BackgroundJob.Enqueue(() => YTManager.Tasks.FetchVideos.run());
}
}
[Produces("application/json")]
[Route("api/Channels")]
public class ChannelsController : Controller {
private readonly MediaDB _context;
public ChannelsController(MediaDB context)
{
_context = context;
}
// GET api/Channels
[HttpGet]
public IEnumerable<Models.Channel> Get() {
return _context.Channels.ToList();
}
// GET api/Channels/5
[HttpGet("{id}")]
public Models.Channel Get(int id) {
return _context.Channels.Single(m => m.ChannelId == id);
}
// GET: api/Channels/sdfs6DFS65f/Videos (using YouTube channel ID)
[HttpGet("{id}/Videos")]
public async Task<IActionResult> GetVideos([FromRoute] string YTchannelID) {
if (!ModelState.IsValid)
return BadRequest(ModelState);
// Verify the channel exists.
var Chan = await _context.Channels.SingleOrDefaultAsync(c => c.YTChannelID == YTchannelID);
if (Chan == null)
return NotFound();
// Send back the found stuff.
return Ok(_context.Entry(Chan).Collection(c => c.Videos).LoadAsync());
}
// POST api/values
[HttpPost]
public IActionResult Post([FromBody]JObject value) {
// Get the channel out of our json body.
Models.Channel posted = value.ToObject<Models.Channel>();
// Verify items aren't empty.
if ((posted.YTChannelID.Length == 0) || (posted.Description.Length == 0) || (posted.Title.Length == 0))
return BadRequest();
// Verify the channel doesn't already exist.
if (_context.Channels.Any(c => c.YTChannelID == posted.YTChannelID))
return BadRequest();
// Seems good, so add it.
_context.Channels.Add(posted);
_context.SaveChanges();
return Ok();
}
// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody]JObject value)
{
Models.Channel posted = value.ToObject<Models.Channel>();
posted.ChannelId = id; // Ensure an id is attached
_context.Channels.Update(posted);
_context.SaveChanges();
}
// DELETE api/values/5
[HttpDelete("{id}")]
public void Delete(int id)
{
_context.Remove(_context.Channels.Single(m => m.ChannelId == id));
_context.SaveChanges();
}
}
}

View File

@ -0,0 +1,126 @@
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);
}
}
}