Some error handling and ensure we are an admin to delete requests.

Also started on the approval of everything
This commit is contained in:
tidusjar 2016-03-08 14:26:26 +00:00
commit 0942bfcbcc
18 changed files with 436 additions and 58 deletions

View file

@ -0,0 +1,120 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: ApprovalModule.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using Nancy;
using Nancy.Security;
using NLog;
using PlexRequests.Store;
using PlexRequests.UI.Models;
namespace PlexRequests.UI.Modules
{
public class ApprovalModule : BaseModule
{
public ApprovalModule(IRepository<RequestedModel> service) : base("approval")
{
this.RequiresAuthentication();
Service = service;
Post["/approve"] = parameters => Approve((int)Request.Form.requestid);
Post["/approveall"] = x => ApproveAll();
}
private IRepository<RequestedModel> Service { get; set; }
private static Logger Log = LogManager.GetCurrentClassLogger();
/// <summary>
/// Approves the specified request identifier.
/// </summary>
/// <param name="requestId">The request identifier.</param>
/// <returns></returns>
private Response Approve(int requestId)
{
// Get the request from the DB
var request = Service.Get(requestId);
if (request == null)
{
Log.Warn("Tried approving a request, but the request did not exist in the database, requestId = {0}", requestId);
return Response.AsJson(new JsonResponseModel { Result = false, Message = "There are no requests to approve. Please refresh." });
}
// Approve it
request.Approved = true;
// Update the record
var result = Service.Update(request);
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." });
}
/// <summary>
/// Approves all.
/// </summary>
/// <returns></returns>
private Response ApproveAll()
{
var requests = Service.GetAll();
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 updatedRequests = new List<RequestedModel>();
foreach (var r in requestedModels)
{
r.Approved = true;
updatedRequests.Add(r);
}
try
{
var result = Service.UpdateAll(updatedRequests); return Response.AsJson(result
? new JsonResponseModel { Result = true }
: new JsonResponseModel { Result = false, Message = "We could not approve all of the requests. Please try again or check the logs." });
}
catch (Exception e)
{
Log.Fatal(e);
return Response.AsJson(new JsonResponseModel { Result = false, Message = "Something bad happened, please check the logs!" });
}
}
}
}

View file

@ -32,6 +32,8 @@ using Humanizer;
using Nancy;
using Nancy.Responses.Negotiation;
using Nancy.Security;
using PlexRequests.Api;
using PlexRequests.Core;
using PlexRequests.Core.SettingModels;
@ -67,11 +69,12 @@ namespace PlexRequests.UI.Modules
private Negotiator LoadRequests()
{
var settings = PrSettings.GetSettings();
return View["Requests/Index", settings];
return View["Index", settings];
}
private Response GetMovies()
{
var isAdmin = Context.CurrentUser.IsAuthenticated();
var dbMovies = Service.GetAll().Where(x => x.Type == RequestType.Movie);
var viewModel = dbMovies.Select(movie => new RequestViewModel
{
@ -88,7 +91,8 @@ namespace PlexRequests.UI.Modules
Overview = movie.Overview,
RequestedBy = movie.RequestedBy,
ReleaseYear = movie.ReleaseDate.Year.ToString(),
Available = movie.Available
Available = movie.Available,
Admin = isAdmin
}).ToList();
return Response.AsJson(viewModel);
@ -96,6 +100,7 @@ namespace PlexRequests.UI.Modules
private Response GetTvShows()
{
var isAdmin = Context.CurrentUser.IsAuthenticated();
var dbTv = Service.GetAll().Where(x => x.Type == RequestType.TvShow);
var viewModel = dbTv.Select(tv => new RequestViewModel
{
@ -112,7 +117,8 @@ namespace PlexRequests.UI.Modules
Overview = tv.Overview,
RequestedBy = tv.RequestedBy,
ReleaseYear = tv.ReleaseDate.Year.ToString(),
Available = tv.Available
Available = tv.Available,
Admin = isAdmin
}).ToList();
return Response.AsJson(viewModel);
@ -120,9 +126,13 @@ namespace PlexRequests.UI.Modules
private Response DeleteRequest(int providerId, RequestType type)
{
var currentEntity = Service.GetAll().FirstOrDefault(x => x.ProviderId == providerId && x.Type == type);
Service.Delete(currentEntity);
return Response.AsJson(new { Result = true });
if (Context.CurrentUser.IsAuthenticated())
{
var currentEntity = Service.GetAll().FirstOrDefault(x => x.ProviderId == providerId && x.Type == type);
Service.Delete(currentEntity);
return Response.AsJson(new JsonResponseModel { Result = true });
}
return Response.AsJson(new JsonResponseModel { Result = false, Message = "You are not an Admin, so you cannot delete any requests." });
}
}
}