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 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using UnivateProperties_API.Containers.Misc;
  7. using UnivateProperties_API.Containers.Property;
  8. using UnivateProperties_API.Context;
  9. using UnivateProperties_API.Model.Misc;
  10. namespace UnivateProperties_API.Repository.Misc
  11. {
  12. public class CarouselRepository : ICarouselRepository
  13. {
  14. private readonly DataContext dBContext;
  15. public CarouselRepository(DataContext _dBContext)
  16. {
  17. dBContext = _dBContext;
  18. }
  19. public List<Carousel> Get(Func<Carousel, bool> where)
  20. {
  21. var CarouselList = dBContext.Carousel.Where(where).ToList();
  22. foreach (var item in CarouselList)
  23. {
  24. if (!string.IsNullOrEmpty(item.Image) && !item.Image.StartsWith("data:image"))
  25. item.Image = ImageFormatter.ImageToBase64(item.Image);
  26. }
  27. return CarouselList;
  28. }
  29. public List<Carousel> GetAll()
  30. {
  31. var CarouselList = dBContext.Carousel.ToList();
  32. foreach (var item in CarouselList)
  33. {
  34. if (!string.IsNullOrEmpty(item.Image) && !item.Image.StartsWith("data:image"))
  35. item.Image = ImageFormatter.ImageToBase64(item.Image);
  36. }
  37. return CarouselList;
  38. }
  39. public Carousel GetDetailed(Func<Carousel, bool> first)
  40. {
  41. return dBContext.Carousel.FirstOrDefault(first);
  42. }
  43. public List<Carousel> GetDetailedAll()
  44. {
  45. return dBContext.Carousel.ToList();
  46. }
  47. public void Insert(Carousel item)
  48. {
  49. string image = item.Image;
  50. item.Image = "";
  51. item.Id = dBContext.GetMaxID("Carousel") + 1;
  52. dBContext.Add(item);
  53. Save();
  54. bool saveFiles = false;
  55. var loc = dBContext.Location.FirstOrDefault()?.PropertyImageLocation;
  56. var lastID = item.Id;
  57. item.Id = lastID;
  58. if (!string.IsNullOrEmpty(loc))
  59. {
  60. saveFiles = true;
  61. loc = loc.Replace("Properties", "Carousel");
  62. if (Directory.Exists(loc))
  63. {
  64. Directory.CreateDirectory(loc);
  65. }
  66. }
  67. if (saveFiles)
  68. {
  69. string path = ImageFormatter.Base64ToImage(image, loc, lastID.ToString());
  70. item.Image = path;
  71. }
  72. else
  73. {
  74. item.Image = image;
  75. }
  76. Update(item);
  77. }
  78. public void Insert(IEnumerable<Carousel> items)
  79. {
  80. foreach (var item in items)
  81. {
  82. dBContext.Carousel.Add(item);
  83. Save();
  84. }
  85. }
  86. public void Remove(Carousel item)
  87. {
  88. dBContext.Carousel.Remove(item);
  89. Save();
  90. }
  91. public void Remove(IEnumerable<Carousel> items)
  92. {
  93. foreach (var item in items)
  94. {
  95. dBContext.Carousel.Remove(item);
  96. Save();
  97. }
  98. }
  99. public void RemoveAtId(int item)
  100. {
  101. var Carousel = Get(x => x.Id == item).FirstOrDefault();
  102. if (Carousel != null)
  103. {
  104. dBContext.Carousel.Remove(Carousel);
  105. Save();
  106. }
  107. }
  108. public void Save()
  109. {
  110. dBContext.SaveChanges();
  111. }
  112. public void Update(Carousel item)
  113. {
  114. dBContext.Entry(item).State = EntityState.Modified;
  115. Save();
  116. }
  117. public int NewId()
  118. {
  119. // Not sure if properties need it
  120. return 0;
  121. }
  122. public List<CarouselList> GetCarouselItems()
  123. {
  124. List<CarouselList> list = new List<CarouselList>();
  125. var CarouselList = dBContext.Carousel.ToList();
  126. foreach (var item in CarouselList)
  127. {
  128. if (!string.IsNullOrEmpty(item.Image) && !item.Image.StartsWith("data:image"))
  129. item.Image = ImageFormatter.ImageToBase64(item.Image);
  130. var carItem = new CarouselList()
  131. {
  132. Id = item.Id,
  133. Image = item.Image,
  134. Header = item.Header
  135. };
  136. if (item.PropertyId > 0)
  137. {
  138. var property = dBContext.Properties.Where(p => p.Id == item.PropertyId).FirstOrDefault();
  139. if (property != null)
  140. {
  141. property.Province = dBContext.Provinces.Where(p => p.Id == property.ProvinceId).FirstOrDefault();
  142. property.City = dBContext.Cities.Where(c => c.Id == property.CityId).FirstOrDefault();
  143. property.Suburb = dBContext.Suburbs.Where(s => s.Id == property.SuburbId).FirstOrDefault();
  144. }
  145. carItem.Address = string.Format("{0}, {1} <br/>{2}", property.Suburb.Description, property.City.Description, property.AddressLine3);
  146. carItem.IsProperty = true;
  147. }
  148. if (item.TimeshareId > 0)
  149. {
  150. var timeshare = dBContext.Weeks.Where(t => t.Id == item.TimeshareId).FirstOrDefault();
  151. carItem.Bedrooms = timeshare.Bedrooms;
  152. carItem.Sleeps = timeshare.MaxSleep;
  153. carItem.Arrival = timeshare.ArrivalDate;
  154. carItem.Departure = timeshare.DepartureDate;
  155. carItem.IsProperty = false;
  156. }
  157. list.Add(carItem);
  158. }
  159. return list;
  160. }
  161. public CarouselList GetCarousel(int id)
  162. {
  163. var carousel = dBContext.Carousel.Where(c => c.Id == id).FirstOrDefault();
  164. CarouselList item = new CarouselList();
  165. if (carousel != null)
  166. {
  167. foreach (string prop in carousel.GetAllProperties())
  168. {
  169. if (prop != "Item" && prop != "Display")
  170. item[prop] = carousel[prop];
  171. }
  172. if (item.PropertyId > 0)
  173. {
  174. var property = dBContext.Properties.Where(p => p.Id == item.PropertyId).FirstOrDefault();
  175. if (property != null)
  176. {
  177. property.Province = dBContext.Provinces.Where(p => p.Id == property.ProvinceId).FirstOrDefault();
  178. property.City = dBContext.Cities.Where(c => c.Id == property.CityId).FirstOrDefault();
  179. property.Suburb = dBContext.Suburbs.Where(s => s.Id == property.SuburbId).FirstOrDefault();
  180. }
  181. item.Address = string.Format("{0}, {1} <br/>{2}", property.Suburb.Description, property.City.Description, property.AddressLine3);
  182. item.IsProperty = true;
  183. }
  184. if (item.TimeshareId > 0)
  185. {
  186. var timeshare = dBContext.Weeks.Where(t => t.Id == item.TimeshareId).FirstOrDefault();
  187. item.Bedrooms = timeshare.Bedrooms;
  188. item.Sleeps = timeshare.MaxSleep;
  189. item.Arrival = timeshare.ArrivalDate;
  190. item.Departure = timeshare.DepartureDate;
  191. item.IsProperty = false;
  192. }
  193. }
  194. return item;
  195. }
  196. }
  197. }