From e037ad0f2b8e7a628a587421b6458ad27cf77b34 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Fri, 1 Apr 2016 23:05:17 +0100 Subject: [PATCH] Got the search finished up for #32 --- PlexRequests.Core/IRequestService.cs | 2 + PlexRequests.Core/JsonRequestService.cs | 7 ++ .../SettingModels/HeadphonesSettings.cs | 2 +- .../SettingModels/PlexRequestSettings.cs | 2 +- .../Repository/RequestJsonRepository.cs | 10 +-- PlexRequests.UI/Content/search.js | 22 +++++ PlexRequests.UI/Modules/SearchModule.cs | 89 +++++++++++++++++-- PlexRequests.UI/Views/Search/Index.cshtml | 2 +- 8 files changed, 118 insertions(+), 18 deletions(-) diff --git a/PlexRequests.Core/IRequestService.cs b/PlexRequests.Core/IRequestService.cs index fc5187aed..342e83d05 100644 --- a/PlexRequests.Core/IRequestService.cs +++ b/PlexRequests.Core/IRequestService.cs @@ -34,6 +34,8 @@ namespace PlexRequests.Core { long AddRequest(RequestedModel model); RequestedModel CheckRequest(int providerId); + RequestedModel CheckRequest(string musicId); + 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 21ddcecc7..4808cde3b 100644 --- a/PlexRequests.Core/JsonRequestService.cs +++ b/PlexRequests.Core/JsonRequestService.cs @@ -65,6 +65,13 @@ namespace PlexRequests.Core return blob != null ? ByteConverterHelper.ReturnObject(blob.Content) : null; } + public RequestedModel CheckRequest(string musicId) + { + var blobs = Repo.GetAll(); + var blob = blobs.FirstOrDefault(x => x.MusicId == musicId); + return blob != null ? ByteConverterHelper.ReturnObject(blob.Content) : null; + } + public void DeleteRequest(RequestedModel request) { var blob = Repo.Get(request.Id); diff --git a/PlexRequests.Core/SettingModels/HeadphonesSettings.cs b/PlexRequests.Core/SettingModels/HeadphonesSettings.cs index e77abca47..96deeb821 100644 --- a/PlexRequests.Core/SettingModels/HeadphonesSettings.cs +++ b/PlexRequests.Core/SettingModels/HeadphonesSettings.cs @@ -36,7 +36,7 @@ namespace PlexRequests.Core.SettingModels public bool Enabled { get; set; } public string Ip { get; set; } public int Port { get; set; } - public int ApiKey { get; set; } + public string ApiKey { get; set; } public bool Ssl { get; set; } public string SubDir { get; set; } diff --git a/PlexRequests.Core/SettingModels/PlexRequestSettings.cs b/PlexRequests.Core/SettingModels/PlexRequestSettings.cs index 76266cc07..4b4009def 100644 --- a/PlexRequests.Core/SettingModels/PlexRequestSettings.cs +++ b/PlexRequests.Core/SettingModels/PlexRequestSettings.cs @@ -45,7 +45,7 @@ namespace PlexRequests.Core.SettingModels public string NoApprovalUsers { get; set; } [JsonIgnore] - public List NoApprovalUserList + public List ApprovalWhiteList { get { diff --git a/PlexRequests.Store/Repository/RequestJsonRepository.cs b/PlexRequests.Store/Repository/RequestJsonRepository.cs index d02a111b0..872e07745 100644 --- a/PlexRequests.Store/Repository/RequestJsonRepository.cs +++ b/PlexRequests.Store/Repository/RequestJsonRepository.cs @@ -37,13 +37,11 @@ namespace PlexRequests.Store.Repository public class RequestJsonRepository : IRequestRepository { private ICacheProvider Cache { get; } - - private string TypeName { get; } + public RequestJsonRepository(ISqliteConfiguration config, ICacheProvider cacheProvider) { Db = config; Cache = cacheProvider; - TypeName = typeof(RequestJsonRepository).Name; } private ISqliteConfiguration Db { get; } @@ -60,7 +58,7 @@ namespace PlexRequests.Store.Repository public IEnumerable GetAll() { - var key = TypeName + "GetAll"; + var key = "GetAll"; var item = Cache.GetOrSet(key, () => { using (var con = Db.DbConnection()) @@ -74,7 +72,7 @@ namespace PlexRequests.Store.Repository public RequestBlobs Get(int id) { - var key = TypeName + "Get" + id; + var key = "Get" + id; var item = Cache.GetOrSet(key, () => { using (var con = Db.DbConnection()) @@ -107,7 +105,7 @@ namespace PlexRequests.Store.Repository private void ResetCache() { Cache.Remove("Get"); - Cache.Remove(TypeName + "GetAll"); + Cache.Remove("GetAll"); } public bool UpdateAll(IEnumerable entity) diff --git a/PlexRequests.UI/Content/search.js b/PlexRequests.UI/Content/search.js index b21b6f409..126d65634 100644 --- a/PlexRequests.UI/Content/search.js +++ b/PlexRequests.UI/Content/search.js @@ -107,6 +107,28 @@ $(document).on("click", ".requestMovie", function (e) { }); +// Click Request for album +$(document).on("click", ".requestAlbum", function (e) { + e.preventDefault(); + var buttonId = e.target.id; + if ($("#" + buttonId).attr('disabled')) { + return; + } + + $("#" + buttonId).prop("disabled", true); + loadingButton(buttonId, "primary"); + + + var $form = $('#form' + buttonId); + + var type = $form.prop('method'); + var url = $form.prop('action'); + var data = $form.serialize(); + + sendRequestAjax(data, type, url, buttonId); + +}); + function sendRequestAjax(data, type, url, buttonId) { $.ajax({ type: type, diff --git a/PlexRequests.UI/Modules/SearchModule.cs b/PlexRequests.UI/Modules/SearchModule.cs index 81124df26..ee4f518f5 100644 --- a/PlexRequests.UI/Modules/SearchModule.cs +++ b/PlexRequests.UI/Modules/SearchModule.cs @@ -86,7 +86,7 @@ namespace PlexRequests.UI.Modules Post["request/movie"] = parameters => RequestMovie((int)Request.Form.movieId); Post["request/tv"] = parameters => RequestTvShow((int)Request.Form.tvId, (string)Request.Form.seasons); - Post["request/album"] = parameters => RequestTvShow((int)Request.Form.tvId, (string)Request.Form.seasons); + Post["request/album"] = parameters => RequestAlbum((string)Request.Form.albumId); } private TheMovieDbApi MovieApi { get; } private INotificationService NotificationService { get; } @@ -261,7 +261,7 @@ namespace PlexRequests.UI.Modules }; Log.Trace(settings.DumpJson()); - if (!settings.RequireMovieApproval || settings.NoApprovalUserList.Any(x => x.Equals(Username, StringComparison.OrdinalIgnoreCase))) + if (!settings.RequireMovieApproval || settings.ApprovalWhiteList.Any(x => x.Equals(Username, StringComparison.OrdinalIgnoreCase))) { var cpSettings = CpService.GetSettings(); @@ -288,7 +288,7 @@ namespace PlexRequests.UI.Modules }; NotificationService.Publish(notificationModel); - return Response.AsJson(new JsonResponseModel {Result = true, Message = $"{fullMovieName} was successfully added!" }); + return Response.AsJson(new JsonResponseModel { Result = true, Message = $"{fullMovieName} was successfully added!" }); } return Response.AsJson(new JsonResponseModel @@ -380,7 +380,7 @@ namespace PlexRequests.UI.Modules } //#endif - + var model = new RequestedModel { ProviderId = showInfo.externals?.thetvdb ?? 0, @@ -415,7 +415,7 @@ namespace PlexRequests.UI.Modules model.SeasonList = seasonsList.ToArray(); - if (!settings.RequireTvShowApproval || settings.NoApprovalUserList.Any(x => x.Equals(Username, StringComparison.OrdinalIgnoreCase))) + if (!settings.RequireTvShowApproval || settings.ApprovalWhiteList.Any(x => x.Equals(Username, StringComparison.OrdinalIgnoreCase))) { var sonarrSettings = SonarrService.GetSettings(); var sender = new TvSender(SonarrApi, SickrageApi); @@ -476,10 +476,33 @@ namespace PlexRequests.UI.Modules private Response RequestAlbum(string releaseId) { - var settings = HeadphonesService.GetSettings(); + var settings = PrService.GetSettings(); + var existingRequest = RequestService.CheckRequest(releaseId); + Log.Debug("Checking for an existing request"); + + if (existingRequest != null) + { + Log.Debug("We do have an existing album request"); + if (!existingRequest.UserHasRequested(Username)) + { + Log.Debug("Not in the requested list so adding them and updating the request. User: {0}", Username); + existingRequest.RequestedUsers.Add(Username); + RequestService.UpdateRequest(existingRequest); + } + return Response.AsJson(new JsonResponseModel { Result = true, Message = settings.UsersCanViewOnlyOwnRequests ? $"{existingRequest.Title} was successfully added!" : $"{existingRequest.Title} has already been requested!" }); + } + + + Log.Debug("This is a new request"); var albumInfo = MusicBrainzApi.GetAlbum(releaseId); var img = GetMusicBrainzCoverArt(albumInfo.id); + + Log.Trace("Album Details:"); + Log.Trace(albumInfo.DumpJson()); + Log.Trace("CoverArt Details:"); + Log.Trace(img.DumpJson()); + var model = new RequestedModel { Title = albumInfo.title, @@ -487,11 +510,59 @@ namespace PlexRequests.UI.Modules Overview = albumInfo.disambiguation, PosterPath = img, Type = RequestType.Album, + ProviderId = 0, + RequestedUsers = new List() { Username }, + Status = albumInfo.status }; - - // TODO need to send to Headphones - return Response.AsJson(""); + + if (!settings.RequireMusicApproval || + settings.ApprovalWhiteList.Any(x => x.Equals(Username, StringComparison.OrdinalIgnoreCase))) + { + Log.Debug("We don't require approval OR the user is in the whitelist"); + var hpSettings = HeadphonesService.GetSettings(); + + Log.Trace("Headphone Settings:"); + Log.Trace(hpSettings.DumpJson()); + + if (!hpSettings.Enabled) + { + RequestService.AddRequest(model); + return + Response.AsJson(new JsonResponseModel + { + Result = true, + Message = $"{model.Title} was successfully added!" + }); + } + + var headphonesResult = HeadphonesApi.AddAlbum(hpSettings.ApiKey, hpSettings.FullUri, model.MusicBrainzId); + Log.Info("Result from adding album to Headphones = {0}", headphonesResult); + RequestService.AddRequest(model); + if (headphonesResult) + { + return + Response.AsJson(new JsonResponseModel + { + Result = true, + Message = $"{model.Title} was successfully added!" + }); + } + + return + Response.AsJson(new JsonResponseModel + { + Result = false, + Message = $"There was a problem adding {model.Title}. Please contact your admin!" + }); + } + + var result = RequestService.AddRequest(model); + return Response.AsJson(new JsonResponseModel + { + Result = true, + Message = $"{model.Title} was successfully added!" + }); } private string GetMusicBrainzCoverArt(string id) diff --git a/PlexRequests.UI/Views/Search/Index.cshtml b/PlexRequests.UI/Views/Search/Index.cshtml index aea09d6ac..ef9145ee7 100644 --- a/PlexRequests.UI/Views/Search/Index.cshtml +++ b/PlexRequests.UI/Views/Search/Index.cshtml @@ -182,7 +182,7 @@
- +
Track Count: {{trackCount}} Country: {{country}}