You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

81 lines
1.7 KiB

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<Ad> 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<Ad> GetAds()
{
return ListAll().AsEnumerable().Where(m => m.Active && m.Expired == false);
}
#endregion
}
}