123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- using Microsoft.EntityFrameworkCore;
- using Npgsql;
- using ProRestaurant.Models;
- using ProRestaurant.Models.Accounts;
- using ProRestaurant.Models.Restaurants;
- using System.Data.SqlClient;
- using System.Linq;
-
- namespace ProRestaurant.DBContexts
- {
- public class DBContext : DbContext
- {
- private string connectionString = "";
- private bool UseSQL = false;
- public DBContext(DbContextOptions<DBContext> options)
- : base(options)
- {
- foreach (var extention in options.Extensions)
- {
- if (extention.GetType().ToString() == "Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal.NpgsqlOptionsExtension")
- {
- connectionString = ((Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal.NpgsqlOptionsExtension)extention).ConnectionString;
- UseSQL = false;
- }
- if (extention.GetType().ToString() == "Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal.SqlServerOptionsExtension")
- {
- connectionString = ((Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal.SqlServerOptionsExtension)extention).ConnectionString;
- UseSQL = true;
- }
- }
- }
-
- #region Accounts
- public DbSet<User> Users { get; set; }
- public DbSet<DriverDetail> DriverDetails { get; set; }
- public DbSet<UserAddress> UserAddresses { get; set; }
- #endregion
-
- #region Restaurants
- public DbSet<Restaurant> Restaurants { get; set; }
- public DbSet<TradingHours> TradingHours { get; set; }
- public DbSet<MenuCategory> MenuCategories { get; set; }
- public DbSet<MenuItem> MenuItems { get; set; }
- #endregion
-
- public override int SaveChanges()
- {
- foreach (var item in ChangeTracker
- .Entries()
- .Where(x => x.State == EntityState.Modified || x.State == EntityState.Added)
- .Select(x => x.Entity)
- .ToList())
- {
- if (item is BaseObject)
- {
- (item as BaseObject).UpdateModified(string.Empty);
- }
- }
- UpdateSoftDeleteStatuses();
- return base.SaveChanges();
- }
-
- private void UpdateSoftDeleteStatuses()
- {
- foreach (var entry in ChangeTracker.Entries())
- {
- switch (entry.State)
- {
- case EntityState.Added:
- entry.CurrentValues["IsDeleted"] = false;
- break;
- case EntityState.Deleted:
- entry.State = EntityState.Modified;
- entry.CurrentValues["IsDeleted"] = true;
- break;
- }
- }
- }
-
- protected override void OnModelCreating(ModelBuilder modelBuilder)
- {
- modelBuilder.Entity<User>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
- modelBuilder.Entity<UserAddress>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
- modelBuilder.Entity<DriverDetail>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
- modelBuilder.Entity<Restaurant>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
- modelBuilder.Entity<TradingHours>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
- }
-
- public int GetNextID(string tableName)
- {
- int returnValue = 0;
- if (!UseSQL)
- {
- NpgsqlConnection connection = new NpgsqlConnection(connectionString);
- connection.Open();
-
- NpgsqlCommand cmd = connection.CreateCommand();
- cmd.CommandText = string.Format("select MAX(\"Id\") from \"{0}\"", tableName);
- NpgsqlDataReader reader = cmd.ExecuteReader();
-
- while (reader.Read())
- {
- returnValue = int.Parse(reader[0] == null ? "0" : reader[0].ToString() == "" ? "0" : reader[0].ToString());
- }
-
- connection.Close();
- }
- else
- {
- SqlConnection connection = new SqlConnection(connectionString);
- connection.Open();
-
- SqlCommand cmd = connection.CreateCommand();
- cmd.CommandText = string.Format("select MAX(\"Id\") from \"{0}\"", tableName);
- SqlDataReader reader = cmd.ExecuteReader();
-
- while (reader.Read())
- {
- returnValue = int.Parse(reader[0] == null ? "0" : reader[0].ToString() == "" ? "0" : reader[0].ToString());
- }
-
- connection.Close();
- }
- return returnValue + 1;
- }
- }
- }
|