コード例 #1
0
        public void SetUp()
        {
            ClearDatabase();

            _user1 = new User
                {
                    Username = "******",
                    Password = "******",
                    LastLoginDate = new DateTime(2011, 1, 2, 3, 4, 5),
                };

            _user1.AddReview(new Review
                {
                    Title = "Review 1 title",
                    Body = "Review 1 body",
                    DateTime = new DateTime(2011, 2, 3, 4, 5, 6),
                    Rating = 5
                });

            _user1.AddReview(new Review
                {
                    Title = "Review 2 title",
                    Body = "Review 2 body",
                    DateTime = new DateTime(2011, 2, 3, 6, 7, 8),
                    Rating = 4
                });

            _repository = new AdoUserRepository();
        }
コード例 #2
0
        public void Delete(User user)
        {
            using (IDbConnection connection = new SqlConnection(ConnectionString))
            using (IDbCommand command = connection.CreateCommand())
            {
                connection.Open();

                command.CommandText =
                    "delete from Review where UserId = @id;"+
                    "delete from [User] where [User].Id = @id";

                var parameter = command.CreateParameter();
                parameter.DbType = DbType.Int32;
                parameter.ParameterName = "id";
                parameter.Value = user.Id;

                command.Parameters.Add(parameter);

                command.ExecuteNonQuery();
            }
        }
コード例 #3
0
        private void InsertReview(IDbConnection connection, User user, Review review)
        {
            using (IDbCommand command = connection.CreateCommand())
            {
                command.CommandText =
                    "insert into [Review] (UserId, Title, Body, Rating, DateTime) " +
                    "values (@userId, @title, @body, @rating, @dateTime);" +
                    "select SCOPE_IDENTITY()";

                var title = command.CreateParameter();
                title.DbType = DbType.String;
                title.ParameterName = "title";
                title.Value = review.Title;

                var body = command.CreateParameter();
                body.DbType = DbType.String;
                body.ParameterName = "body";
                body.Value = review.Body;

                var dateTime = command.CreateParameter();
                dateTime.DbType = DbType.DateTime;
                dateTime.ParameterName = "dateTime";
                dateTime.Value = review.DateTime;

                var rating = command.CreateParameter();
                rating.DbType = DbType.Int32;
                rating.ParameterName = "rating";
                rating.Value = review.Rating;

                var userId = command.CreateParameter();
                userId.DbType = DbType.Int32;
                userId.ParameterName = "userId";
                userId.Value = user.Id;

                command.Parameters.Add(title);
                command.Parameters.Add(body);
                command.Parameters.Add(dateTime);
                command.Parameters.Add(rating);
                command.Parameters.Add(userId);

                var id = Convert.ToInt32(command.ExecuteScalar());
                review.Id = id;
            }
        }
コード例 #4
0
 public void Update(User user)
 {
     // Orka ...
 }
コード例 #5
0
        public void Insert(User user)
        {
            using (IDbConnection connection = new SqlConnection(ConnectionString))
            {
                connection.Open();

                using (IDbCommand command = connection.CreateCommand())
                {
                    command.CommandText =
                        "insert into [User] (Username, Password, LastLoginDate) " +
                        "values (@username, @password, @lastLoginDate);" +
                        "select SCOPE_IDENTITY()";

                    var usernameParameter = command.CreateParameter();
                    usernameParameter.DbType = DbType.String;
                    usernameParameter.ParameterName = "username";
                    usernameParameter.Value = user.Username;

                    var passwordParameter = command.CreateParameter();
                    passwordParameter.DbType = DbType.String;
                    passwordParameter.ParameterName = "password";
                    passwordParameter.Value = user.Password;

                    var loginDateParameter = command.CreateParameter();
                    loginDateParameter.DbType = DbType.DateTime;
                    loginDateParameter.ParameterName = "lastLoginDate";
                    loginDateParameter.Value = user.LastLoginDate;

                    command.Parameters.Add(usernameParameter);
                    command.Parameters.Add(passwordParameter);
                    command.Parameters.Add(loginDateParameter);

                    var id = Convert.ToInt32(command.ExecuteScalar());

                    user.Id = id;
                }

                foreach (var review in user.Reviews)
                {
                    InsertReview(connection, user, review);
                }
            }
        }
コード例 #6
0
        public void SetUp()
        {
            DataDeleter.ClearDatabase();

            _user1 = new User
                {
                    Username = "******",
                    Password = "******",
                    LastLoginDate = new DateTime(2011, 1, 2, 3, 4, 5),
                };

            _user2 = new User
                {
                    Username = "******",
                    Password = "******",
                    LastLoginDate = new DateTime(2011, 2, 2, 3, 4, 5),
                };

            _user3 = new User
                {
                    Username = "******",
                    Password = "******",
                    LastLoginDate = new DateTime(2011, 3, 3, 3, 4, 5),
                };

            _user4 = new User
                {
                    Username = "******",
                    Password = "******",
                    LastLoginDate = new DateTime(2011, 4, 4, 4, 4, 5),
                };

            _user1.AddReview(new Review
                {
                    Title = "User 1's first review",
                    Body = "This is the first review written by User 1",
                    DateTime = new DateTime(2011, 2, 3, 4, 5, 6),
                    Rating = 5
                });

            _user1.AddReview(new Review
                {
                    Title = "User 1's second review",
                    Body = "This is the second review written by User 1",
                    DateTime = new DateTime(2011, 2, 3, 6, 7, 8),
                    Rating = 4
                });

            _user2.AddReview(new Review
                {
                    Title = "User 2's first review",
                    Body = "This is the first review written by User 2",
                    DateTime = new DateTime(2011, 2, 4, 5, 6, 7),
                    Rating = 3
                });

            _user3.AddReview(new Review
                {
                    Title = "User 3's first review",
                    Body = "This is the first review written by User 3",
                    DateTime = new DateTime(2011, 2, 5, 6, 7, 8),
                    Rating = 5
                });

            _user3.AddReview(new Review
                {
                    Title = "User 3's second review",
                    Body = "This is the second review written by User 3",
                    DateTime = new DateTime(2011, 2, 5, 7, 8, 9),
                    Rating = 5
                });

            SaveAllUsers();

            _repository = new UserRepository();
        }
コード例 #7
0
        public void Save_writes_the_user_to_the_database()
        {
            var user = new User
                {
                    Username = "******",
                    LastLoginDate = new DateTime(2011, 5, 5, 5, 5, 5)
                };

            _repository.Save(user);

            var actual = _repository.Get(user.Id);

            Assert.AreEqual("new user", actual.Username);
        }
コード例 #8
0
        private static void CreateTestData()
        {
            using (var session = SessionFactorySingleton.OpenSession())
            using (var transaction = session.BeginTransaction())
            {
                var user1 = new User
                    {
                        Username = "******",
                        Password = "******",
                        LastLoginDate = new DateTime(2011, 1, 2, 3, 4, 5),
                    };

                var user2 = new User
                    {
                        Username = "******",
                        Password = "******",
                        LastLoginDate = new DateTime(2022, 2, 2, 3, 4, 5),
                    };

                var user3 = new User
                    {
                        Username = "******",
                        Password = "******",
                        LastLoginDate = new DateTime(3033, 3, 3, 3, 4, 5),
                    };

                user1.AddReview(new Review
                    {
                        Title = "User 1's first review",
                        Body = "This is the first review written by User 1",
                        DateTime = new DateTime(2011, 2, 3, 4, 5, 6),
                        Rating = 5
                    });

                user1.AddReview(new Review
                    {
                        Title = "User 1's second review",
                        Body = "This is the second review written by User 1",
                        DateTime = new DateTime(2011, 2, 3, 6, 7, 8),
                        Rating = 4
                    });

                user2.AddReview(new Review
                    {
                        Title = "User 2's first review",
                        Body = "This is the first review written by User 2",
                        DateTime = new DateTime(2011, 2, 4, 5, 6, 7),
                        Rating = 5
                    });

                user2.AddReview(new Review
                    {
                        Title = "User 2's second review",
                        Body = "This is the second review written by User 2",
                        DateTime = new DateTime(2022, 2, 4, 6, 7, 8),
                        Rating = 4
                    });

                user2.AddReview(new Review
                    {
                        Title = "User 2's third review",
                        Body = "This is the third review written by User 2",
                        DateTime = new DateTime(2022, 2, 4, 7, 8, 9),
                        Rating = 3
                    });

                user3.AddReview(new Review
                    {
                        Title = "User 3's first review",
                        Body = "This is the first review written by User 3",
                        DateTime = new DateTime(3011, 3, 4, 5, 6, 7),
                        Rating = 1
                    });

                session.Save(user1);
                session.Save(user2);
                session.Save(user3);

                transaction.Commit();
            }
        }