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.

PropertyImageRepository.cs 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.Property;
  7. using UnivateProperties_API.Context;
  8. using UnivateProperties_API.Model.Properties;
  9. namespace UnivateProperties_API.Repository.Properties
  10. {
  11. public class PropertyImageRepository : IPropertyImageRepository
  12. {
  13. private readonly DataContext dBContext;
  14. public PropertyImageRepository(DataContext _dBContext)
  15. {
  16. dBContext = _dBContext;
  17. }
  18. public List<PropertyImage> Get(Func<PropertyImage, bool> where)
  19. {
  20. var images = dBContext.PropertyImages.Where(where).OrderByDescending(i => i.IsDefault).ThenBy(i => i.Id).ToList();
  21. var url = dBContext.Location.FirstOrDefault()?.SiteURL;
  22. foreach (PropertyImage img in images)
  23. {
  24. if (!img.Image.StartsWith("data:image"))
  25. {
  26. if (!string.IsNullOrEmpty(url))
  27. img.Image = ImageFormatter.ImageToURL(img.Image, url);
  28. else
  29. img.Image = ImageFormatter.ImageToBase64(img.Image);
  30. }
  31. }
  32. return images;
  33. }
  34. public List<PropertyImage> GetAll()
  35. {
  36. return dBContext.PropertyImages.ToList();
  37. }
  38. public PropertyImage GetDetailed(Func<PropertyImage, bool> first)
  39. {
  40. return dBContext.PropertyImages.FirstOrDefault(first);
  41. }
  42. public List<PropertyImage> GetDetailedAll()
  43. {
  44. throw new NotImplementedException();
  45. }
  46. public List<string> GetImages(int PropertyId)
  47. {
  48. var images = (from p in dBContext.PropertyImages
  49. where p.PropertyId == PropertyId
  50. orderby p.IsDefault descending
  51. select p.Image).ToList();
  52. List<string> formated = new List<string>();
  53. var url = dBContext.Location.FirstOrDefault()?.SiteURL;
  54. foreach (string img in images)
  55. {
  56. if (!img.StartsWith("data:image"))
  57. {
  58. if (!string.IsNullOrEmpty(url))
  59. formated.Add(ImageFormatter.ImageToURL(img, url));
  60. else
  61. formated.Add(ImageFormatter.ImageToBase64(img));
  62. }
  63. else
  64. formated.Add(img);
  65. }
  66. return formated;
  67. }
  68. public void Insert(PropertyImage item)
  69. {
  70. dBContext.PropertyImages.Add(item);
  71. Save();
  72. }
  73. public void Insert(IEnumerable<PropertyImage> items)
  74. {
  75. foreach (var item in items)
  76. {
  77. dBContext.PropertyImages.Add(item);
  78. }
  79. Save();
  80. }
  81. public void Remove(PropertyImage item)
  82. {
  83. dBContext.PropertyImages.Remove(item);
  84. Save();
  85. }
  86. public void Remove(IEnumerable<PropertyImage> items)
  87. {
  88. foreach (var item in items)
  89. {
  90. dBContext.PropertyImages.Remove(item);
  91. }
  92. Save();
  93. }
  94. public void RemoveAtId(int item)
  95. {
  96. var image = Get(x => x.Id == item).FirstOrDefault();
  97. if (image != null)
  98. {
  99. dBContext.PropertyImages.Remove(image);
  100. Save();
  101. }
  102. }
  103. public void Save()
  104. {
  105. dBContext.SaveChanges();
  106. }
  107. public void Update(PropertyImage item)
  108. {
  109. dBContext.Entry(item).State = EntityState.Modified;
  110. Save();
  111. }
  112. public int NewId()
  113. {
  114. // Not sure if properties need it
  115. return 0;
  116. }
  117. public void Update(NewPropertyImages propertyImages)
  118. {
  119. if (propertyImages.Images != null)
  120. {
  121. bool saveFiles = false;
  122. var loc = dBContext.Location.FirstOrDefault()?.PropertyImageLocation;
  123. if (!string.IsNullOrEmpty(loc))
  124. {
  125. saveFiles = true;
  126. if (loc.EndsWith("\\"))
  127. {
  128. loc = loc.Substring(0, loc.Length - 1);
  129. }
  130. loc += string.Format("\\{0}", propertyImages.PropertyId);
  131. if (!Directory.Exists(loc))
  132. {
  133. Directory.CreateDirectory(loc);
  134. }
  135. }
  136. var lastID = dBContext.PropertyImages.Max(i => i.Id) + 1;
  137. foreach (var image in propertyImages.Images)
  138. {
  139. var newImage = new PropertyImage
  140. {
  141. PropertyId = propertyImages.PropertyId
  142. };
  143. if (saveFiles)
  144. {
  145. string path = ImageFormatter.Base64ToImage(image.Image, loc, lastID.ToString());
  146. newImage.Image = path;
  147. lastID++;
  148. }
  149. else
  150. {
  151. newImage.Image = image.Image;
  152. }
  153. dBContext.PropertyImages.Add(newImage);
  154. }
  155. Save();
  156. }
  157. }
  158. public void UpdateToPhysical()
  159. {
  160. bool saveFiles = false;
  161. var loc = dBContext.Location.FirstOrDefault()?.PropertyImageLocation;
  162. if (!string.IsNullOrEmpty(loc))
  163. {
  164. saveFiles = true;
  165. if (loc.EndsWith("\\"))
  166. {
  167. loc = loc.Substring(0, loc.Length - 1);
  168. }
  169. }
  170. if (saveFiles)
  171. {
  172. var images = dBContext.PropertyImages.ToList();
  173. foreach (var image in images)
  174. {
  175. string filePath = string.Format("{0}\\{1}", loc, image.PropertyId );
  176. if (!Directory.Exists(filePath))
  177. {
  178. Directory.CreateDirectory(filePath);
  179. }
  180. string path = ImageFormatter.Base64ToImage(image.Image, filePath, image.Id.ToString());
  181. image.Image = path;
  182. }
  183. Save();
  184. }
  185. }
  186. }
  187. }