add a better way to merge RequestedBy and RequestedUsers to avoid code duplication and simplify checks

pull/110/head
Drewster727 9 years ago
parent a668ea04ec
commit 1ca3e0532b

@ -4,6 +4,7 @@ using System.Security.Cryptography;
using Dapper.Contrib.Extensions;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
namespace PlexRequests.Store
{
@ -39,14 +40,28 @@ namespace PlexRequests.Store
public string SeasonsRequested { get; set; }
public List<string> RequestedUsers { get; set; }
public bool UserHasRequested(string username)
[JsonIgnore]
public List<string> AllUsers
{
bool alreadyRequested = !string.IsNullOrEmpty(RequestedBy) && RequestedBy.Equals(username, StringComparison.OrdinalIgnoreCase);
if (!alreadyRequested && RequestedUsers != null && RequestedUsers.Count > 0)
get
{
alreadyRequested = RequestedUsers.Any(x => x.Equals(username, StringComparison.OrdinalIgnoreCase));
var u = new List<string>();
if (!string.IsNullOrEmpty(RequestedBy))
{
u.Add(RequestedBy);
}
if (RequestedUsers.Any())
{
u.AddRange(RequestedUsers.Where(requestedUser => requestedUser != RequestedBy));
}
return u;
}
return alreadyRequested;
}
public bool UserHasRequested(string username)
{
return AllUsers.Any(x => x.Equals(username, StringComparison.OrdinalIgnoreCase));
}
}

@ -88,10 +88,6 @@ namespace PlexRequests.UI.Modules
}
var viewModel = dbMovies.Select(movie => {
if (!string.IsNullOrEmpty(movie.RequestedBy) && !movie.RequestedUsers.Any(x => x.Equals(movie.RequestedBy, StringComparison.OrdinalIgnoreCase)))
{
movie.RequestedUsers.Add(movie.RequestedBy);
}
return new RequestViewModel
{
ProviderId = movie.ProviderId,
@ -107,7 +103,7 @@ namespace PlexRequests.UI.Modules
Approved = movie.Available || movie.Approved,
Title = movie.Title,
Overview = movie.Overview,
RequestedUsers = isAdmin ? movie.RequestedUsers.ToArray() : new string[] { },
RequestedUsers = isAdmin ? movie.AllUsers.ToArray() : new string[] { },
ReleaseYear = movie.ReleaseDate.Year.ToString(),
Available = movie.Available,
Admin = isAdmin,
@ -131,11 +127,6 @@ namespace PlexRequests.UI.Modules
}
var viewModel = dbTv.Select(tv => {
if (!string.IsNullOrEmpty(tv.RequestedBy) &&
!tv.RequestedUsers.Any(x => x.Equals(tv.RequestedBy, StringComparison.OrdinalIgnoreCase)))
{
tv.RequestedUsers.Add(tv.RequestedBy);
}
return new RequestViewModel
{
ProviderId = tv.ProviderId,
@ -151,7 +142,7 @@ namespace PlexRequests.UI.Modules
Approved = tv.Available || tv.Approved,
Title = tv.Title,
Overview = tv.Overview,
RequestedUsers = isAdmin ? tv.RequestedUsers.ToArray() : new string[] { },
RequestedUsers = isAdmin ? tv.AllUsers.ToArray() : new string[] { },
ReleaseYear = tv.ReleaseDate.Year.ToString(),
Available = tv.Available,
Admin = isAdmin,

Loading…
Cancel
Save