Exemple #1
0
        public async Task<ActionResult> UnsubscribeUser(string listId, string email)
        {
            try
            {
                IMailChimpManager manager = new MailChimpManager(APIKEY);

                using (var db = new BePreferencesEntities())
                {
                    BEMemberList member = db.BEMemberLists.FirstOrDefault(e => e.Email == email);
                    if (member != null)
                    {
                        await this.manager.Members.DeleteAsync(listId, member.Email);
                                                
                    }

                    RemoveMemberToDatabase(member, listId);
                    
                    return RedirectToAction("Index", new { email = "*****@*****.**" });
                }
                
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
Exemple #2
0
        private void AddMemberToDatabase(BEMemberList member, string newListId)
        {
            try
            {
                using(var db = new BePreferencesEntities())
                {
                    BEMemberList newMember = new BEMemberList
                    {
                        Email = member.Email,
                        FirstName = member.FirstName,
                        LastName = member.LastName,
                        AccessToken = member.AccessToken,
                        Created = DateTime.Now,
                        ListId = newListId
                    };

                    db.BEMemberLists.Add(newMember);
                    db.SaveChanges();
                    
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
Exemple #3
0
        private void ReInstateMemberToDatabase(BEMemberList member)
        {
            try
            {
                using (var db = new BePreferencesEntities())
                {
                    member.Archived = false;

                    db.Entry(member).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
Exemple #4
0
        private void RemoveMemberToDatabase(BEMemberList member, string newListId)
        {
            try
            {
                using (var db = new BePreferencesEntities())
                {
                    member.ListId = newListId;
                    member.Archived = true;

                    db.Entry(member).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();

                }
            }
            catch (Exception)
            {

                throw;
            }
        }
Exemple #5
0
        public async Task<ActionResult> GenerateMembers()
        {
            IMailChimpManager manager = new MailChimpManager(APIKEY);

            List<BEMemberList> newMemberList = new List<BEMemberList>();

            List<MailChimp.Net.Models.Member> allMembers = new List<MailChimp.Net.Models.Member>();

            var mailChimpListCollection = await manager.Lists.GetAllAsync().ConfigureAwait(false);

            foreach (MailChimp.Net.Models.List lst in mailChimpListCollection)
            {
                var lstMembers = await manager.Members.GetAllAsync(lst.Id).ConfigureAwait(false);

                allMembers.AddRange(lstMembers);
            }

            foreach (MailChimp.Net.Models.Member member in allMembers)
            {
                using (var db = new BePreferencesEntities())
                {
                    var newMember = new BEMemberList
                    {
                        FirstName = member.MergeFields.ContainsKey("FNAME") ? member.MergeFields["FNAME"].ToString() : "NA",
                        LastName = member.MergeFields.ContainsKey("LNAME") ? member.MergeFields["LNAME"].ToString() : "NA",
                        Email = member.EmailAddress,
                        AccessToken = Guid.NewGuid().ToString(),
                        Created = DateTime.Now,
                        ListId = member.ListId
                       

                    };

                    db.BEMemberLists.Add(newMember);
                    db.SaveChanges();
                }
            }

            return View(newMemberList);
        }
Exemple #6
0
        public async Task<ActionResult> SubscribeUser(string listId, string email)
        {
            try
            {
                 if (listId == "NA")
                        return Content("error");

                    using (var db = new BePreferencesEntities())
                    {
                        if (db.BEMemberLists.FirstOrDefault(m => m.Email == email && m.ListId == listId && m.Archived == true) != null)
                        {
                            BEMemberList member = db.BEMemberLists.FirstOrDefault(e => e.Email == email && e.ListId == listId);
                            if (member != null)
                            {
                                // MailChimp.Net.Models.Member memberToSubscribe = await GetMember(currentlistId, email);
                                var newMember = new MailChimp.Net.Models.Member { EmailAddress = email, StatusIfNew = MailChimp.Net.Models.Status.Subscribed };

                                newMember.MergeFields.Add("FNAME", member.FirstName);
                                newMember.MergeFields.Add("LNAME", member.LastName);
                                newMember.MergeFields.Add("FULLNAME", member.FirstName + " " + member.LastName);

                                await this.manager.Members.AddOrUpdateAsync(listId, newMember);
                                ReInstateMemberToDatabase(member);


                                return RedirectToAction("Index", new { email = member.Email });
                            }
                            else
                            {
                                return RedirectToAction("Index", new { email = email });
                            }
                        }
                        else
                        {


                            BEMemberList member = db.BEMemberLists.FirstOrDefault(e => e.Email == email);
                            if (member != null)
                            {
                                // MailChimp.Net.Models.Member memberToSubscribe = await GetMember(currentlistId, email);
                                var newMember = new MailChimp.Net.Models.Member { EmailAddress = email, StatusIfNew = MailChimp.Net.Models.Status.Subscribed };

                                newMember.MergeFields.Add("FNAME", member.FirstName);
                                newMember.MergeFields.Add("LNAME", member.LastName);
                                newMember.MergeFields.Add("FULLNAME", member.FirstName + " " + member.LastName);

                                await this.manager.Members.AddOrUpdateAsync(listId, newMember);
                                AddMemberToDatabase(member, listId);


                                return RedirectToAction("Index", new { email = member.Email });
                            }
                            else
                            {
                                return RedirectToAction("Index", new { email = email });
                            }
                        }
                       
                    }
                
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
        }