public void Create(User user)
        {
            var executor = new CommandExecutor("dbo.CreateUser", connectionString);

            executor.SetParam("@Username", user.Login, SqlDbType.NVarChar);
            executor.SetParam("@Password", user.Password, SqlDbType.NVarChar);

            var userType = (int) user.Type + 1;
            executor.SetParam("@Usertype", userType, SqlDbType.Int);

            var result = executor.ExecuteCommand();
            result.ThrowIfException();
        }
        public void Delete(Ticket ticket)
        {
            var executor = new CommandExecutor("dbo.DeleteTicket", connectionString);
            executor.SetParam("@Id", ticket.Id, SqlDbType.Int);

            executor.ExecuteCommand(true).ThrowIfException();
        }
        public IEnumerable<Showtime> GetPendingShowtimes(int movieId)
        {
            var executor = new CommandExecutor("dbo.BrowsePendingShowtimes", connectionString);
            executor.SetParam("@MovieId", movieId, SqlDbType.Int);
            var result = executor.ExecuteCommand();

            result.ThrowIfException();

            var dataSet = result as DataSet;

            foreach (DataRow row in dataSet.Tables[0].Rows)
            {
                yield return new Showtime
                {
                    Id = row["Id"].ToInt(),
                    Time = row["ShowtimeDate"].ToDate(),
                    Movie = new Movie
                    {
                        Id = row["MovieId"].ToInt(),
                        Title = row["MovieTitle"].ToString()
                    },
                    Auditorium = new Auditorium
                    {
                        Id = row["AuditoriumId"].ToInt(),
                        Name = row["AuditoriumName"].ToString(),
                        Rows = row["AuditoriumRows"].ToInt(),
                        Seats = row["AuditoriumSeats"].ToInt()
                    },
                    Price = row["Price"].ToInt(),
                    ThreeDee = row["ThreeDee"].ToBool()
                };
            }
        }
        public void Delete(Auditorium auditorium)
        {
            var executor = new CommandExecutor("dbo.DeleteAuditorium", connectionString);
            executor.SetParam("@Id", auditorium.Id, SqlDbType.Int);

            executor.ExecuteCommand(true).ThrowIfException();
        }
        public void Delete(Showtime showtime)
        {
            var executor = new CommandExecutor("dbo.DeleteShowtime", connectionString);
            executor.SetParam("@Id", showtime.Id, SqlDbType.Int);

            executor.ExecuteCommand(true).ThrowIfException();
        }
        public void Delete(User user)
        {
            var executor = new CommandExecutor("dbo.DeleteUser", connectionString);

            executor.SetParam("@Username", user.Login, SqlDbType.NVarChar);

            var result = executor.ExecuteCommand();
            result.ThrowIfException();
        }
        public void SetLogo(BitmapImage logo)
        {
            var executor = new CommandExecutor("dbo.SetLogo", connectionString);

            var logoData = logo == null ? DBNull.Value : (object)logo.ToByteArray();
            executor.SetParam("@Logo", logoData, SqlDbType.Image);

            executor.ExecuteCommand(true).ThrowIfException();
        }
        public IEnumerable<Seat> GetOccupiedSeats(int showtimeId)
        {
            var executor = new CommandExecutor("dbo.GetOccupiedSeats", connectionString);
            executor.SetParam("@ShowtimeId", showtimeId, SqlDbType.Int);
            var result = executor.ExecuteCommand();

            result.ThrowIfException();

            var dataSet = result as DataSet;

            foreach (DataRow row in dataSet.Tables[0].Rows)
            {
                yield return new Seat
                {
                    SeatNumber = row["SeatNumber"].ToInt(),
                    RowNumber = row["RowNumber"].ToInt()
                };
            }
        }
        public void RegisterTickets(int showtimeId, List<Seat> seats)
        {
            var executor = new CommandExecutor("dbo.RegisterTickets", connectionString);

            executor.SetParam("@ShowtimeId", showtimeId, SqlDbType.Int);

            var seatList = new DataTable();
            seatList.Columns.Add("SeatNumber");
            seatList.Columns.Add("RowNumber");

            seats.ForEach(seat => seatList.Rows.Add(seat.SeatNumber, seat.RowNumber));
            executor.SetParam("@Tickets", seatList, SqlDbType.Structured, "dbo.SeatList");

            executor.ExecuteCommand(true).ThrowIfException();
        }
        public void Save(Auditorium auditorium, bool update)
        {
            CommandExecutor executor;
            if (update)
            {
                executor = new CommandExecutor("dbo.UpdateAuditorium", connectionString);
                executor.SetParam("@Id", auditorium.Id, SqlDbType.Int);
            }
            else
            {
                executor = new CommandExecutor("dbo.CreateAuditorium", connectionString);
            }

            executor.SetParam("@Name", auditorium.Name, SqlDbType.NVarChar);
            executor.SetParam("@Rows", auditorium.Rows, SqlDbType.Int);
            executor.SetParam("@Seats", auditorium.Seats, SqlDbType.Int);

            executor.ExecuteCommand(true).ThrowIfException();
        }
        public void Save(Showtime showtime, bool update)
        {
            CommandExecutor executor;
            if (update)
            {
                executor = new CommandExecutor("dbo.UpdateShowtime", connectionString);
                executor.SetParam("@Id", showtime.Id, SqlDbType.Int);
            }
            else
            {
                executor = new CommandExecutor("dbo.CreateShowtime", connectionString);
            }

            executor.SetParam("@MovieId", showtime.Movie.Id, SqlDbType.Int);
            executor.SetParam("@AuditoriumId", showtime.Auditorium.Id, SqlDbType.Int);
            executor.SetParam("@ShowtimeDate", showtime.Time, SqlDbType.DateTime);
            executor.SetParam("@Price", showtime.Price, SqlDbType.Money);
            executor.SetParam("@ThreeDee", showtime.ThreeDee, SqlDbType.Bit);

            executor.ExecuteCommand(true).ThrowIfException();
        }