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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. foreach (PropertyImage img in images)
  22. {
  23. if (!img.Image.StartsWith("data:image"))
  24. img.Image = ImageFormatter.ImageToBase64(img.Image);
  25. }
  26. return images;
  27. }
  28. public List<PropertyImage> GetAll()
  29. {
  30. return dBContext.PropertyImages.ToList();
  31. }
  32. public PropertyImage GetDetailed(Func<PropertyImage, bool> first)
  33. {
  34. return dBContext.PropertyImages.FirstOrDefault(first);
  35. }
  36. public List<PropertyImage> GetDetailedAll()
  37. {
  38. throw new NotImplementedException();
  39. }
  40. public List<string> GetImages(int PropertyId)
  41. {
  42. var images = (from p in dBContext.PropertyImages
  43. where p.PropertyId == PropertyId
  44. orderby p.IsDefault descending
  45. select p.Image).ToList();
  46. List<string> formated = new List<string>();
  47. foreach (string img in images)
  48. {
  49. if (!img.StartsWith("data:image"))
  50. formated.Add(ImageFormatter.ImageToBase64(img));
  51. else
  52. formated.Add(img);
  53. }
  54. return formated;
  55. }
  56. public void Insert(PropertyImage item)
  57. {
  58. dBContext.PropertyImages.Add(item);
  59. Save();
  60. }
  61. public void Insert(IEnumerable<PropertyImage> items)
  62. {
  63. foreach (var item in items)
  64. {
  65. dBContext.PropertyImages.Add(item);
  66. }
  67. Save();
  68. }
  69. public void Remove(PropertyImage item)
  70. {
  71. dBContext.PropertyImages.Remove(item);
  72. Save();
  73. }
  74. public void Remove(IEnumerable<PropertyImage> items)
  75. {
  76. foreach (var item in items)
  77. {
  78. dBContext.PropertyImages.Remove(item);
  79. }
  80. Save();
  81. }
  82. public void RemoveAtId(int item)
  83. {
  84. var image = Get(x => x.Id == item).FirstOrDefault();
  85. if (image != null)
  86. {
  87. dBContext.PropertyImages.Remove(image);
  88. Save();
  89. }
  90. }
  91. public void Save()
  92. {
  93. dBContext.SaveChanges();
  94. }
  95. public void Update(PropertyImage item)
  96. {
  97. dBContext.Entry(item).State = EntityState.Modified;
  98. Save();
  99. }
  100. public int NewId()
  101. {
  102. // Not sure if properties need it
  103. return 0;
  104. }
  105. public void Update(NewPropertyImages propertyImages)
  106. {
  107. if (propertyImages.Images != null)
  108. {
  109. bool saveFiles = false;
  110. var loc = dBContext.Location.FirstOrDefault()?.PropertyImageLocation;
  111. if (!string.IsNullOrEmpty(loc))
  112. {
  113. saveFiles = true;
  114. loc += string.Format("\\{0}", propertyImages.PropertyId);
  115. if (Directory.Exists(loc))
  116. {
  117. Directory.CreateDirectory(loc);
  118. }
  119. }
  120. var lastID = dBContext.PropertyImages.Max(i => i.Id) + 1;
  121. foreach (var image in propertyImages.Images)
  122. {
  123. var newImage = new PropertyImage
  124. {
  125. PropertyId = propertyImages.PropertyId
  126. };
  127. if (saveFiles)
  128. {
  129. string path = ImageFormatter.Base64ToImage(image.Image, loc, lastID.ToString());
  130. newImage.Image = path;
  131. lastID++;
  132. }
  133. else
  134. {
  135. newImage.Image = image.Image;
  136. }
  137. dBContext.PropertyImages.Add(newImage);
  138. }
  139. Save();
  140. }
  141. }
  142. }
  143. }