Ejemplo n.º 1
0
        public void CheckoutBook(int BookID, string DueDate)
        {
            var connectionString = "Server = 127.0.0.1; Port = 5432; Database = bookish; User Id = Bookish; Password = password; ";

            var UserID      = AccessDB.FindUserIDByEmail(Email, connectionString);
            var anyCopies   = AccessDB.AreThereAvailableCopies(BookID, connectionString);
            var DateTimeDue = DateTime.Parse(DueDate);

            if (anyCopies == true)
            {
                NpgsqlConnection connection = new Npgsql.NpgsqlConnection(connectionString);
                connection.Open();

                using (var transaction = connection.BeginTransaction())
                {
                    connection.Execute("INSERT INTO \"Books Taken Out\" (\"UserID\",\"BookID\", \"DueDate\") VALUES (@UserID, @BookID, @DateTimeDue)", new { UserID, BookID, DateTimeDue });
                    connection.Execute("UPDATE \"Books\" SET \"AvailableCopies\" = \"AvailableCopies\" - 1 WHERE \"BookID\" = @BookID", new { BookID });

                    transaction.Commit();
                }
                connection.Close();
            }
        }