| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 | using Microsoft.EntityFrameworkCore;
using Npgsql;
using System.Linq;
using UnivateProperties_API.Model;
using UnivateProperties_API.Model.Banks;
using UnivateProperties_API.Model.Campaigns;
using UnivateProperties_API.Model.Communication;
using UnivateProperties_API.Model.Financial;
using UnivateProperties_API.Model.Logging;
using UnivateProperties_API.Model.Misc;
using UnivateProperties_API.Model.ProcessFlow;
using UnivateProperties_API.Model.Properties;
using UnivateProperties_API.Model.Region;
using UnivateProperties_API.Model.Timeshare;
using UnivateProperties_API.Model.Users;
namespace UnivateProperties_API.Context
{
    public class DataContext : DbContext
    {
        private string connectionString = "";
        public DataContext(DbContextOptions<DataContext> 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;
                }
            }
        }
        #region User
        public virtual DbSet<Agency> Agencies { get; set; }
        public virtual DbSet<Agent> Agents { get; set; }
        public virtual DbSet<User> Users { get; set; }
        public virtual DbSet<Individual> Individuals { get; set; }
        public virtual DbSet<Address> Addresses { get; set; }
        public virtual DbSet<UserRole> Roles { get; set; }
        #endregion User
        #region Communication
        public virtual DbSet<Email> Emails { get; set; }
        public virtual DbSet<SMTPAccount> Accounts { get; set; }
        public virtual DbSet<SMTPHost> Hosts { get; set; }
        public virtual DbSet<Template> Templates { get; set; }
        public virtual DbSet<PlaceHolder> PlaceHolders { get; set; }
        #endregion Communication
        #region Property
        public DbSet<Property> Properties { get; set; }
        public DbSet<PropertyImage> PropertyImages { get; set; }
        public DbSet<PropertyType> PropertyTypes { get; set; }
        public DbSet<PropertyUserField> PropertyUserFields { get; set; }
        public DbSet<UserDefinedField> UserDefinedFields { get; set; }
        public DbSet<UserDefinedGroup> UserDefinedGroups { get; set; }
        #endregion
        #region Region 
        public DbSet<Province> Provinces { get; set; }
        public DbSet<City> Cities { get; set; }
        public DbSet<Suburb> Suburbs { get; set; }
        #endregion
        #region Timeshare
        public DbSet<TimeshareWeek> Weeks { get; set; }
        public DbSet<Status> Status { get; set; }
        public DbSet<UnitConfiguration> UnitConfigurations { get; set; }
        public DbSet<UnitConfigurationType> UnitConfigurationTypes { get; set; }
        public DbSet<Season> Seasons { get; set; }
        public DbSet<Bank> Banks { get; set; }
        public DbSet<BankAccount> BankAccounts { get; set; }
        #endregion Timeshare
        #region ProcessFlow
        public DbSet<ProcessFlow> ProcessFlows { get; set; }
        public DbSet<BidItem> BidItems { get; set; }
        #endregion
        #region Logs
        public DbSet<SearchLog> SearchLogs { get; set; }
        #endregion
        #region Misc
        public DbSet<Location> Location { get; set; }
        public DbSet<Carousel> Carousel { get; set; }
        public DbSet<PlaceHolderFormat> PlaceHolderFormats { get; set; }
        public DbSet<Default> Defaults { get; set; }
        public DbSet<TC> TermsConditions { get; set; }
        #endregion
        #region Payments
        public DbSet<Payment> Payments { get; set; }
        public DbSet<ListingFee> ListingFees { get; set; }
        #endregion
        #region Campaign
        public DbSet<Campaign> Campaigns { get; set; }
        public DbSet<CampaignItem> CampaignItems { get; set; }  
        public DbSet<CampaignPlaceHolder> CampaignPlaceHolders { get; set; }
        public DbSet<CampaignItemPlaceHolder> CampaignItemPlaceHolders { 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 BaseEntity)
                {
                    (item as BaseEntity).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<Individual>()
                .Ignore(b => b.FullName);
            modelBuilder.Entity<SMTPHost>().ToTable("Hosts");
            modelBuilder.Entity<UnitConfiguration>()
                .HasIndex(u => u.Code)
                .IsUnique();
            modelBuilder.Entity<Individual>(b =>
            {
                b.HasKey(e => e.Id);
                b.Property(e => e.Id).ValueGeneratedOnAdd();
            });
            modelBuilder.Entity<Individual>()
            .HasIndex(i => new { i.Telephone, i.CellNumber, i.Email })
            .IsUnique(true);
            modelBuilder.Entity<Agent>(b =>
            {
                b.HasKey(e => e.Id);
                b.Property(e => e.Id).ValueGeneratedOnAdd();
            });
            modelBuilder.Entity<Agency>(b =>
            {
                b.HasKey(e => e.Id);
                b.Property(e => e.Id).ValueGeneratedOnAdd();
            });
            modelBuilder.Entity<Person>(b =>
            {
                b.HasKey(e => e.Id);
                b.Property(e => e.Id).ValueGeneratedOnAdd();
            });
            modelBuilder.Entity<User>()
            .HasIndex(u => u.Username)
            .IsUnique(true);
            modelBuilder.Entity<Email>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<SMTPAccount>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<SMTPHost>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<Property>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<PropertyImage>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<PropertyType>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<PropertyUserField>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<UserDefinedField>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<UserDefinedGroup>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<City>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<Province>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<Suburb>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<Season>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<Status>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<TimeshareWeek>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<UnitConfiguration>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<UnitConfigurationType>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<Agency>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<Person>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<User>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<Bank>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<BankAccount>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<SearchLog>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<Address>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<BidItem>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<ProcessFlow>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<Template>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<Carousel>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<PlaceHolder>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<Campaign>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<CampaignItem>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<CampaignItemPlaceHolder>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<CampaignPlaceHolder>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
            modelBuilder.Entity<PlaceHolderFormat>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
        }
    
        //public int GetMaxID(string tableName)
        //{
        //    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();
        //    int returnValue = 0; 
        //    while(reader.Read())
        //    {
        //        returnValue = int.Parse(reader[0] == null ? "0" : reader[0].ToString() == "" ? "0" : reader[0].ToString());
        //    }
        //    connection.Close();
        //    return returnValue;
        //}
    }
}
 |