Huge cleanup and rework pass, postgres now usable too

This commit is contained in:
2018-02-19 23:57:14 -05:00
parent d996f7a2cb
commit a3a3c3a780
18 changed files with 472 additions and 110 deletions

View File

@ -13,35 +13,40 @@ namespace YTManager.Controllers {
[Produces("application/json")]
[Route("api/Admin")]
public class AdminController : Controller {
// POST api/Admin/refreshytvids
// Force a general youtube channel update.
[HttpPost("job_update")]
public void Post_forcejobupdate() {
Hangfire.RecurringJob.Trigger(Startup.periodicupdatejobID);
// ID for mass update job, used to tell if the job is running or not.
public static string Mass_Updater_ID { get; } = "2013066213";
// POST api/Admin/Trigger_Mass_Update
// Trigger a mass channel update.
[HttpPost("Trigger_Mass_Update")]
public IActionResult Trigger_Mass_Update() {
Hangfire.RecurringJob.Trigger(Mass_Updater_ID);
return Ok();
}
// POST api/Admin/job_periodicupdate/YTChannelID
// Force a YT Channel update.
[HttpPost("job_update/{id}")]
public void Post_forcejobupdateID([FromRoute] string id) {
Hangfire.BackgroundJob.Enqueue(() => YTManager.Tasks.FetchVideos.run(id));
// POST api/Admin/Update/YTChannelID
// Update the videos for a specific channel.
[HttpPost("Update/{YoutubeID}")]
public IActionResult Update([FromRoute] string YoutubeID) {
Hangfire.BackgroundJob.Enqueue(() => Tasks.FetchVideos.ChannelUpdate(Startup.DBStr, YoutubeID));
return Ok();
}
// POST api/Admin/job_periodicupdate
// POST api/Admin/Start_Updater
// Ensures that the background YT Channel update API is running.
[HttpPost("job_periodicupdate")]
public void Post_periodicupdatejob() {
[HttpPost("Start_Updater")]
public IActionResult Start_Updater() {
Hangfire.RecurringJob.AddOrUpdate(
Startup.periodicupdatejobID,
() => YTManager.Tasks.FetchVideos.run(""),
Hangfire.Cron.Hourly);
Mass_Updater_ID,
() => Tasks.FetchVideos.MassUpdate(Startup.DBStr), Hangfire.Cron.Hourly);
return Ok();
}
// GET api/Admin/job_periodicupdate
// GET api/Admin/Updater
// Check if the periodic update job is enqued.
[HttpGet("job_periodicupdate")]
public IActionResult Get_periodicupdatejob() {
bool exists = Hangfire.JobStorage.Current.GetConnection().GetRecurringJobs().Count(j => j.Id == Startup.periodicupdatejobID) > 0;
[HttpGet("Update")]
public IActionResult Get_Update_Status() {
bool exists = Hangfire.JobStorage.Current.GetConnection().GetRecurringJobs().Any(j => j.Id == Mass_Updater_ID);
return Ok(exists ? "true" : "false");
}
}

View File

@ -28,7 +28,7 @@ namespace YTManager.Controllers {
// GET api/Channels/5
[HttpGet("{YTchannelID}")]
public Task<Models.Channel> Get([FromQuery] string YTchannelID) {
return db.Channels.SingleOrDefaultAsync(c => c.YTChannelID == YTchannelID);
return db.Channels.SingleOrDefaultAsync(c => c.YoutubeID == YTchannelID);
}
// GET: api/Channels/sdfs6DFS65f/Videos (using YouTube channel ID)
@ -38,7 +38,7 @@ namespace YTManager.Controllers {
return BadRequest(ModelState);
// Attempt to get the video from the database.
var channel = await db.Channels.SingleOrDefaultAsync(m => m.YTChannelID == YTchannelID);
var channel = await db.Channels.SingleOrDefaultAsync(m => m.YoutubeID == YTchannelID);
// If the channel wasn't found then send back not found.
if (channel == null)
@ -48,6 +48,27 @@ namespace YTManager.Controllers {
return Ok(db.Entry(channel).Collection(c => c.Videos).LoadAsync());
}
// POST api/Channels/sdfs6DFS65f
[HttpPost("{YTchannelID}")]
public async Task<IActionResult> Post([FromRoute] string YTchannelID) {
// Check if we were able to parse.
if (!ModelState.IsValid) return BadRequest(ModelState);
// Verify the channel doesn't already exist.
if (db.Channels.Any(c => c.YoutubeID == YTchannelID))
return BadRequest();
// Get the channel info.
var ch = await Tasks.FetchVideos.Get_YTChannel(YTchannelID);
// Seems good, so add it.
db.Channels.Add(ch);
await db.SaveChangesAsync();
// And all is well.
return Ok();
}
// POST api/Channels
[HttpPost]
public async Task<IActionResult> Post([FromBody] Channel channel) {
@ -55,28 +76,25 @@ namespace YTManager.Controllers {
if (!ModelState.IsValid) return BadRequest(ModelState);
// Verify the channel doesn't already exist.
if (db.Channels.Any(c => c.YTChannelID == channel.YTChannelID))
if (db.Channels.Any(c => c.YoutubeID == channel.YoutubeID))
return BadRequest();
// Seems good, so add it.
db.Channels.Add(channel);
await db.SaveChangesAsync();
// Get all new videos for the channel.
Hangfire.RecurringJob.Trigger(Startup.periodicupdatejobID);
// And all is well.
return Ok();
}
// DELETE api/Channels/5
[HttpDelete("{YTChannelID}")]
public async Task<IActionResult> Delete([FromQuery] string YTChannelID) {
public async Task<IActionResult> Delete([FromRoute] string YTChannelID) {
// Check if we were able to parse.
if (!ModelState.IsValid) return BadRequest(ModelState);
// Attempt to find the channel.
var channel = await db.Channels.SingleOrDefaultAsync(c => c.YTChannelID == YTChannelID);
var channel = await db.Channels.SingleOrDefaultAsync(c => c.YoutubeID == YTChannelID);
// Check if such an entry exists already.
if (channel == null)

View File

@ -31,7 +31,7 @@ namespace YTManager.Controllers {
if (!ModelState.IsValid) return BadRequest(ModelState);
// Attempt to get the video from the database.
var video = await db.Videos.SingleOrDefaultAsync(m => m.key == id);
var video = await db.Videos.SingleOrDefaultAsync(m => m.PrimaryKey == id);
// If the video wasn't found then send back not foud.
if (video == null)
@ -47,7 +47,7 @@ namespace YTManager.Controllers {
if (!ModelState.IsValid) return BadRequest(ModelState);
// Check if such a database exists already.
if (await db.Videos.AnyAsync(d => d.YTVideoID == video.YTVideoID))
if (await db.Videos.AnyAsync(d => d.YoutubeID == video.YoutubeID))
return BadRequest();
// Add our video to the database and tell db to update.
@ -65,7 +65,7 @@ namespace YTManager.Controllers {
if (!ModelState.IsValid) return BadRequest(ModelState);
// Attempt to find the video.
var video = await db.Videos.SingleOrDefaultAsync(m => m.YTVideoID == YTVideoID);
var video = await db.Videos.SingleOrDefaultAsync(m => m.YoutubeID == YTVideoID);
// Check if such a database exists already.
if (video == null)