You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Ombi/PlexRequests.UI/Modules/IssuesModule.cs

180 lines
6.0 KiB

9 years ago
using System;
using System.Collections.Generic;
using System.Linq;
9 years ago
using System.Threading.Tasks;
using System.Web.UI.WebControls;
9 years ago
using Nancy;
using Nancy.Extensions;
9 years ago
using Nancy.Responses.Negotiation;
9 years ago
using Nancy.Security;
using NLog;
9 years ago
using PlexRequests.Core;
using PlexRequests.Core.Models;
9 years ago
using PlexRequests.Core.SettingModels;
9 years ago
using PlexRequests.Helpers;
using PlexRequests.Store;
using PlexRequests.UI.Helpers;
using PlexRequests.UI.Models;
9 years ago
namespace PlexRequests.UI.Modules
{
public class IssuesModule : BaseAuthModule
{
public IssuesModule(ISettingsService<PlexRequestSettings> pr, IIssueService issueService, IRequestService request) : base("issues", pr)
9 years ago
{
IssuesService = issueService;
RequestService = request;
9 years ago
Get["/"] = x => Index();
9 years ago
Get["/{id}", true] = async (x, ct) => await Details(x.id);
Post["/issue", true] = async (x, ct) => await ReportIssue((int)Request.Form.requestId, (IssueState)(int)Request.Form.issue, null);
9 years ago
Get["/inprogress", true] = async (x, ct) => await GetIssues(IssueStatus.InProgressIssue);
Get["/pending", true] = async (x, ct) => await GetIssues(IssueStatus.PendingIssue);
Get["/resolved", true] = async (x, ct) => await GetIssues(IssueStatus.ResolvedIssue);
9 years ago
Post["/remove", true] = async (x, ct) => await RemoveIssue((int)Request.Form.issueId);
Get["/issuecount", true] = async (x, ct) => await IssueCount();
Post["/issuecomment", true] = async (x, ct) => await ReportIssue((int)Request.Form.requestId, IssueState.Other, (string)Request.Form.commentArea);
9 years ago
}
private IIssueService IssuesService { get; }
private IRequestService RequestService { get; }
9 years ago
private static Logger Log = LogManager.GetCurrentClassLogger();
9 years ago
public Negotiator Index()
{
return View["Index"];
}
private async Task<Response> GetIssues(IssueStatus status)
9 years ago
{
var issues = await IssuesService.GetAllAsync();
var issuesModels = issues as IssuesModel[] ?? issues.Where(x => x.IssueStatus == status).ToArray();
var model = issuesModels.Select(i => new IssuesViewModel
{
Title = i.Title, Type = i.Type.ToString().CamelCaseToWords(), Count = i.Issues.Count, Id = i.Id, RequestId = i.RequestId
}).ToList();
return Response.AsJson(model);
9 years ago
}
9 years ago
public async Task<Response> IssueCount()
{
var issues = await IssuesService.GetAllAsync();
9 years ago
9 years ago
var myIssues = await FilterIssues(issues);
9 years ago
var count = myIssues.Count();
9 years ago
return Response.AsJson(count);
}
public async Task<Negotiator> Details(int id)
{
var issue = await IssuesService.GetAsync(id);
return issue == null
? Index()
9 years ago
: View["Details", issue];
}
private async Task<Response> ReportIssue(int requestId, IssueState issue, string comment)
{
var model = new IssueModel
{
Issue = issue,
UserReported = Username,
UserNote = !string.IsNullOrEmpty(comment)
? $"{Username} - {comment}"
: string.Empty,
};
var request = await RequestService.GetAsync(requestId);
var issueEntity = await IssuesService.GetAllAsync();
var existingIssue = issueEntity.FirstOrDefault(x => x.RequestId == requestId);
// An issue already exists
if (existingIssue != null)
{
existingIssue.Issues.Add(model);
var result = await IssuesService.UpdateIssueAsync(existingIssue);
9 years ago
return Response.AsJson(result
? new JsonResponseModel { Result = true }
: new JsonResponseModel { Result = false });
}
// New issue
var issues = new IssuesModel
{
Title = request.Title,
PosterUrl = request.PosterPath,
RequestId = requestId,
9 years ago
Type = request.Type,
IssueStatus = IssueStatus.PendingIssue
};
issues.Issues.Add(model);
var issueId = await IssuesService.AddIssueAsync(issues);
request.IssueId = issueId;
await RequestService.UpdateRequestAsync(request);
return Response.AsJson(new JsonResponseModel { Result = true });
}
9 years ago
private async Task<IEnumerable<IssueModel>> FilterIssues(IEnumerable<IssuesModel> issues)
{
var settings = await PlexRequestSettings.GetSettingsAsync();
IEnumerable<IssueModel> myIssues;
if (IsAdmin)
{
myIssues = issues.Where(x => x.Deleted == false).SelectMany(i => i.Issues);
}
else if (settings.UsersCanViewOnlyOwnRequests)
{
myIssues = (from issuesModel in issues
from i in issuesModel.Issues
where i.UserReported.Equals(Username, StringComparison.CurrentCultureIgnoreCase)
select i).ToList();
}
else
{
myIssues = issues.Where(x => x.Deleted == false).SelectMany(i => i.Issues);
}
return myIssues;
}
9 years ago
private async Task<Response> RemoveIssue(int issueId)
{
try
{
this.RequiresClaims(UserClaims.PowerUser);
await IssuesService.DeleteIssueAsync(issueId);
return Response.AsJson(new JsonResponseModel {Result = true, Message = "Issue Removed"});
}
catch (Exception e)
{
Log.Error(e);
return Response.AsJson(new JsonResponseModel { Result = false, Message = "Looks like we couldn't remove the issue. Check the logs!" });
}
}
9 years ago
}
}