using System.Collections; using System.Collections.Generic; using System.Linq; namespace Sevomin.Models.Repositories { public class AdRepository : IAdRepository { #region Singleton private static AdRepository member; private static object locker = new object(); private AdRepository() { } static AdRepository() { lock (locker) { member = new AdRepository(); } } public static AdRepository Current { get { return member; } } #endregion #region AdRepository public IQueryable ListAll() { return SevominDbContext.Current.Ads; } public void Add(Ad entity) { SevominDbContext.Current.Ads.Add(entity); Save(); } public void Delete(Ad entity) { SevominDbContext.Current.Ads.Remove(entity); Save(); } public Ad Find(long key) { return ListAll().SingleOrDefault(m => m.Id == key); } public void Save() { SevominDbContext.Current.SaveChanges(); } public void ChangeDays(long id, int duration) { Find(id).Duration = duration; Save(); } public void ToggleActive(long id) { Find(id).Active = !Find(id).Active; Save(); } public IEnumerable GetAds() { return ListAll().AsEnumerable().Where(m => m.Active && m.Expired == false); } #endregion } }