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

This commit is contained in:
Drewster727 2016-03-30 13:59:03 -05:00
commit 1ca3e0532b
2 changed files with 23 additions and 17 deletions

View file

@ -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; }
[JsonIgnore]
public List<string> AllUsers
{
get
{
var u = new List<string>();
if (!string.IsNullOrEmpty(RequestedBy))
{
u.Add(RequestedBy);
}
if (RequestedUsers.Any())
{
u.AddRange(RequestedUsers.Where(requestedUser => requestedUser != RequestedBy));
}
return u;
}
}
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;
return AllUsers.Any(x => x.Equals(username, StringComparison.OrdinalIgnoreCase));
}
}