12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- 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<Restaurant, bool> where)
- {
- return dBContext.Restaurants.Where(where).FirstOrDefault();
- }
-
- public IEnumerable<Restaurant> GetRestaurants()
- {
- return dBContext.Restaurants.ToList();
- }
-
- public List<RestaurantCard> GetSearch()
- {
- var restaurants = dBContext.Restaurants.ToList();
-
- List<RestaurantCard> cards = new List<RestaurantCard>();
-
- 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();
- }
- }
- }
|