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

106 lines
3.3 KiB

9 years ago
using System.Linq;
using System.Threading.Tasks;
using Nancy;
using Nancy.Responses.Negotiation;
using PlexRequests.Core;
using PlexRequests.Core.Models;
9 years ago
using PlexRequests.Core.SettingModels;
using PlexRequests.Store;
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();
Get["/issuecount", true] = async (x, ct) => await IssueCount();
Get["/details/{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);
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
public Negotiator Index()
{
return View["Index"];
}
public async Task<Response> IssueCount()
{
var issues = await IssuesService.GetAllAsync();
var count = issues.Count(x => x.Deleted == false);
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,
IssueStatus = IssueStatus.PendingIssue,
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,
Type = request.Type
};
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
}
}