using Microsoft.EntityFrameworkCore; using ProRestaurant.Classes; using ProRestaurant.Containers; using ProRestaurant.DBContexts; using ProRestaurant.Models.Restaurants; using System; using System.Collections.Generic; using System.Linq; namespace ProRestaurant.Repository.Restaurants { public class RestaurantRepository : IRestaurantRepository { private readonly DBContext dBContext; public RestaurantRepository(DBContext db) { dBContext = db; } public Restaurant GetRestaurant(Func where) { return dBContext.Restaurants.Where(where).FirstOrDefault(); } public IEnumerable GetRestaurants() { return dBContext.Restaurants.ToList(); } public List GetSearch() { var restaurants = dBContext.Restaurants.ToList(); List cards = new List(); foreach (var rest in restaurants) { var card = new RestaurantCard { Id = rest.Id, Name = rest.Name, Suburb = rest.Suburb, SubText = rest.Categories, DeliveryFee = string.Format("{0:C}", rest.DeliveryFee), DeliveryTime = rest.DeliveryTime }; if (!rest.Logo.Contains("data:image")) card.Logo = ImageFormatter.ImageToBase64(rest.Logo); else card.Logo = rest.Logo; cards.Add(card); } return cards; } public void Insert(Restaurant restaurant) { dBContext.Add(restaurant); Save(); } public void Remove(Restaurant restaurant) { dBContext.Restaurants.Remove(restaurant); Save(); } public void Save() { dBContext.SaveChanges(); } public void Update(Restaurant restaurant) { dBContext.Entry(restaurant).State = EntityState.Modified; Save(); } } }