diff --git a/PlexRequests.Core/IRequestService.cs b/PlexRequests.Core/IRequestService.cs index fb68fab37..fc5187aed 100644 --- a/PlexRequests.Core/IRequestService.cs +++ b/PlexRequests.Core/IRequestService.cs @@ -33,7 +33,7 @@ namespace PlexRequests.Core public interface IRequestService { long AddRequest(RequestedModel model); - bool CheckRequest(int providerId); + RequestedModel CheckRequest(int providerId); void DeleteRequest(RequestedModel request); bool UpdateRequest(RequestedModel model); RequestedModel Get(int id); diff --git a/PlexRequests.Core/JsonRequestService.cs b/PlexRequests.Core/JsonRequestService.cs index 64033b50c..45eccb51e 100644 --- a/PlexRequests.Core/JsonRequestService.cs +++ b/PlexRequests.Core/JsonRequestService.cs @@ -58,10 +58,11 @@ namespace PlexRequests.Core return result ? id : -1; } - public bool CheckRequest(int providerId) + public RequestedModel CheckRequest(int providerId) { var blobs = Repo.GetAll(); - return blobs.Any(x => x.ProviderId == providerId); + var blob = blobs.FirstOrDefault(x => x.ProviderId == providerId); + return blob != null ? ByteConverterHelper.ReturnObject(blob.Content) : null; } public void DeleteRequest(RequestedModel request) diff --git a/PlexRequests.Store/RequestedModel.cs b/PlexRequests.Store/RequestedModel.cs index 18ef216af..34a9f181c 100644 --- a/PlexRequests.Store/RequestedModel.cs +++ b/PlexRequests.Store/RequestedModel.cs @@ -2,6 +2,8 @@ using System.Security.Cryptography; using Dapper.Contrib.Extensions; +using System.Collections.Generic; +using System.Linq; namespace PlexRequests.Store { @@ -18,7 +20,10 @@ namespace PlexRequests.Store public RequestType Type { get; set; } public string Status { get; set; } public bool Approved { get; set; } + + [Obsolete("Use RequestedUsers")] public string RequestedBy { get; set; } + public DateTime RequestedDate { get; set; } public bool Available { get; set; } public IssueState Issues { get; set; } @@ -27,6 +32,17 @@ namespace PlexRequests.Store public int[] SeasonList { get; set; } public int SeasonCount { get; set; } public string SeasonsRequested { get; set; } + public List RequestedUsers { get; set; } + + public bool UserHasRequested(string username) + { + bool alreadyRequested = !string.IsNullOrEmpty(RequestedBy) && RequestedBy.Equals(username, StringComparison.OrdinalIgnoreCase); + if (!alreadyRequested && RequestedUsers != null && RequestedUsers.Count > 0) + { + alreadyRequested = RequestedUsers.Any(x => x.Equals(username, StringComparison.OrdinalIgnoreCase)); + } + return alreadyRequested; + } } public enum RequestType diff --git a/PlexRequests.UI/Content/requests.js b/PlexRequests.UI/Content/requests.js index 22fba0978..6b6d00572 100644 --- a/PlexRequests.UI/Content/requests.js +++ b/PlexRequests.UI/Content/requests.js @@ -433,7 +433,7 @@ function buildRequestContext(result, type) { releaseDate: result.releaseDate, releaseDateTicks: result.releaseDateTicks, approved: result.approved, - requestedBy: result.requestedBy, + requestedUsers: result.requestedUsers ? result.requestedUsers.join(', ') : '', requestedDate: result.requestedDate, requestedDateTicks: result.requestedDateTicks, available: result.available, diff --git a/PlexRequests.UI/Models/RequestViewModel.cs b/PlexRequests.UI/Models/RequestViewModel.cs index 5ee389be9..292bc39df 100644 --- a/PlexRequests.UI/Models/RequestViewModel.cs +++ b/PlexRequests.UI/Models/RequestViewModel.cs @@ -41,7 +41,7 @@ namespace PlexRequests.UI.Models public RequestType Type { get; set; } public string Status { get; set; } public bool Approved { get; set; } - public string RequestedBy { get; set; } + public string[] RequestedUsers { get; set; } public string RequestedDate { get; set; } public long RequestedDateTicks { get; set; } public string ReleaseYear { get; set; } diff --git a/PlexRequests.UI/Modules/RequestsModule.cs b/PlexRequests.UI/Modules/RequestsModule.cs index 6651b1c8a..9d43b34a7 100644 --- a/PlexRequests.UI/Modules/RequestsModule.cs +++ b/PlexRequests.UI/Modules/RequestsModule.cs @@ -84,7 +84,7 @@ namespace PlexRequests.UI.Modules var dbMovies = Service.GetAll().Where(x => x.Type == RequestType.Movie); if (settings.UsersCanViewOnlyOwnRequests && !isAdmin) { - dbMovies = dbMovies.Where(x => x.RequestedBy.Equals(Session[SessionKeys.UsernameKey].ToString(), StringComparison.OrdinalIgnoreCase)); + dbMovies = dbMovies.Where(x => x.UserHasRequested(Session[SessionKeys.UsernameKey].ToString())); } var viewModel = dbMovies.Select(movie => new RequestViewModel @@ -102,7 +102,7 @@ namespace PlexRequests.UI.Modules Approved = movie.Available || movie.Approved, Title = movie.Title, Overview = movie.Overview, - RequestedBy = movie.RequestedBy, + RequestedUsers = isAdmin ? movie.RequestedUsers.ToArray() : new string[] { }, ReleaseYear = movie.ReleaseDate.Year.ToString(), Available = movie.Available, Admin = isAdmin, @@ -121,7 +121,7 @@ namespace PlexRequests.UI.Modules var dbTv = Service.GetAll().Where(x => x.Type == RequestType.TvShow); if (settings.UsersCanViewOnlyOwnRequests && !isAdmin) { - dbTv = dbTv.Where(x => x.RequestedBy.Equals(Session[SessionKeys.UsernameKey].ToString(), StringComparison.OrdinalIgnoreCase)); + dbTv = dbTv.Where(x => x.UserHasRequested(Session[SessionKeys.UsernameKey].ToString())); } var viewModel = dbTv.Select(tv => new RequestViewModel @@ -139,7 +139,7 @@ namespace PlexRequests.UI.Modules Approved = tv.Available || tv.Approved, Title = tv.Title, Overview = tv.Overview, - RequestedBy = tv.RequestedBy, + RequestedUsers = isAdmin ? tv.RequestedUsers.ToArray() : new string[] { }, ReleaseYear = tv.ReleaseDate.Year.ToString(), Available = tv.Available, Admin = isAdmin, diff --git a/PlexRequests.UI/Modules/SearchModule.cs b/PlexRequests.UI/Modules/SearchModule.cs index a57bd307b..59614a0c2 100644 --- a/PlexRequests.UI/Modules/SearchModule.cs +++ b/PlexRequests.UI/Modules/SearchModule.cs @@ -179,11 +179,20 @@ namespace PlexRequests.UI.Modules Log.Trace(movieInfo.DumpJson); //#if !DEBUG + var settings = PrService.GetSettings(); + + // check if the movie has already been requested Log.Info("Requesting movie with id {0}", movieId); - if (RequestService.CheckRequest(movieId)) + var existingRequest = RequestService.CheckRequest(movieId); + if (existingRequest != null) { - Log.Trace("movie with id {0} exists", movieId); - return Response.AsJson(new JsonResponseModel { Result = false, Message = $"{fullMovieName} has already been requested!" }); + // check if the current user is already marked as a requester for this movie, if not, add them + if (!existingRequest.UserHasRequested(Session[SessionKeys.UsernameKey].ToString())) + { + existingRequest.RequestedUsers.Add(Session[SessionKeys.UsernameKey].ToString()); + RequestService.UpdateRequest(existingRequest); + } + return Response.AsJson(new JsonResponseModel { Result = false, Message = settings.UsersCanViewOnlyOwnRequests ? $"{fullMovieName} was successfully added!" : $"{fullMovieName} has already been requested!" }); } Log.Debug("movie with id {0} doesnt exists", movieId); @@ -213,12 +222,10 @@ namespace PlexRequests.UI.Modules Status = movieInfo.Status, RequestedDate = DateTime.Now, Approved = false, - RequestedBy = Session[SessionKeys.UsernameKey].ToString(), + RequestedUsers = new List() { Session[SessionKeys.UsernameKey].ToString() }, Issues = IssueState.None, }; - - var settings = PrService.GetSettings(); Log.Trace(settings.DumpJson()); if (!settings.RequireMovieApproval || settings.NoApprovalUserList.Any(x => x.Equals(Session[SessionKeys.UsernameKey].ToString(), StringComparison.OrdinalIgnoreCase))) { @@ -310,9 +317,20 @@ namespace PlexRequests.UI.Modules string fullShowName = $"{showInfo.name} ({firstAir.Year})"; //#if !DEBUG - if (RequestService.CheckRequest(showId)) + var settings = PrService.GetSettings(); + + // check if the show has already been requested + Log.Info("Requesting tv show with id {0}", showId); + var existingRequest = RequestService.CheckRequest(showId); + if (existingRequest != null) { - return Response.AsJson(new JsonResponseModel { Result = false, Message = $"{fullShowName} has already been requested!" }); + // check if the current user is already marked as a requester for this show, if not, add them + if (!existingRequest.UserHasRequested(Session[SessionKeys.UsernameKey].ToString())) + { + existingRequest.RequestedUsers.Add(Session[SessionKeys.UsernameKey].ToString()); + RequestService.UpdateRequest(existingRequest); + } + return Response.AsJson(new JsonResponseModel { Result = false, Message = settings.UsersCanViewOnlyOwnRequests ? $"{fullShowName} was successfully added!" : $"{fullShowName} has already been requested!" }); } try @@ -340,7 +358,7 @@ namespace PlexRequests.UI.Modules Status = showInfo.status, RequestedDate = DateTime.Now, Approved = false, - RequestedBy = Session[SessionKeys.UsernameKey].ToString(), + RequestedUsers = new List() { Session[SessionKeys.UsernameKey].ToString() }, Issues = IssueState.None, ImdbId = showInfo.externals?.imdb ?? string.Empty, SeasonCount = showInfo.seasonCount @@ -363,7 +381,6 @@ namespace PlexRequests.UI.Modules model.SeasonList = seasonsList.ToArray(); - var settings = PrService.GetSettings(); if (!settings.RequireTvShowApproval || settings.NoApprovalUserList.Any(x => x.Equals(Session[SessionKeys.UsernameKey].ToString(), StringComparison.OrdinalIgnoreCase))) { var sonarrSettings = SonarrService.GetSettings(); diff --git a/PlexRequests.UI/Views/Requests/Index.cshtml b/PlexRequests.UI/Views/Requests/Index.cshtml index 11d8dd33e..a206b383c 100644 --- a/PlexRequests.UI/Views/Requests/Index.cshtml +++ b/PlexRequests.UI/Views/Requests/Index.cshtml @@ -140,7 +140,7 @@ {{#if_eq type "tv"}}
Series Requested: {{seriesRequested}}
{{/if_eq}} -
Requested By: {{requestedBy}}
+
Requested By: {{requestedUsers}}
Requested Date: {{requestedDate}}
{{#if otherMessage}}