Fixed a bug in the Login and added a unit test to cover that.

Added a button to approve an individual request.
Fixed some minor bugs in the request screen
This commit is contained in:
Jamie Rees 2016-03-10 19:58:32 +00:00
commit 452ad07ba0
17 changed files with 238 additions and 25 deletions

View file

@ -76,6 +76,10 @@ namespace PlexRequests.UI
container.Register<IConfigurationReader, ConfigurationReader>();
container.Register<IIntervals, UpdateInterval>();
container.Register<ICouchPotatoApi, CouchPotatoApi>();
container.Register<IPlexApi, PlexApi>();
base.ConfigureRequestContainer(container, context);

View file

@ -13,6 +13,7 @@ var tvimer = 0;
movieLoad();
tvLoad();
// Approve all
$('#approveAll').click(function () {
$.ajax({
type: 'post',
@ -127,6 +128,34 @@ $(document).on("click", ".delete", function (e) {
});
// Approve single request
$(document).on("click", ".approve", function (e) {
e.preventDefault();
var buttonId = e.target.id;
var $form = $('#approve' + buttonId);
$.ajax({
type: $form.prop('method'),
url: $form.prop('action'),
data: $form.serialize(),
dataType: "json",
success: function (response) {
if (checkJsonResponse(response)) {
generateNotify("Success! Request Approved.", "success");
$("button[custom-button='" + buttonId + "']").remove();
$("#" + buttonId + "notapproved").prop("class", "fa fa-check");
}
},
error: function (e) {
console.log(e);
generateNotify("Something went wrong!", "danger");
}
});
});
// Clear issues
$(document).on("click", ".clear", function (e) {
e.preventDefault();

View file

@ -5,7 +5,13 @@
message: message
}, {
// settings
type: type
type: type,
animate: {
enter: 'animated bounceInDown',
exit: 'animated bounceOutUp'
},
newest_on_top: true
});
}

View file

@ -32,7 +32,10 @@ using Nancy;
using Nancy.Security;
using NLog;
using PlexRequests.Api;
using PlexRequests.Api.Interfaces;
using PlexRequests.Core;
using PlexRequests.Core.SettingModels;
using PlexRequests.Store;
using PlexRequests.UI.Models;
@ -41,11 +44,13 @@ namespace PlexRequests.UI.Modules
public class ApprovalModule : BaseModule
{
public ApprovalModule(IRepository<RequestedModel> service) : base("approval")
public ApprovalModule(IRepository<RequestedModel> service, ISettingsService<CouchPotatoSettings> cpService, ICouchPotatoApi cpApi) : base("approval")
{
this.RequiresAuthentication();
Service = service;
CpService = cpService;
CpApi = cpApi;
Post["/approve"] = parameters => Approve((int)Request.Form.requestid);
Post["/approveall"] = x => ApproveAll();
@ -54,6 +59,8 @@ namespace PlexRequests.UI.Modules
private IRepository<RequestedModel> Service { get; set; }
private static Logger Log = LogManager.GetCurrentClassLogger();
private ISettingsService<CouchPotatoSettings> CpService { get; }
private ICouchPotatoApi CpApi { get; }
/// <summary>
/// Approves the specified request identifier.
@ -62,6 +69,10 @@ namespace PlexRequests.UI.Modules
/// <returns></returns>
private Response Approve(int requestId)
{
if (!Context.CurrentUser.IsAuthenticated())
{
return Response.AsJson(new JsonResponseModel { Result = false, Message = "You are not an Admin, so you cannot approve any requests." });
}
// Get the request from the DB
var request = Service.Get(requestId);
@ -71,15 +82,59 @@ namespace PlexRequests.UI.Modules
return Response.AsJson(new JsonResponseModel { Result = false, Message = "There are no requests to approve. Please refresh." });
}
// Approve it
request.Approved = true;
switch (request.Type)
{
case RequestType.Movie:
return RequestMovieAndUpdateStatus(request);
case RequestType.TvShow:
return RequestTvAndUpdateStatus(request);
default:
throw new ArgumentOutOfRangeException(nameof(request));
}
}
// Update the record
var result = Service.Update(request);
private Response RequestTvAndUpdateStatus(RequestedModel request)
{
// TODO
return Response.AsJson(new JsonResponseModel());
}
return Response.AsJson(result
? new JsonResponseModel { Result = true }
: new JsonResponseModel { Result = false, Message = "We could not approve this request. Please try again or check the logs." });
private Response RequestMovieAndUpdateStatus(RequestedModel request)
{
if (!Context.CurrentUser.IsAuthenticated())
{
return Response.AsJson(new JsonResponseModel { Result = false, Message = "You are not an Admin, so you cannot approve any requests." });
}
var cpSettings = CpService.GetSettings();
var cp = new CouchPotatoApi();
Log.Info("Adding movie to CP : {0}", request.Title);
var result = cp.AddMovie(request.ImdbId, cpSettings.ApiKey, request.Title, cpSettings.FullUri);
Log.Trace("Adding movie to CP result {0}", result);
if (result)
{
// Approve it
request.Approved = true;
// Update the record
var inserted = Service.Update(request);
return Response.AsJson(inserted
? new JsonResponseModel {Result = true}
: new JsonResponseModel
{
Result = false,
Message = "We could not approve this request. Please try again or check the logs."
});
}
return
Response.AsJson(
new
{
Result = false,
Message =
"Something went wrong adding the movie to CouchPotato! Please check your settings."
});
}
/// <summary>
@ -88,18 +143,36 @@ namespace PlexRequests.UI.Modules
/// <returns></returns>
private Response ApproveAll()
{
var requests = Service.GetAll();
var requests = Service.GetAll().Where(x => x.Approved == false);
var requestedModels = requests as RequestedModel[] ?? requests.ToArray();
if (!requestedModels.Any())
{
return Response.AsJson(new JsonResponseModel { Result = false, Message = "There are no requests to approve. Please refresh." });
}
var cpSettings = CpService.GetSettings();
var updatedRequests = new List<RequestedModel>();
foreach (var r in requestedModels)
{
r.Approved = true;
updatedRequests.Add(r);
if (r.Type == RequestType.Movie)
{
var result = SendMovie(cpSettings, r, CpApi);
if (result)
{
r.Approved = true;
updatedRequests.Add(r);
}
else
{
Log.Error("Could not approve send the movie {0} to couch potato!", r.Title);
}
}
if (r.Type == RequestType.TvShow)
{
// TODO
}
}
try
{
@ -116,5 +189,14 @@ namespace PlexRequests.UI.Modules
}
}
private bool SendMovie(CouchPotatoSettings settings, RequestedModel r, ICouchPotatoApi cp)
{
Log.Info("Adding movie to CP : {0}", r.Title);
var result = cp.AddMovie(r.ImdbId, settings.ApiKey, r.Title, settings.FullUri);
Log.Trace("Adding movie to CP result {0}", result);
return result;
}
}
}

View file

@ -32,6 +32,7 @@ using Nancy.Responses.Negotiation;
using PlexRequests.Api.Interfaces;
using PlexRequests.Api.Models;
using PlexRequests.Api.Models.Plex;
using PlexRequests.Core;
using PlexRequests.Core.SettingModels;
using PlexRequests.UI.Models;
@ -61,10 +62,17 @@ namespace PlexRequests.UI.Modules
private Response LoginUser()
{
var username = Request.Form.username.Value;
if (string.IsNullOrWhiteSpace(username))
{
return Response.AsJson(new JsonResponseModel { Result = false, Message = "Incorrect User or Password" });
}
var authenticated = false;
var settings = AuthService.GetSettings();
var username = Request.Form.username.Value;
if (IsUserInDeniedList(username, settings))
{

View file

@ -4,7 +4,9 @@
<h4>Below you can see yours and all other requests, as well as their download and approval status.</h4>
@if (Context.CurrentUser.IsAuthenticated())
{
<button id="approveAll" class="btn btn-primary" type="submit"><i class="fa fa-plus"></i> Approve All</button>
<button id="approveAll" class="btn btn-success" type="submit"><i class="fa fa-plus"></i> Approve All</button>
<br/>
<br/>
}
<!-- Nav tabs -->
<ul id="nav-tabs" class="nav nav-tabs" role="tablist">
@ -50,7 +52,7 @@
<script id="search-template" type="text/x-handlebars-template">
<div id="{{id}}Template">
<div id="{{requestId}}Template">
<div class="row">
<div class="col-sm-2">
{{#if_eq type "movie"}}
@ -76,7 +78,7 @@
<p>
Approved:
{{#if_eq approved false}}
<i class="fa fa-times"></i>
<i id="{{requestId}}notapproved" class="fa fa-times"></i>
{{/if_eq}}
{{#if_eq approved true}}
<i class="fa fa-check"></i>
@ -105,6 +107,12 @@
<br />
<br />
{{#if_eq admin true}}
{{#if_eq approved false}}
<form method="POST" action="/approval/approve" id="approve{{requestId}}">
<input name="requestId" type="text" value="{{requestId}}" hidden="hidden" />
<button id="{{requestId}}" custom-button="{{requestId}}" style="text-align: right" class="btn btn-success approve" type="submit"><i class="fa fa-plus"></i> Approve</button>
</form>
{{/if_eq}}
<form method="POST" action="/requests/delete" id="delete{{requestId}}">
<input name="Id" type="text" value="{{requestId}}" hidden="hidden" />
<button id="{{requestId}}" style="text-align: right" class="btn btn-danger delete" type="submit"><i class="fa fa-plus"></i> Remove</button>