diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index aecdfacc6..7c8a82ef0 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -171,9 +171,11 @@ + + diff --git a/NzbDrone.Core/Providers/IQualityProvider.cs b/NzbDrone.Core/Providers/IQualityProvider.cs new file mode 100644 index 000000000..22ad05465 --- /dev/null +++ b/NzbDrone.Core/Providers/IQualityProvider.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NzbDrone.Core.Repository.Quality; + +namespace NzbDrone.Core.Providers +{ + public interface IQualityProvider + { + void AddProfile(QualityProfile profile, List allowedQualities); + void UpdateProfile(QualityProfile profile, List allowedQualities); + void RemoveProfile(int profileId); + List GetProfiles(); + } +} diff --git a/NzbDrone.Core/Providers/QualityProvider.cs b/NzbDrone.Core/Providers/QualityProvider.cs new file mode 100644 index 000000000..e21b7d50c --- /dev/null +++ b/NzbDrone.Core/Providers/QualityProvider.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NzbDrone.Core.Repository.Quality; +using SubSonic.Repository; + +namespace NzbDrone.Core.Providers +{ + public class QualityProvider : IQualityProvider + { + private IRepository _sonicRepo; + + public QualityProvider(IRepository sonicRepo) + { + _sonicRepo = sonicRepo; + } + + #region IQualityProvider Members + + public void AddProfile(QualityProfile profile, List allowedQualities) + { + var profileId = _sonicRepo.Add(profile); + + foreach (var allowed in allowedQualities) + { + allowed.ProfileId = (int)profileId; + _sonicRepo.Add(allowed); + } + } + + public void UpdateProfile(QualityProfile profile, List allowedQualities) + { + if (!_sonicRepo.Exists(q => q.ProfileId == profile.ProfileId)) + { + //Log Error + throw new NotImplementedException(); + } + + _sonicRepo.Update(profile); + + //Check to see if any items in the DB do not exist in this list + //Check to see if any of the allowedQualities already exist, if so update, else add + + foreach (var inDb in _sonicRepo.All().Where(q => q.ProfileId == profile.ProfileId)) + { + if (!allowedQualities.Exists(l => l.ProfileId == inDb.ProfileId && l.Quality == inDb.Quality)) + _sonicRepo.Delete(inDb.Id); + } + + foreach (var allowed in allowedQualities) + { + allowed.ProfileId = profile.ProfileId; + if (!_sonicRepo.Exists(q => q.ProfileId == profile.ProfileId && q.Quality == allowed.Quality)) + _sonicRepo.Add(allowed); + + else + _sonicRepo.Update(allowed); + } + } + + public void RemoveProfile(int profileId) + { + _sonicRepo.DeleteMany(q => q.ProfileId == profileId); + _sonicRepo.Delete(profileId); + } + + public List GetProfiles() + { + var profiles = _sonicRepo.All().ToList(); + + foreach (var profile in profiles) + { + profile.AllowedQualities = _sonicRepo.Find(q => q.ProfileId == profile.ProfileId).ToList(); + } + return profiles; + } + + #endregion + } +} diff --git a/NzbDrone.Core/Repository/Quality/AllowedQuality.cs b/NzbDrone.Core/Repository/Quality/AllowedQuality.cs index c6f51eb6f..87fcbf4a8 100644 --- a/NzbDrone.Core/Repository/Quality/AllowedQuality.cs +++ b/NzbDrone.Core/Repository/Quality/AllowedQuality.cs @@ -1,4 +1,6 @@ -namespace NzbDrone.Core.Repository.Quality +using SubSonic.SqlGeneration.Schema; + +namespace NzbDrone.Core.Repository.Quality { public class AllowedQuality { @@ -7,5 +9,8 @@ public int Order { get; set; } public bool MarkComplete { get; set; } public QualityTypes Quality { get; set; } + + [SubSonicToOneRelation(ThisClassContainsJoinKey = true)] + public virtual QualityProfile QualityProfile { get; private set; } } } diff --git a/NzbDrone.Core/Repository/Quality/QualityProfile.cs b/NzbDrone.Core/Repository/Quality/QualityProfile.cs index 20e666170..b68cb299d 100644 --- a/NzbDrone.Core/Repository/Quality/QualityProfile.cs +++ b/NzbDrone.Core/Repository/Quality/QualityProfile.cs @@ -7,33 +7,15 @@ namespace NzbDrone.Core.Repository.Quality { public class QualityProfile { - public int Id { get; set; } - public QualityTypes Cutoff { get; set; } + [SubSonicPrimaryKey(true)] + public int ProfileId { get; set; } + public string Name { get; set; } + public bool UserProfile { get; set; } //Allows us to tell the difference between default and user profiles - [EditorBrowsable(EditorBrowsableState.Never)] - public string SonicAllowed - { - get - { - string result = String.Empty; - foreach (var q in Allowed) - { - result += (int)q + "|"; - } - return result.Trim('|'); - } - private set - { - var qualities = value.Split('|'); - Allowed = new List(qualities.Length); - foreach (var quality in qualities) - { - Allowed.Add((QualityTypes)Convert.ToInt32(quality)); - } - } - } + [SubSonicToManyRelation] + public virtual List Allowed { get; private set; } [SubSonicIgnore] - public List Allowed { get; set; } + public List AllowedQualities { get; set; } } }