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 } }