From f03392691c55db9cf6cc637ef2d8c9106be14752 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Tue, 26 Jul 2016 12:59:39 +0100 Subject: [PATCH] Reworking the login page for #426 --- PlexRequests.UI/Modules/BaseModule.cs | 13 +++- PlexRequests.UI/Modules/SearchModule.cs | 81 +++++++++----------- PlexRequests.UI/Modules/UserLoginModule.cs | 30 +++++--- PlexRequests.UI/Views/Search/Index.cshtml | 6 -- PlexRequests.UI/Views/UserLogin/Index.cshtml | 36 +++++++-- 5 files changed, 98 insertions(+), 68 deletions(-) diff --git a/PlexRequests.UI/Modules/BaseModule.cs b/PlexRequests.UI/Modules/BaseModule.cs index 7bfa7eeb6..d0aa8006c 100644 --- a/PlexRequests.UI/Modules/BaseModule.cs +++ b/PlexRequests.UI/Modules/BaseModule.cs @@ -69,7 +69,18 @@ namespace PlexRequests.UI.Modules ModulePath = settingModulePath; - Before += (ctx) => SetCookie(); + Before += (ctx) => + { + SetCookie(); + + if (!string.IsNullOrEmpty(ctx.Request.Session["TempMessage"] as string)) + { + ctx.ViewBag.TempMessage = ctx.Request.Session["TempMessage"]; + ctx.ViewBag.TempType = ctx.Request.Session["TempType"]; + ctx.Request.Session.DeleteAll(); + } + return null; + }; } private int _dateTimeOffset = -1; diff --git a/PlexRequests.UI/Modules/SearchModule.cs b/PlexRequests.UI/Modules/SearchModule.cs index e21150dbd..bc53733d6 100644 --- a/PlexRequests.UI/Modules/SearchModule.cs +++ b/PlexRequests.UI/Modules/SearchModule.cs @@ -183,48 +183,43 @@ namespace PlexRequests.UI.Modules private async Task ProcessMovies(MovieSearchType searchType, string searchTerm) { - var apiMovies = new List(); - await Task.Factory.StartNew( - () => - { - switch (searchType) - { - case MovieSearchType.Search: - return - MovieApi.SearchMovie(searchTerm) - .Result.Select( - x => - new MovieResult() - { - Adult = x.Adult, - BackdropPath = x.BackdropPath, - GenreIds = x.GenreIds, - Id = x.Id, - OriginalLanguage = x.OriginalLanguage, - OriginalTitle = x.OriginalTitle, - Overview = x.Overview, - Popularity = x.Popularity, - PosterPath = x.PosterPath, - ReleaseDate = x.ReleaseDate, - Title = x.Title, - Video = x.Video, - VoteAverage = x.VoteAverage, - VoteCount = x.VoteCount - }) - .ToList(); - case MovieSearchType.CurrentlyPlaying: - return MovieApi.GetCurrentPlayingMovies().Result.ToList(); - case MovieSearchType.Upcoming: - return MovieApi.GetUpcomingMovies().Result.ToList(); - default: - return new List(); - } - }).ContinueWith( - (t) => - { - apiMovies = t.Result; - }); + List apiMovies; + switch (searchType) + { + case MovieSearchType.Search: + var movies = await MovieApi.SearchMovie(searchTerm); + apiMovies = movies.Select(x => + new MovieResult() + { + Adult = x.Adult, + BackdropPath = x.BackdropPath, + GenreIds = x.GenreIds, + Id = x.Id, + OriginalLanguage = x.OriginalLanguage, + OriginalTitle = x.OriginalTitle, + Overview = x.Overview, + Popularity = x.Popularity, + PosterPath = x.PosterPath, + ReleaseDate = x.ReleaseDate, + Title = x.Title, + Video = x.Video, + VoteAverage = x.VoteAverage, + VoteCount = x.VoteCount + }) + .ToList(); + break; + case MovieSearchType.CurrentlyPlaying: + apiMovies = await MovieApi.GetCurrentPlayingMovies(); + break; + case MovieSearchType.Upcoming: + apiMovies = await MovieApi.GetUpcomingMovies(); + break; + default: + apiMovies = new List(); + break; + } + var allResults = await RequestService.GetAllAsync(); allResults = allResults.Where(x => x.Type == RequestType.Movie); @@ -236,7 +231,7 @@ namespace PlexRequests.UI.Modules var plexMovies = Checker.GetPlexMovies(); var settings = await PrService.GetSettingsAsync(); var viewMovies = new List(); - foreach (MovieResult movie in apiMovies) + foreach (var movie in apiMovies) { var viewMovie = new SearchMovieViewModel { @@ -437,7 +432,7 @@ namespace PlexRequests.UI.Modules } Analytics.TrackEventAsync(Category.Search, Action.Request, "Movie", Username, CookieHelper.GetAnalyticClientId(Cookies)); - var movieInfo = MovieApi.GetMovieInformation(movieId).Result; + var movieInfo = await MovieApi.GetMovieInformation(movieId); var fullMovieName = $"{movieInfo.Title}{(movieInfo.ReleaseDate.HasValue ? $" ({movieInfo.ReleaseDate.Value.Year})" : string.Empty)}"; var existingRequest = await RequestService.CheckRequestAsync(movieId); diff --git a/PlexRequests.UI/Modules/UserLoginModule.cs b/PlexRequests.UI/Modules/UserLoginModule.cs index f9be1c19c..9baffdc1e 100644 --- a/PlexRequests.UI/Modules/UserLoginModule.cs +++ b/PlexRequests.UI/Modules/UserLoginModule.cs @@ -31,6 +31,7 @@ using System.Threading.Tasks; using Nancy; using Nancy.Extensions; +using Nancy.Linker; using Nancy.Responses.Negotiation; using NLog; @@ -49,16 +50,18 @@ namespace PlexRequests.UI.Modules { public class UserLoginModule : BaseModule { - public UserLoginModule(ISettingsService auth, IPlexApi api, ISettingsService plexSettings, ISettingsService pr, ISettingsService lp, IAnalytics a) : base("userlogin", pr) + public UserLoginModule(ISettingsService auth, IPlexApi api, ISettingsService plexSettings, ISettingsService pr, + ISettingsService lp, IAnalytics a, IResourceLinker linker) : base("userlogin", pr) { AuthService = auth; LandingPageSettings = lp; Analytics = a; Api = api; PlexSettings = plexSettings; + Linker = linker; - Get["UserLoginIndex","/", true] = async (x, ct) => await Index(); - Post["/", true] = async (x,ct) => await LoginUser(); + Get["UserLoginIndex", "/", true] = async (x, ct) => await Index(); + Post["/", true] = async (x, ct) => await LoginUser(); Get["/logout"] = x => Logout(); } @@ -66,13 +69,13 @@ namespace PlexRequests.UI.Modules private ISettingsService LandingPageSettings { get; } private ISettingsService PlexSettings { get; } private IPlexApi Api { get; } + private IResourceLinker Linker { get; } private IAnalytics Analytics { get; } private static Logger Log = LogManager.GetCurrentClassLogger(); public async Task Index() { - var settings = await AuthService.GetSettingsAsync(); return View["Index", settings]; } @@ -84,7 +87,9 @@ namespace PlexRequests.UI.Modules Log.Debug("Username \"{0}\" attempting to login", username); if (string.IsNullOrWhiteSpace(username)) { - return Response.AsJson(new JsonResponseModel { Result = false, Message = "Incorrect User or Password" }); + Session["TempMessage"] = "username"; + var uri = Linker.BuildAbsoluteUri(Context, "UserLoginIndex"); + return Response.AsRedirect(uri.ToString()); // TODO Check this } var authenticated = false; @@ -95,7 +100,8 @@ namespace PlexRequests.UI.Modules if (IsUserInDeniedList(username, settings)) { Log.Debug("User is in denied list, not allowing them to authenticate"); - return Response.AsJson(new JsonResponseModel { Result = false, Message = "Incorrect User or Password" }); + var uri = Linker.BuildAbsoluteUri(Context, "UserLoginIndex", "error=check"); + return Response.AsRedirect(uri.ToString()); // TODO Check this } var password = string.Empty; @@ -153,7 +159,8 @@ namespace PlexRequests.UI.Modules if (!authenticated) { - return Response.AsJson(new JsonResponseModel { Result = false, Message = "Incorrect User or Password" }); + var uri = Linker.BuildAbsoluteUri(Context, "UserLoginIndex", "error=incorrect"); + return Response.AsRedirect(uri.ToString()); // TODO Check this } var landingSettings = LandingPageSettings.GetSettings(); @@ -161,16 +168,19 @@ namespace PlexRequests.UI.Modules if (landingSettings.Enabled) { if (!landingSettings.BeforeLogin) - return Response.AsJson(new JsonResponseModel { Result = true, Message = "landing" }); + { + var uri = Linker.BuildAbsoluteUri(Context, "LandingPageIndex"); + return Response.AsRedirect(uri.ToString()); + } } - return Response.AsJson(new JsonResponseModel { Result = true, Message = "search" }); + var retVal = Linker.BuildAbsoluteUri(Context, "SearchIndex"); + return Response.AsRedirect(retVal.ToString()); // TODO Check this } private Response Logout() { - Log.Debug("Logging Out"); if (Session[SessionKeys.UsernameKey] != null) { Session.Delete(SessionKeys.UsernameKey); diff --git a/PlexRequests.UI/Views/Search/Index.cshtml b/PlexRequests.UI/Views/Search/Index.cshtml index 2eed7b4dd..cb63a487c 100644 --- a/PlexRequests.UI/Views/Search/Index.cshtml +++ b/PlexRequests.UI/Views/Search/Index.cshtml @@ -157,15 +157,9 @@
- {{#if_eq type "movie"}} - -

{{title}} ({{year}})

-
- {{else}}

{{title}} ({{year}})

- {{/if_eq}}

{{overview}}

diff --git a/PlexRequests.UI/Views/UserLogin/Index.cshtml b/PlexRequests.UI/Views/UserLogin/Index.cshtml index ad8bf59fc..1dd021d7d 100644 --- a/PlexRequests.UI/Views/UserLogin/Index.cshtml +++ b/PlexRequests.UI/Views/UserLogin/Index.cshtml @@ -1,5 +1,14 @@ -@using PlexRequests.UI.Helpers + +@using PlexRequests.UI.Helpers @using PlexRequests.UI.Resources +@{ + var nullableError = Context.ViewBag.TempMessage ?? string.Empty; + var error = false; + if (!string.IsNullOrEmpty(nullableError)) + { + error = true; + } +}

@UI.UserLogin_Title

@@ -8,6 +17,7 @@

+
@@ -24,7 +34,7 @@
- +

@@ -36,15 +46,25 @@ \ No newline at end of file