public void DeleteByPageId_Success()
        {
            // Arrange
            var targetHelp = Models.CreateFirstTimeHelp();
            FirstTimeHelpRepository.Create(targetHelp);

            var noiseHelp = Models.CreateFirstTimeHelp();
            FirstTimeHelpRepository.Create(noiseHelp);

            var userName1 = "*****@*****.**";
            var userName2 = "*****@*****.**";

            var targetSettings1 = new UserPageSettings { PageId = targetHelp.Id, UserName = userName1 };
            UserPageSettingsRepository.Create(targetSettings1);
            var targetSettings2 = new UserPageSettings { PageId = targetHelp.Id, UserName = userName2 };
            UserPageSettingsRepository.Create(targetSettings2);
            var noiseSettings = new UserPageSettings { PageId = noiseHelp.Id, UserName = userName1 };
            UserPageSettingsRepository.Create(noiseSettings);

            // Act
            UserPageSettingsRepository.DeleteByPageId(targetHelp.Id);

            // Assert
            var deletedSettings1 = UserPageSettingsRepository.Read(userName1, targetHelp.Id);
            Assert.That(deletedSettings1, Is.Null, "First user's settings for target page should have been deleted.");
            var deletedSettings2 = UserPageSettingsRepository.Read(userName2, targetHelp.Id);
            Assert.That(deletedSettings2, Is.Null, "Second user's settings for target page should have been deleted.");
            var existingSettings = UserPageSettingsRepository.Read(userName1, noiseHelp.Id);
            Assert.That(existingSettings, Is.Not.Null, "Settings for other pages should remain.");
        }
        public void Process_NewPage()
        {
            // Arrange
            var pageId = 100;
            var userName = "******";

            var requestContract = new UserPageSettings
            {
                HidePage = true,
                PageId = pageId,
                UserName = "******",
            };
            var serializer = new JavaScriptSerializer();
            var requestData = serializer.Serialize(requestContract);

            var httpContext = Mocks.Create<HttpContextBase>();
            httpContext.Setup(c => c.User.Identity.Name).Returns(userName);

            var userPageSettingsRepository = Mocks.Create<IUserPageSettingsRepository>();
            userPageSettingsRepository.Setup(r => r.Read(userName, pageId)).Returns(null as UserPageSettings);
            userPageSettingsRepository.Setup(r => r.Create(It.Is<UserPageSettings>(
                s => s.HidePage == requestContract.HidePage
                    && s.PageId == pageId
                    && s.UserName == userName
            )));

            var processor = new SaveUserPageSettingsRequestProcessor(httpContext.Object, userPageSettingsRepository.Object);

            // Act
            var result = processor.Process(requestData);

            // Assert
            Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.OK), "The new settings record should be created successfully.");
            Mocks.VerifyAll();
        }
        public void Create(UserPageSettings userPageSettings)
        {
            using (var connection = SqlConnectionFactory.GetConnection())
            using (var command = connection.CreateCommand())
            {
                command.CommandType = CommandType.Text;
                command.CommandText = SqlScripts.UserPageSettings_Create;
                AddParameters(userPageSettings, command);

                connection.Open();
                userPageSettings.Id = (int)command.ExecuteScalar();
            }
        }
        public void Crud_Success()
        {
            var userName = "******";
            var help = Models.CreateFirstTimeHelp();
            FirstTimeHelpRepository.Create(help);

            var settings = new UserPageSettings(){
                UserName = userName,
                PageId = help.Id,
                HidePage = false,
            };
            UserPageSettingsRepository.Create(settings);

            settings.HidePage = true;
            UserPageSettingsRepository.Update(settings);

            var existingSettings = UserPageSettingsRepository.Read(settings.UserName, settings.PageId);

            Assert.That(existingSettings.HidePage, Is.True, "The value in the database should have been changed to true.");
        }
        public void DeleteExcept_Success()
        {
            // Arrange
            var keptHelp = Models.CreateFirstTimeHelp();
            FirstTimeHelpRepository.Create(keptHelp);

            var deletedHelp = Models.CreateFirstTimeHelp();
            FirstTimeHelpRepository.Create(deletedHelp);

            var anotherDeletedHelp = Models.CreateFirstTimeHelp();
            FirstTimeHelpRepository.Create(anotherDeletedHelp);

            var userName1 = "*****@*****.**";
            var userName2 = "*****@*****.**";

            var keptPageSetting = new UserPageSettings { PageId = keptHelp.Id, UserName = userName1 };
            UserPageSettingsRepository.Create(keptPageSetting);
            var deletedPageSetting1 = new UserPageSettings { PageId = deletedHelp.Id, UserName = userName1 };
            UserPageSettingsRepository.Create(deletedPageSetting1);
            var deletedPageSetting2 = new UserPageSettings { PageId = deletedHelp.Id, UserName = userName2 };
            UserPageSettingsRepository.Create(deletedPageSetting2);
            var anotherDeletedPageSetting = new UserPageSettings { PageId = anotherDeletedHelp.Id, UserName = userName1 };
            UserPageSettingsRepository.Create(anotherDeletedPageSetting);

            // Act
            UserPageSettingsRepository.DeleteExcept(new List<int> { keptHelp.Id });

            // Assert
            var keptResult = UserPageSettingsRepository.Read(userName1, keptHelp.Id);
            Assert.That(keptResult, Is.Not.Null, "The kept settings should still be in the data store.");
            var deletedResult1 = UserPageSettingsRepository.Read(userName1, deletedHelp.Id);
            Assert.That(deletedResult1, Is.Null, "Settings shoudl not exist for the first deleted page and first user.");
            var deletedResult2 = UserPageSettingsRepository.Read(userName2, deletedHelp.Id);
            Assert.That(deletedResult2, Is.Null, "Settings should not exist for the first deleted page and second user.");
            var anotherDeletedResult = UserPageSettingsRepository.Read(userName1, anotherDeletedHelp.Id);
            Assert.That(anotherDeletedResult, Is.Null, "Settings should not exist for the second deleted page and first user.");
        }
        public void Update(UserPageSettings userPageSettings)
        {
            using (var connection = SqlConnectionFactory.GetConnection())
            using (var command = connection.CreateCommand())
            {
                command.CommandType = CommandType.Text;
                command.CommandText = SqlScripts.UserPageSettings_Update;
                AddParameters(userPageSettings, command);
                command.Parameters.Add(new SqlParameter("@id", userPageSettings.Id));

                connection.Open();
                command.ExecuteNonQuery();

            }
        }
 private static void AddParameters(UserPageSettings userPageSettings, SqlCommand command)
 {
     command.Parameters.AddRange(new SqlParameter[]{
         new SqlParameter("@pageId", userPageSettings.PageId),
         new SqlParameter("@hidePage", userPageSettings.HidePage),
         new SqlParameter("@userName", userPageSettings.UserName),
     });
 }