예제 #1
0
        public void Should_Create_Update_Delete_AssociationUserAddress()
        {
            AssociationUserAddress address;

            //add
            using (var _db = new AssociationsContext())
            {
                address = new AssociationUserAddress();
                address.AssociationUserId = 21;
                address.Type = AddressType.Other;
                address.CountryId = 1;
                _db.AssociationUserAddresses.Add(address);
                _db.SaveChanges();
            }

            //retrieve and check
            using (var _db = new AssociationsContext())
            {
                var query = from a in _db.AssociationUserAddresses.ObjectSet.IncludeAll("Country", "State", "State.Country", "Suburb", "Suburb.Country", "Suburb.State")
                            where a.Id == address.Id
                            select a;

                address = query.SingleOrDefault();
                Assert.AreEqual(1, address.Country.Id);
            }

            //update
            using (var _db = new AssociationsContext())
            {
                address = new AssociationUserAddress() { Id = address.Id };
                address.AssociationUserId = 21;
                address.Type = AddressType.Other;
                address.SuburbId = 42;
                _db.AssociationUserAddresses.Attach(address);
                _db.ObjectStateManager.ChangeObjectState(address, EntityState.Modified);
                _db.SaveChanges();
            }

            //retrieve and check
            using (var _db = new AssociationsContext())
            {
                var query = from a in _db.AssociationUserAddresses.ObjectSet.IncludeAll("Country", "State", "State.Country", "Suburb", "Suburb.Country", "Suburb.State")
                            where a.Id == address.Id
                            select a;

                address = query.SingleOrDefault();
                Assert.AreEqual(42, address.Suburb.Id);
            }

            //delete
            using (var _db = new AssociationsContext())
            {
                address = new AssociationUserAddress() { Id = address.Id };
                _db.AssociationUserAddresses.Attach(address);
                _db.AssociationUserAddresses.Delete(address);
                _db.SaveChanges();
            }

            //retrieve and check
            using (var _db = new AssociationsContext())
            {
                var query = from a in _db.AssociationUserAddresses.ObjectSet.IncludeAll("Country", "State", "State.Country", "Suburb", "Suburb.Country", "Suburb.State")
                            where a.Id == address.Id
                            select a;

                address = query.SingleOrDefault();
                Assert.IsNull(address);
            }
        }
예제 #2
0
        public void AttachUserAddress(AssociationUserAddress address)
        {
            if (address.Suburb != null)
            {
                int? id = address.SuburbId;
                address.Suburb = null;
                address.SuburbId = id;
            }
            else if (address.State != null)
            {
                int? id = address.StateId;
                address.State = null;
                address.StateId = id;
            }
            else if (address.Country != null)
            {
                int? id = address.CountryId;
                address.Country = null;
                address.CountryId = id;
            }

            AssociationUserAddresses.Attach(address);

            if (address.Id == 0)
                ObjectStateManager.ChangeObjectState(address, EntityState.Added);
            else
                ObjectStateManager.ChangeObjectState(address, EntityState.Modified);
        }
예제 #3
0
        string Register(Association association)
        {
            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    AssociationsContext associationContext = new AssociationsContext();

                    var email = txtEmail.Text.ToLower();

                    //check id user with such email existed already
                    var existedUser = (from u in associationContext.AssociationUsers.ObjectSet
                                       where u.AssociationId == association.Id && u.Email == email
                                       select u).SingleOrDefault();

                    if (existedUser != null)
                        return "User with such email is already registered!";

                    //create user 
                    AssociationUser user = new AssociationUser();

                    user.AssociationId = association.Id;
                    user.Country = new Country() { Id = int.Parse(ddlCountry.SelectedValue) }; ;
                    user.Email = email;

                    //name
                    user.Prefix = NamePrefix.All.FirstOrDefault(p => p.Name == ddlPrefix.SelectedItem.Text);
                    user.FirstName = txtFirstName.Text.ToUpper();
                    user.LastName = txtLastName.Text.ToUpper();
                    //phone
                    user.OfficePhone = txtPhoneOffice.Text;
                    user.MobilePhone = txtPhoneMobile.Text;
                    user.HomePhone = txtPhoneHome.Text;
                    user.IsOfficePhoneDefault = rbDefaultPhoneOffice.Checked;
                    user.IsMobilePhoneDefault = rbDefaultPhoneMobile.Checked;
                    user.IsHomePhoneDefault = rbDefaultPhoneHome.Checked;

                    user.IsTravelAgency = checkTravelAgent.Checked;
                    user.PositionTitle = txtTitle.Text.ToUpper();
                    user.IataNumber = txtIataNumber.Text.ToUpper();

                    user.CreatedTime = user.LastUpdatedTime = DateTime.UtcNow;

                    associationContext.AttachUser(user);
                    associationContext.SaveChanges();

                    //home address
                    AssociationUserAddress homeAddress = new AssociationUserAddress();
                    homeAddress.Nickname = user.FullName + " HOME";
                    homeAddress.AssociationUserId = user.Id;
                    homeAddress.Type = AddressType.Home;
                    homeAddress.CountryId = user.CountryId;

                    //work address
                    AssociationUserAddress workAddress = new AssociationUserAddress();
                    workAddress.Nickname = user.FullName + " WORK";
                    workAddress.AssociationUserId = user.Id;
                    workAddress.CountryId = user.CountryId;
                    workAddress.Type = AddressType.Work;

                    workAddress.BusinessName = txtBusinessName.Text.ToUpper();
                    workAddress.BuildingName = txtBuilding.Text.ToUpper();
                    workAddress.Address1 = txtAddress1.Text.ToUpper();
                    workAddress.Address2 = txtAddress2.Text.ToUpper();
                    if (ddlSuburb.Visible && ddlSuburb.SelectedIndex > 0)
                    {
                        int suburbId = int.Parse(ddlSuburb.SelectedValue);
                        if (suburbId > 0)
                        {
                            workAddress.CountryId = null;
                            workAddress.SuburbId = suburbId;
                        }
                    }
                    else
                    {
                        if (rowState.Visible && ddlState.SelectedIndex > 0)
                        {
                            int stateId = int.Parse(ddlState.SelectedValue);
                            if (stateId > 0)
                            {
                                workAddress.CountryId = null;
                                workAddress.StateId = stateId;
                            }
                        }
                        workAddress.SuburbName = txtSuburbName.Text.ToUpper();
                        workAddress.SuburbCode = txtSuburbCode.Text.ToUpper();
                    }

                    associationContext.AttachUserAddress(homeAddress);
                    associationContext.AttachUserAddress(workAddress);
                    associationContext.SaveChanges();

                    AssociationUserActivation activation = new AssociationUserActivation();
                    activation.AssociationUserId = user.Id;
                    activation.Guid = Guid.NewGuid();
                    activation.ExpiryTime = DateTime.UtcNow.AddHours(2.0);  //expiry
                    associationContext.AssociationUserActivations.Add(activation);
                    associationContext.SaveChanges();

                    var emailProvider = associationContext.AssociationEmails.FirstOrDefault(e => e.AssociationId == association.Id);
                    if (emailProvider != null)
                    {
                        var uri = Request.Url;
                        string baseUrl = String.Format("{0}://{1}:{2}", uri.Scheme, uri.Host ?? "80", uri.Port);
                        string activtionLink = Path.Combine(baseUrl + Page.GetRouteUrl("activation", new { guid = activation.Guid.ToString("D") }));
                        string contactUsLink = Path.Combine(baseUrl + Page.GetRouteUrl("contactus", null));

                        var txtContent = MailTemplateHelper.GetRegistratioTxtContent(association.Name.ToUpper(), user.FullName.ToUpper(), activtionLink, contactUsLink);
                        var htmlContent = MailTemplateHelper.GetRegistratioHtmlContent(association.Name, user.FullName, activtionLink, contactUsLink);
                        var avBody = AlternateView.CreateAlternateViewFromString(htmlContent, null, MediaTypeNames.Text.Html);

                        EmailHelper.SendMail(emailProvider, user.Email, association.Name.ToUpper() + " New User Registration", txtContent, null, avBody, true);
                    }

                    scope.Complete();
                }

                return null;
            }
            catch (Exception ex)
            {
                StringBuilder builder = new StringBuilder();
                builder.AppendLine(ex.Message);
                if (ex.InnerException != null)
                {
                    builder.AppendLine(ex.InnerException.Message);
                    if (ex.InnerException.InnerException != null)
                        builder.AppendLine(ex.InnerException.InnerException.Message);
                }
                return builder.ToString();
            }
        }