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

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace Sevomin.Models.Repositories
  5. {
  6. public class AdRepository : IAdRepository
  7. {
  8. #region Singleton
  9. private static AdRepository member;
  10. private static object locker = new object();
  11. private AdRepository()
  12. {
  13. }
  14. static AdRepository()
  15. {
  16. lock (locker)
  17. {
  18. member = new AdRepository();
  19. }
  20. }
  21. public static AdRepository Current
  22. {
  23. get
  24. {
  25. return member;
  26. }
  27. }
  28. #endregion
  29. #region AdRepository
  30. public IQueryable<Ad> ListAll()
  31. {
  32. return SevominDbContext.Current.Ads;
  33. }
  34. public void Add(Ad entity)
  35. {
  36. SevominDbContext.Current.Ads.Add(entity);
  37. Save();
  38. }
  39. public void Delete(Ad entity)
  40. {
  41. SevominDbContext.Current.Ads.Remove(entity);
  42. Save();
  43. }
  44. public Ad Find(long key)
  45. {
  46. return ListAll().SingleOrDefault(m => m.Id == key);
  47. }
  48. public void Save()
  49. {
  50. SevominDbContext.Current.SaveChanges();
  51. }
  52. public void ChangeDays(long id, int duration)
  53. {
  54. Find(id).Duration = duration;
  55. Save();
  56. }
  57. public void ToggleActive(long id)
  58. {
  59. Find(id).Active = !Find(id).Active;
  60. Save();
  61. }
  62. public IEnumerable<Ad> GetAds()
  63. {
  64. return ListAll().AsEnumerable().Where(m => m.Active && m.Expired == false);
  65. }
  66. #endregion
  67. }
  68. }