public List <PartnerSdkModels.Customers.Customer> GetAll()
        {
            List <PartnerSdkModels.Customers.Customer> customers = new List <PartnerSdkModels.Customers.Customer>();

            using (var dbContext = new CSPDatabaseModelEntities())
            {
                using (var dataCampaignManager = new DataManagers.MarketingCampaignManager(dbContext))
                {
                    List <Customer> customersDb = dataCampaignManager.GetAll <Customer>().ToList();

                    customers = customersDb != null
                                   ? customersDb.Select(x => CustomerMapper.MapFromSource(x)).ToList()
                                   : null;
                }
            }

            return(customers);
        }
        public List <Campaign> GetAll()
        {
            List <Campaign> campaigns = new List <Campaign>();

            using (var dbContext = new CSPDatabaseModelEntities())
            {
                using (var dataCampaignManager = new DataManagers.MarketingCampaignManager(dbContext))
                {
                    List <MarketingCampaign> campaignsDb = dataCampaignManager.GetAll <MarketingCampaign>(null, c => c.MarketingCampaignsCustomers).ToList();

                    campaigns = campaignsDb != null
                                   ? campaignsDb.Select(x => MarketingCampaignMapper.MapFromSource(x)).ToList()
                                   : null;
                }
            }

            return(campaigns);
        }
        public void DeleteCampaign(string campaignId)
        {
            ExceptionDispatchInfo capturedException    = null;
            DbContextTransaction  dbContextTransaction = null;

            Guid id;
            MarketingCampaign marketingCampaign = new MarketingCampaign()
            {
                Id = !string.IsNullOrEmpty(campaignId) && Guid.TryParse(campaignId, out id) ? id : Guid.Empty
            };

            try
            {
                using (var dbContext = new CSPDatabaseModelEntities())
                {
                    // Begin transaction
                    using (dbContextTransaction = dbContext.Database.BeginTransaction())
                    {
                        using (var dataCampaignsCustomerManager = new DataManagers.MarketingCampaignsCustomer(dbContext))
                        {
                            dataCampaignsCustomerManager.DeleteWhere <MarketingCampaignsCustomer>(c => c.MarketingCampaignId.ToString() == campaignId);
                        }

                        using (var dataCampaignManager = new DataManagers.MarketingCampaignManager(dbContext))
                        {
                            dataCampaignManager.DeleteWhere <MarketingCampaign>(c => c.Id.ToString() == campaignId);
                        }

                        dbContextTransaction.Commit();
                    }
                }
            }
            catch (Exception ex)
            {
                capturedException = ExceptionDispatchInfo.Capture(ex);

                if (dbContextTransaction != null)
                {
                    // Rollback transaction
                    dbContextTransaction.Rollback();
                }
            }
        }
        public Campaign GetCampaign(string campaignId)
        {
            Campaign campaign = new Campaign();

            using (var dbContext = new CSPDatabaseModelEntities())
            {
                using (var dataCampaignManager = new DataManagers.MarketingCampaignManager(dbContext))
                {
                    MarketingCampaign campaignsDb = dataCampaignManager.FirstOrDefaultBy <MarketingCampaign>(c => c.Id != null &&
                                                                                                             c.Id.ToString() == campaignId, c => c.MarketingCampaignsCustomers);

                    campaign = campaignsDb != null
                                   ? MarketingCampaignMapper.MapFromSource(campaignsDb)
                                   : null;
                }
            }

            return(campaign);
        }
        public Campaign Create(Campaign campaign)
        {
            Campaign          campaignResult    = null;
            MarketingCampaign marketingCampaign = MarketingCampaignMapper.MapFromSource(campaign);

            using (var dbContext = new CSPDatabaseModelEntities())
            {
                using (var dataCampaignManager = new DataManagers.MarketingCampaignManager(dbContext))
                {
                    MarketingCampaign campaignDb = dataCampaignManager.AddOrUpdate <MarketingCampaign>(marketingCampaign, c => c.Id.ToString() == campaign.CampaignId);

                    MarketingCampaign campaignNew = dbContext.MarketingCampaigns.Include(c => c.MarketingCampaignsCustomers)
                                                    .Include(c => c.MarketingCampaignsCustomers.Select(x => x.Customer)).FirstOrDefault(c => c.Id == campaignDb.Id);

                    campaignResult = MarketingCampaignMapper.MapFromSource(campaignNew);
                }
            }

            return(campaignResult);
        }