Renamed folders

This commit is contained in:
tidusjar 2016-03-03 10:26:16 +00:00
commit d1827cd3be
117 changed files with 16 additions and 16 deletions

View file

@ -0,0 +1,143 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: AdminModule.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using System.Dynamic;
using System.Linq;
using Nancy;
using Nancy.Extensions;
using Nancy.ModelBinding;
using Nancy.Responses.Negotiation;
using Nancy.Security;
using PlexRequests.Api;
using PlexRequests.Core;
using PlexRequests.Core.SettingModels;
using PlexRequests.UI.Models;
namespace PlexRequests.UI.Modules
{
public class AdminModule : NancyModule
{
private ISettingsService<RequestPlexSettings> RpService { get; set; }
private ISettingsService<CouchPotatoSettings> CpService { get; set; }
public AdminModule(ISettingsService<RequestPlexSettings> rpService, ISettingsService<CouchPotatoSettings> cpService ) : base("admin")
{
RpService = rpService;
CpService = cpService;
#if !DEBUG
this.RequiresAuthentication();
#endif
Get["/"] = _ => Admin();
Post["/"] = _ => SaveAdmin();
Post["/requestauth"] = _ => RequestAuthToken();
Get["/getusers"] = _ => GetUsers();
Get["/couchpotato"] = _ => CouchPotato();
Post["/couchpotato"] = _ => SaveCouchPotato();
}
private Negotiator Admin()
{
dynamic model = new ExpandoObject();
var settings = RpService.GetSettings();
model = settings;
return View["/Admin/Settings", model];
}
private Response SaveAdmin()
{
var model = this.Bind<RequestPlexSettings>();
RpService.SaveSettings(model);
return Context.GetRedirect("~/admin");
}
private Response RequestAuthToken()
{
var user = this.Bind<PlexAuth>();
if (string.IsNullOrEmpty(user.username) || string.IsNullOrEmpty(user.password))
{
return Context.GetRedirect("~/admin?error=true");
}
var plex = new PlexApi();
var model = plex.GetToken(user.username, user.password);
var oldSettings = RpService.GetSettings();
if (oldSettings != null)
{
oldSettings.PlexAuthToken = model.user.authentication_token;
RpService.SaveSettings(oldSettings);
}
else
{
var newModel = new RequestPlexSettings
{
PlexAuthToken = model.user.authentication_token
};
RpService.SaveSettings(newModel);
}
return Response.AsJson(new {Result = true, AuthToken = model.user.authentication_token});
}
private Response GetUsers()
{
var token = RpService.GetSettings().PlexAuthToken;
var api = new PlexApi();
var users = api.GetUsers(token);
var usernames = users.User.Select(x => x.Username);
return Response.AsJson(usernames); //TODO usernames are not populated.
}
private Negotiator CouchPotato()
{
dynamic model = new ExpandoObject();
var settings = CpService.GetSettings();
model = settings;
return View["/Admin/CouchPotato", model];
}
private Response SaveCouchPotato()
{
var couchPotatoSettings = this.Bind<CouchPotatoSettings>();
CpService.SaveSettings(couchPotatoSettings);
return Context.GetRedirect("~/admin/couchpotato");
}
}
}

View file

@ -0,0 +1,40 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: IndexModule.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using Nancy;
using Nancy.Extensions;
namespace PlexRequests.UI.Modules
{
public class IndexModule : NancyModule
{
public IndexModule()
{
Get["/"] = parameters => Context.GetRedirect("~/search");
Get["/Index"] = parameters => Context.GetRedirect("~/search");
}
}
}

View file

@ -0,0 +1,93 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: LoginModule.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using System;
using System.Dynamic;
using Nancy;
using Nancy.Authentication.Forms;
using Nancy.Extensions;
using PlexRequests.Core;
namespace PlexRequests.UI.Modules
{
public class LoginModule : NancyModule
{
public LoginModule()
{
Get["/login"] = _ =>
{
{
dynamic model = new ExpandoObject();
model.Errored = Request.Query.error.HasValue;
return View["Login/Index", model];
}
};
Get["/logout"] = x => this.LogoutAndRedirect("~/");
Post["/login"] = x =>
{
var userId = UserMapper.ValidateUser((string)Request.Form.Username, (string)Request.Form.Password);
if (userId == null)
{
return Context.GetRedirect("~/login?error=true&username=" + (string)Request.Form.Username);
}
DateTime? expiry = null;
if (Request.Form.RememberMe.HasValue)
{
expiry = DateTime.Now.AddDays(7);
}
return this.LoginAndRedirect(userId.Value, expiry);
};
Get["/register"] = x => {
{
dynamic model = new ExpandoObject();
model.Errored = Request.Query.error.HasValue;
return View["Login/Register", model];
}
};
Post["/register"] = x =>
{
var exists = UserMapper.DoUsersExist();
if (exists)
{
return Context.GetRedirect("~/register?error=true&username=" + (string)Request.Form.Username);
}
var userId = UserMapper.CreateUser(Request.Form.Username, Request.Form.Password);
return this.LoginAndRedirect((Guid)userId);
};
}
}
}

View file

@ -0,0 +1,115 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: RequestsModule.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using System.Linq;
using Humanizer;
using Nancy;
using Nancy.Responses.Negotiation;
using PlexRequests.Store;
using PlexRequests.UI.Models;
namespace PlexRequests.UI.Modules
{
public class RequestsModule : NancyModule
{
private IRepository<RequestedModel> Service { get; set; }
public RequestsModule(IRepository<RequestedModel> service) : base("requests")
{
Service = service;
Get["/"] = _ => LoadRequests();
Get["/movies"] = _ => GetMovies();
Get["/tvshows"] = _ => GetTvShows();
Post["/delete"] = _ =>
{
var convertedType = (string)Request.Form.type == "movie" ? RequestType.Movie : RequestType.TvShow;
return DeleteRequest((int)Request.Form.id, convertedType);
};
}
private Negotiator LoadRequests()
{
return View["Requests/Index"];
}
private Response GetMovies()
{
var dbMovies = Service.GetAll().Where(x => x.Type == RequestType.Movie);
var viewModel = dbMovies.Select(tv => new RequestViewModel
{
Tmdbid = tv.ProviderId,
Type = tv.Type,
Status = tv.Status,
ImdbId = tv.ImdbId,
Id = tv.Id,
PosterPath = tv.PosterPath,
ReleaseDate = tv.ReleaseDate.Humanize(),
RequestedDate = tv.RequestedDate.Humanize(),
Approved = tv.Approved,
Title = tv.Title,
Overview = tv.Overview,
RequestedBy = tv.RequestedBy,
ReleaseYear = tv.ReleaseDate.Year.ToString()
}).ToList();
//TODO check if Available
return Response.AsJson(viewModel);
}
private Response GetTvShows()
{
var dbTv = Service.GetAll().Where(x => x.Type == RequestType.TvShow);
var viewModel = dbTv.Select(tv => new RequestViewModel
{
Tmdbid = tv.ProviderId,
Type = tv.Type,
Status = tv.Status,
ImdbId = tv.ImdbId,
Id = tv.Id,
PosterPath = tv.PosterPath,
ReleaseDate = tv.ReleaseDate.Humanize(),
RequestedDate = tv.RequestedDate.Humanize(),
Approved = tv.Approved,
Title = tv.Title,
Overview = tv.Overview,
RequestedBy = tv.RequestedBy,
ReleaseYear = tv.ReleaseDate.Year.ToString()
}).ToList();
//TODO check if Available
return Response.AsJson(viewModel);
}
private Response DeleteRequest(int providerId, RequestType type)
{
var currentEntity = Service.GetAll().FirstOrDefault(x => x.ProviderId == providerId && x.Type == type);
Service.Delete(currentEntity);
return Response.AsJson(new { Result = true });
}
}
}

View file

@ -0,0 +1,123 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: SearchModule.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using Nancy;
using Nancy.Responses.Negotiation;
using PlexRequests.Api;
using PlexRequests.Core;
using PlexRequests.Helpers;
using PlexRequests.Store;
namespace PlexRequests.UI.Modules
{
public class SearchModule : NancyModule
{
public SearchModule(ICacheProvider cache) : base("search")
{
MovieApi = new TheMovieDbApi();
TvApi = new TheTvDbApi();
Cache = cache;
Get["/"] = parameters => RequestLoad();
Get["movie/{searchTerm}"] = parameters => SearchMovie((string)parameters.searchTerm);
Get["tv/{searchTerm}"] = parameters => SearchTvShow((string)parameters.searchTerm);
Get["movie/upcoming"] = parameters => UpcomingMovies();
Get["movie/playing"] = parameters => CurrentlyPlayingMovies();
Post["request/movie"] = parameters => RequestMovie((int)Request.Form.movieId);
Post["request/tv"] = parameters => RequestTvShow((int)Request.Form.tvId, (bool)Request.Form.latest);
}
private TheMovieDbApi MovieApi { get; }
private TheTvDbApi TvApi { get; }
private ICacheProvider Cache { get; }
private string AuthToken => Cache.GetOrSet(CacheKeys.TvDbToken, TvApi.Authenticate, 50);
private Negotiator RequestLoad()
{
return View["Search/Index"];
}
private Response SearchMovie(string searchTerm)
{
var movies = MovieApi.SearchMovie(searchTerm);
var result = movies.Result;
return Response.AsJson(result);
}
private Response SearchTvShow(string searchTerm)
{
var tvShow = TvApi.SearchTv(searchTerm, AuthToken);
return Response.AsJson(tvShow);
}
private Response UpcomingMovies()
{
var movies = MovieApi.GetUpcomingMovies();
var result = movies.Result;
return Response.AsJson(result);
}
private Response CurrentlyPlayingMovies()
{
var movies = MovieApi.GetCurrentPlayingMovies();
var result = movies.Result;
return Response.AsJson(result);
}
private Response RequestMovie(int movieId)
{
var s = new SettingsService();
if (s.CheckRequest(movieId))
{
return Response.AsJson(new { Result = false, Message = "Movie has already been requested!" });
}
s.AddRequest(movieId, RequestType.Movie);
return Response.AsJson(new { Result = true });
}
/// <summary>
/// Requests the tv show.
/// </summary>
/// <param name="showId">The show identifier.</param>
/// <param name="latest">if set to <c>true</c> [latest].</param>
/// <returns></returns>
private Response RequestTvShow(int showId, bool latest)
{
// Latest send to Sonarr and no need to store in DB
var s = new SettingsService();
if (s.CheckRequest(showId))
{
return Response.AsJson(new { Result = false, Message = "TV Show has already been requested!" });
}
s.AddRequest(showId, RequestType.TvShow);
return Response.AsJson(new {Result = true });
}
}
}

View file

@ -0,0 +1,56 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: UserLoginModule.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using Nancy;
using PlexRequests.UI.Models;
namespace PlexRequests.UI.Modules
{
// TODO: Check the settings to see if we need to authenticate
// TODO: Add ability to logout
// TODO: Create UserLogin page
// TODO: If we need to authenticate we need to check if they are in Plex
// TODO: Allow the user of a username only or a Username and password
public class UserLoginModule : NancyModule
{
public UserLoginModule() : base("userlogin")
{
Get["/"] = _ => View["Index"];
Post["/"] = x => LoginUser();
}
private Response LoginUser()
{
var username = Request.Form.username;
// Add to the session
Request.Session[SessionKeys.UsernameKey] = username;
return Response.AsJson("");
}
}
}