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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnivateProperties_API.Containers.Property;
  6. using UnivateProperties_API.Context;
  7. using UnivateProperties_API.Model.Properties;
  8. namespace UnivateProperties_API.Repository.Properties
  9. {
  10. public class PropertyImageRepository : IPropertyImageRepository
  11. {
  12. private readonly DataContext dBContext;
  13. public PropertyImageRepository(DataContext _dBContext)
  14. {
  15. dBContext = _dBContext;
  16. }
  17. public List<PropertyImage> Get(Func<PropertyImage, bool> where)
  18. {
  19. return dBContext.PropertyImages.Where(where).ToList();
  20. }
  21. public List<PropertyImage> GetAll()
  22. {
  23. return dBContext.PropertyImages.ToList();
  24. }
  25. public PropertyImage GetDetailed(Func<PropertyImage, bool> first)
  26. {
  27. return dBContext.PropertyImages.FirstOrDefault(first);
  28. }
  29. public List<PropertyImage> GetDetailedAll()
  30. {
  31. throw new NotImplementedException();
  32. }
  33. public List<string> GetImages(int PropertyId)
  34. {
  35. var images = (from p in dBContext.PropertyImages
  36. where p.PropertyId == PropertyId
  37. select p.Image).ToList();
  38. List<string> formated = new List<string>();
  39. foreach (string img in images)
  40. {
  41. if (!img.StartsWith("data:image"))
  42. formated.Add(ImageFormatter.ImageToBase64(img));
  43. else
  44. formated.Add(img);
  45. }
  46. return formated;
  47. }
  48. public void Insert(PropertyImage item)
  49. {
  50. dBContext.PropertyImages.Add(item);
  51. Save();
  52. }
  53. public void Insert(IEnumerable<PropertyImage> items)
  54. {
  55. foreach (var item in items)
  56. {
  57. dBContext.PropertyImages.Add(item);
  58. }
  59. Save();
  60. }
  61. public void Remove(PropertyImage item)
  62. {
  63. dBContext.PropertyImages.Remove(item);
  64. Save();
  65. }
  66. public void Remove(IEnumerable<PropertyImage> items)
  67. {
  68. foreach (var item in items)
  69. {
  70. dBContext.PropertyImages.Remove(item);
  71. }
  72. Save();
  73. }
  74. public void RemoveAtId(int item)
  75. {
  76. var image = Get(x => x.Id == item).FirstOrDefault();
  77. if (image != null)
  78. {
  79. dBContext.PropertyImages.Remove(image);
  80. Save();
  81. }
  82. }
  83. public void Save()
  84. {
  85. dBContext.SaveChanges();
  86. }
  87. public void Update(PropertyImage item)
  88. {
  89. dBContext.Entry(item).State = EntityState.Modified;
  90. Save();
  91. }
  92. public int NewId()
  93. {
  94. // Not sure if properties need it
  95. return 0;
  96. }
  97. }
  98. }