public void ConvertNullProperties()
        {
            var user = new User()
            {
                Identifier = Guid.NewGuid(),
            };
            var app = new Application()
            {
                Identifier = Guid.NewGuid(),
            };
            var up = new UserPreference()
            {
                Application = app,
                User        = user,
            };

            var converted = up.Convert();

            Assert.AreEqual <Guid>(up.Application.Identifier, converted.ApplicationIdentifier);
            Assert.AreEqual <Guid>(up.User.Identifier, converted.UserIdentifier);
            Assert.IsNull(converted.CurrentApplicationIdentifier);
            Assert.IsNull(converted.MaxiumAllowedApplications);
            Assert.IsNull(converted.TwitterHandle);
            Assert.IsNull(converted.GitHubHandle);
            Assert.IsNull(converted.AbcHandle);
            Assert.IsNull(converted.City);
            Assert.IsNull(converted.Country);
        }
        public void ConvertNullUser()
        {
            var up = new UserPreference()
            {
                Application = new Application(),
            };

            up.Convert();
        }
        public void Convert()
        {
            var random = new Random();
            var user   = new User()
            {
                Identifier = Guid.NewGuid(),
            };
            var cur = new Application()
            {
                Identifier = Guid.NewGuid(),
            };
            var app = new Application()
            {
                Identifier = Guid.NewGuid(),
            };
            var up = new UserPreference()
            {
                Application                = app,
                User                       = user,
                CurrentApplication         = cur,
                MaximumAllowedApplications = random.Next(),
                TimeZone                   = TimeZoneInfo.Utc,
                TwitterHandle              = StringHelper.ValidString(),
                AbcHandle                  = StringHelper.ValidString(),
                GitHubHandle               = StringHelper.ValidString(),
                City                       = StringHelper.ValidString(),
                Country                    = StringHelper.ValidString(),
            };

            var converted = up.Convert();

            Assert.AreEqual <Guid>(up.Application.Identifier, converted.ApplicationIdentifier);
            Assert.AreEqual <Guid>(up.CurrentApplication.Identifier, converted.CurrentApplicationIdentifier.Value);
            Assert.AreEqual <Guid>(up.User.Identifier, converted.UserIdentifier);
            Assert.AreEqual <string>(TimeZoneInfo.Utc.ToSerializedString(), converted.TimeZone);
            Assert.AreEqual <string>(up.TwitterHandle, converted.TwitterHandle);
            Assert.AreEqual <string>(up.AbcHandle, converted.AbcHandle);
            Assert.AreEqual <string>(up.GitHubHandle, converted.GitHubHandle);
            Assert.AreEqual <string>(up.City, converted.City);
            Assert.AreEqual <string>(up.Country, converted.Country);
            Assert.AreEqual <int?>(up.MaximumAllowedApplications, converted.MaxiumAllowedApplications);
        }
Exemplo n.º 4
0
        public UserPreference Save(UserPreference userPreference)
        {
            Contract.Requires <ArgumentNullException>(null != userPreference);
            Contract.Requires <ArgumentNullException>(null != userPreference.Application);
            Contract.Requires <ArgumentNullException>(null != userPreference.User);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != userPreference.Application.Identifier);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != userPreference.User.Identifier);

            using (new PerformanceMonitor())
            {
                var table  = new AzureTable <UserPreferenceRow>(ServerConfiguration.Default);
                var exists = table.QueryBy(userPreference.Application.Identifier.ToString(), userPreference.User.Identifier.ToString());

                if (null == exists)
                {
                    table.AddEntity(userPreference.Convert());
                }
                else
                {
                    if (null != userPreference.CurrentApplication && Guid.Empty != userPreference.CurrentApplication.Identifier)
                    {
                        exists.CurrentApplicationIdentifier = userPreference.CurrentApplication.Identifier;
                    }

                    if (null != userPreference.TimeZone)
                    {
                        exists.TimeZone = userPreference.TimeZone.ToSerializedString();
                    }

                    if (!string.IsNullOrWhiteSpace(userPreference.TwitterHandle))
                    {
                        exists.TwitterHandle = userPreference.TwitterHandle;
                    }

                    if (!string.IsNullOrWhiteSpace(userPreference.GitHubHandle))
                    {
                        exists.GitHubHandle = userPreference.GitHubHandle;
                    }

                    if (!string.IsNullOrWhiteSpace(userPreference.AbcHandle))
                    {
                        userPreference.AbcHandle = Regex.Replace(userPreference.AbcHandle, @"\s", string.Empty);
                        if (!string.IsNullOrWhiteSpace(userPreference.AbcHandle))
                        {
                            if (exists.AbcHandle != userPreference.AbcHandle)
                            {
                                var profile = new ProfilePage()
                                {
                                    ApplicationIdentifier = userPreference.Application.Identifier,
                                    OwnerIdentifier       = userPreference.User.Identifier,
                                    ExistingHandle        = exists.AbcHandle,
                                    Handle = userPreference.AbcHandle,
                                };

                                try
                                {
                                    profile = this.Save(profile);

                                    exists.AbcHandle = profile.Handle;
                                }
                                catch (Exception ex)
                                {
                                    Logging.Log(ex, EventTypes.Warning, (int)ServiceFault.CannotCreateProfile);
                                }
                            }
                        }
                    }

                    if (!string.IsNullOrWhiteSpace(userPreference.City))
                    {
                        exists.City = userPreference.City;
                    }

                    if (!string.IsNullOrWhiteSpace(userPreference.Country))
                    {
                        exists.Country = userPreference.Country;
                    }

                    exists.MaxiumAllowedApplications = userPreference.MaximumAllowedApplications ?? exists.MaxiumAllowedApplications ?? 1;

                    table.AddOrUpdateEntity(exists);
                }

                return(this.Get(userPreference));
            }
        }