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.

RestaurantRepository.cs 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using Microsoft.EntityFrameworkCore;
  2. using ProRestaurant.Classes;
  3. using ProRestaurant.Containers;
  4. using ProRestaurant.DBContexts;
  5. using ProRestaurant.Models.Restaurants;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. namespace ProRestaurant.Repository.Restaurants
  10. {
  11. public class RestaurantRepository : IRestaurantRepository
  12. {
  13. private readonly DBContext dBContext;
  14. public RestaurantRepository(DBContext db)
  15. {
  16. dBContext = db;
  17. }
  18. public Restaurant GetRestaurant(Func<Restaurant, bool> where)
  19. {
  20. return dBContext.Restaurants.Where(where).FirstOrDefault();
  21. }
  22. public List<RestaurantCard> GetSearch()
  23. {
  24. var restaurants = dBContext.Restaurants.ToList();
  25. List<RestaurantCard> cards = new List<RestaurantCard>();
  26. foreach (var rest in restaurants)
  27. {
  28. var card = new RestaurantCard
  29. {
  30. Id = rest.Id,
  31. Name = rest.Name,
  32. Suburb = rest.Suburb,
  33. SubText = rest.Categories,
  34. DeliveryFee = string.Format("{0:C}", rest.DeliveryFee),
  35. DeliveryTime = rest.DeliveryTime
  36. };
  37. if (!rest.Logo.Contains("data:image"))
  38. card.Logo = ImageFormatter.ImageToBase64(rest.Logo);
  39. else
  40. card.Logo = rest.Logo;
  41. cards.Add(card);
  42. }
  43. return cards;
  44. }
  45. public void Insert(Restaurant restaurant)
  46. {
  47. dBContext.Add(restaurant);
  48. Save();
  49. }
  50. public void Remove(Restaurant restaurant)
  51. {
  52. dBContext.Restaurants.Remove(restaurant);
  53. Save();
  54. }
  55. public void Save()
  56. {
  57. dBContext.SaveChanges();
  58. }
  59. public void Update(Restaurant restaurant)
  60. {
  61. dBContext.Entry(restaurant).State = EntityState.Modified;
  62. Save();
  63. }
  64. List<Restaurant> IRestaurantRepository.GetRestaurants()
  65. {
  66. return dBContext.Restaurants.ToList();
  67. }
  68. }
  69. }