API
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.

CarouselRepository.cs 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnivateProperties_API.Containers.Misc;
  6. using UnivateProperties_API.Containers.Property;
  7. using UnivateProperties_API.Context;
  8. using UnivateProperties_API.Model.Misc;
  9. namespace UnivateProperties_API.Repository.Misc
  10. {
  11. public class CarouselRepository : ICarouselRepository
  12. {
  13. private readonly DataContext dBContext;
  14. public CarouselRepository(DataContext _dBContext)
  15. {
  16. dBContext = _dBContext;
  17. }
  18. public List<Carousel> Get(Func<Carousel, bool> where)
  19. {
  20. var CarouselList = dBContext.Carousel.Where(where).ToList();
  21. foreach (var item in CarouselList)
  22. {
  23. if (!string.IsNullOrEmpty(item.Image) && !item.Image.StartsWith("data:image"))
  24. item.Image = ImageFormatter.ImageToBase64(item.Image);
  25. }
  26. return CarouselList;
  27. }
  28. public List<Carousel> GetAll()
  29. {
  30. var CarouselList = dBContext.Carousel.ToList();
  31. foreach (var item in CarouselList)
  32. {
  33. if (!string.IsNullOrEmpty(item.Image) && !item.Image.StartsWith("data:image"))
  34. item.Image = ImageFormatter.ImageToBase64(item.Image);
  35. }
  36. return CarouselList;
  37. }
  38. public Carousel GetDetailed(Func<Carousel, bool> first)
  39. {
  40. return dBContext.Carousel.FirstOrDefault(first);
  41. }
  42. public List<Carousel> GetDetailedAll()
  43. {
  44. return dBContext.Carousel.ToList();
  45. }
  46. public void Insert(Carousel item)
  47. {
  48. dBContext.Carousel.Add(item);
  49. Save();
  50. }
  51. public void Insert(IEnumerable<Carousel> items)
  52. {
  53. foreach (var item in items)
  54. {
  55. dBContext.Carousel.Add(item);
  56. Save();
  57. }
  58. }
  59. public void Remove(Carousel item)
  60. {
  61. dBContext.Carousel.Remove(item);
  62. Save();
  63. }
  64. public void Remove(IEnumerable<Carousel> items)
  65. {
  66. foreach (var item in items)
  67. {
  68. dBContext.Carousel.Remove(item);
  69. Save();
  70. }
  71. }
  72. public void RemoveAtId(int item)
  73. {
  74. var Carousel = Get(x => x.Id == item).FirstOrDefault();
  75. if (Carousel != null)
  76. {
  77. dBContext.Carousel.Remove(Carousel);
  78. Save();
  79. }
  80. }
  81. public void Save()
  82. {
  83. dBContext.SaveChanges();
  84. }
  85. public void Update(Carousel item)
  86. {
  87. dBContext.Entry(item).State = EntityState.Modified;
  88. Save();
  89. }
  90. public int NewId()
  91. {
  92. // Not sure if properties need it
  93. return 0;
  94. }
  95. public List<CarouselList> GetCarouselItems()
  96. {
  97. List<CarouselList> list = new List<CarouselList>();
  98. var CarouselList = dBContext.Carousel.ToList();
  99. foreach (var item in CarouselList)
  100. {
  101. if (!string.IsNullOrEmpty(item.Image) && !item.Image.StartsWith("data:image"))
  102. item.Image = ImageFormatter.ImageToBase64(item.Image);
  103. var carItem = new CarouselList()
  104. {
  105. Image = item.Image,
  106. Header = item.Header
  107. };
  108. if (item.PropertyId > 0)
  109. {
  110. var property = dBContext.Properties.Include("Province").Include("City").Include("Suburb").Where(p => p.Id == item.PropertyId).FirstOrDefault();
  111. carItem.Address = string.Format("{0}, {1} <br/>{2}", property.Suburb.Description, property.City.Description, property.AddressLine3);
  112. carItem.IsProperty = true;
  113. }
  114. if (item.TimeshareId > 0)
  115. {
  116. var timeshare = dBContext.Weeks.Where(t => t.Id == item.TimeshareId).FirstOrDefault();
  117. carItem.Bedrooms = timeshare.Bedrooms;
  118. carItem.Sleeps = timeshare.MaxSleep;
  119. carItem.Arrival = timeshare.ArrivalDate;
  120. carItem.Departure = timeshare.DepartureDate;
  121. carItem.IsProperty = false;
  122. }
  123. list.Add(carItem);
  124. }
  125. return list;
  126. }
  127. }
  128. }