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

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).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. select p.Image).ToList();
  45. List<string> formated = new List<string>();
  46. foreach (string img in images)
  47. {
  48. if (!img.StartsWith("data:image"))
  49. formated.Add(ImageFormatter.ImageToBase64(img));
  50. else
  51. formated.Add(img);
  52. }
  53. return formated;
  54. }
  55. public void Insert(PropertyImage item)
  56. {
  57. dBContext.PropertyImages.Add(item);
  58. Save();
  59. }
  60. public void Insert(IEnumerable<PropertyImage> items)
  61. {
  62. foreach (var item in items)
  63. {
  64. dBContext.PropertyImages.Add(item);
  65. }
  66. Save();
  67. }
  68. public void Remove(PropertyImage item)
  69. {
  70. dBContext.PropertyImages.Remove(item);
  71. Save();
  72. }
  73. public void Remove(IEnumerable<PropertyImage> items)
  74. {
  75. foreach (var item in items)
  76. {
  77. dBContext.PropertyImages.Remove(item);
  78. }
  79. Save();
  80. }
  81. public void RemoveAtId(int item)
  82. {
  83. var image = Get(x => x.Id == item).FirstOrDefault();
  84. if (image != null)
  85. {
  86. dBContext.PropertyImages.Remove(image);
  87. Save();
  88. }
  89. }
  90. public void Save()
  91. {
  92. dBContext.SaveChanges();
  93. }
  94. public void Update(PropertyImage item)
  95. {
  96. dBContext.Entry(item).State = EntityState.Modified;
  97. Save();
  98. }
  99. public int NewId()
  100. {
  101. // Not sure if properties need it
  102. return 0;
  103. }
  104. public void Update(NewPropertyImages propertyImages)
  105. {
  106. if (propertyImages.Images != null)
  107. {
  108. var lastID = dBContext.GetMaxID("PropertyImages");
  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. foreach (var image in propertyImages.Images)
  121. {
  122. lastID++;
  123. var newImage = new PropertyImage
  124. {
  125. Id = lastID,
  126. PropertyId = propertyImages.PropertyId
  127. };
  128. if (saveFiles)
  129. {
  130. string path = ImageFormatter.Base64ToImage(image.Image, loc, lastID.ToString());
  131. newImage.Image = path;
  132. }
  133. else
  134. {
  135. newImage.Image = image.Image;
  136. }
  137. dBContext.PropertyImages.Add(newImage);
  138. }
  139. Save();
  140. }
  141. }
  142. }
  143. }