diff --git a/OdeToFoodRider/OdeToFoodRider/Data/OdeToFoodDbContext.cs b/OdeToFoodRider/OdeToFoodRider/Data/OdeToFoodDbContext.cs new file mode 100644 index 0000000..d20581b --- /dev/null +++ b/OdeToFoodRider/OdeToFoodRider/Data/OdeToFoodDbContext.cs @@ -0,0 +1,12 @@ +using Microsoft.EntityFrameworkCore; +using OdeToFoodRider.Models; + +namespace OdeToFoodRider.Data +{ + public class OdeToFoodDbContext : DbContext + { + public OdeToFoodDbContext(DbContextOptions options) : base(options) { } + + public DbSet Restaurants { get; set; } + } +} \ No newline at end of file diff --git a/OdeToFoodRider/OdeToFoodRider/Services/SqlRestaurantData.cs b/OdeToFoodRider/OdeToFoodRider/Services/SqlRestaurantData.cs new file mode 100644 index 0000000..e3d2ac6 --- /dev/null +++ b/OdeToFoodRider/OdeToFoodRider/Services/SqlRestaurantData.cs @@ -0,0 +1,34 @@ +using System.Collections.Generic; +using System.Linq; +using OdeToFoodRider.Data; +using OdeToFoodRider.Models; + +namespace OdeToFoodRider.Services +{ + class SqlRestaurantData : IRestaurantData + { + private OdeToFoodDbContext _context; + + public SqlRestaurantData(OdeToFoodDbContext context) + { + _context = context; + } + + public Restaurant Add(Restaurant restaurant) + { + _context.Restaurants.Add(restaurant); + _context.SaveChanges(); + return restaurant; + } + + public Restaurant Get(int id) + { + return _context.Restaurants.FirstOrDefault(r => r.Id == id); + } + + public IEnumerable GetAll() + { + return _context.Restaurants.OrderBy(r => r.Name); + } + } +} \ No newline at end of file diff --git a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Data/OdeToFoodDbContext.cs b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Data/OdeToFoodDbContext.cs new file mode 100644 index 0000000..93b6cf2 --- /dev/null +++ b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Data/OdeToFoodDbContext.cs @@ -0,0 +1,16 @@ +using Microsoft.EntityFrameworkCore; +using OdeToFoodVisualStudio.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace OdeToFoodVisualStudio.Data +{ + public class OdeToFoodDbContext : DbContext + { + public OdeToFoodDbContext(DbContextOptions options) : base(options) {} + + public DbSet Restaurants { get; set; } + } +} diff --git a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Services/SqlRestaurantData.cs b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Services/SqlRestaurantData.cs new file mode 100644 index 0000000..e4d9fa8 --- /dev/null +++ b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Services/SqlRestaurantData.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using OdeToFoodVisualStudio.Data; +using OdeToFoodVisualStudio.Models; + +namespace OdeToFoodVisualStudio.Services +{ + public class SqlRestaurantData : IRestaurantData + { + private OdeToFoodDbContext _context; + + public SqlRestaurantData(OdeToFoodDbContext context) + { + _context = context; + } + + public Restaurant Add(Restaurant restaurant) + { + _context.Restaurants.Add(restaurant); + _context.SaveChanges(); + return restaurant; + } + + public Restaurant Get(int id) + { + return _context.Restaurants.FirstOrDefault(r => r.Id == id); + } + + // Possible performance issues if returning an IEnumerable with large data sets - IQueryable is performance-safer + public IEnumerable GetAll() + { + return _context.Restaurants.OrderBy(r => r.Name); + } + } +}