using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using UnivateProperties_API.Containers.Property; using UnivateProperties_API.Context; using UnivateProperties_API.Model.Properties; namespace UnivateProperties_API.Repository.Properties { public class PropertyImageRepository : IPropertyImageRepository { private readonly DataContext dBContext; public PropertyImageRepository(DataContext _dBContext) { dBContext = _dBContext; } public List Get(Func where) { return dBContext.PropertyImages.Where(where).ToList(); } public List GetAll() { return dBContext.PropertyImages.ToList(); } public PropertyImage GetDetailed(Func first) { return dBContext.PropertyImages.FirstOrDefault(first); } public List GetDetailedAll() { throw new NotImplementedException(); } public List GetImages(int PropertyId) { var images = (from p in dBContext.PropertyImages where p.PropertyId == PropertyId select p.Image).ToList(); List formated = new List(); foreach (string img in images) { if (!img.StartsWith("data:image")) formated.Add(ImageFormatter.ImageToBase64(img)); else formated.Add(img); } return formated; } public void Insert(PropertyImage item) { dBContext.PropertyImages.Add(item); Save(); } public void Insert(IEnumerable items) { foreach (var item in items) { dBContext.PropertyImages.Add(item); } Save(); } public void Remove(PropertyImage item) { dBContext.PropertyImages.Remove(item); Save(); } public void Remove(IEnumerable items) { foreach (var item in items) { dBContext.PropertyImages.Remove(item); } Save(); } public void RemoveAtId(int item) { var image = Get(x => x.Id == item).FirstOrDefault(); if (image != null) { dBContext.PropertyImages.Remove(image); Save(); } } public void Save() { dBContext.SaveChanges(); } public void Update(PropertyImage item) { dBContext.Entry(item).State = EntityState.Modified; Save(); } public int NewId() { // Not sure if properties need it return 0; } } }