//event handler which update laboratory book after changes in view model

        public async void Column_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            var connectionString = ConfigurationManager.ConnectionStrings["cs_login"].ConnectionString;
            var connection       = new MySqlConnection(connectionString);

            var column = sender as ColumnModel;

            var commandString = $"SELECT db_name FROM db_list WHERE db_id = {this.LaboratoryBookID}";

            var sqlCommand = new MySqlCommand(commandString, connection);

            try
            {
                await connection.OpenAsync();

                var         laboratoryBookName = (string)(await sqlCommand.ExecuteScalarAsync());
                ColumnModel oldColumn          = column;
                foreach (ColumnModel oldCol in ColumnListBeforeChange)
                {
                    var columnMatched = false;
                    foreach (ColumnModel newCol in LaboratoryBookColumns)
                    {
                        if (oldCol.ColumnName == newCol.ColumnName)
                        {
                            columnMatched = true;
                        }
                    }

                    if (!columnMatched)
                    {
                        oldColumn = oldCol;
                        break;
                    }
                }

                sqlCommand.CommandText = $"ALTER TABLE laboratory_book_{laboratoryBookName} " +
                                         $"CHANGE COLUMN `{oldColumn.ColumnName}` `{column.ColumnName}` {column.ColumnType} " +
                                         $"NULL DEFAULT NULL; ";

                var result = await sqlCommand.ExecuteNonQueryAsync();

                ColumnListBeforeChange = new List <ColumnModel>();

                foreach (var _column in LaboratoryBookColumns)
                {
                    var col = new ColumnModel();
                    col.ColumnName = _column.ColumnName;
                    col.ColumnType = _column.ColumnType;

                    ColumnListBeforeChange.Add(col);
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show
                (
                    exception.Message,
                    "Column update error",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error
                );

                this.LaboratoryBookColumns.Clear();

                foreach (var _column in ColumnListBeforeChange)
                {
                    var col = new ColumnModel();
                    col.ColumnName       = _column.ColumnName;
                    col.ColumnType       = _column.ColumnType;
                    col.PropertyChanged += Column_PropertyChanged;
                    LaboratoryBookColumns.Add(col);
                }
                this.SelectedColumn = LaboratoryBookColumns[0];
            }
            finally
            {
                await connection.CloseAsync();

                sqlCommand?.Dispose();
            }
        }
        public ModifyDatabaseViewModel(User laboratoryBookUser, string laboratoryBookName)
        {
            this.LaboratoryBookUser = laboratoryBookUser;
            this.LaboratoryBookName = laboratoryBookName;

            this.LaboratoryBookID = GetLaboratoryBookID(laboratoryBookName);
            // set users list
            this.LaboratoryBookUsers = GetUsers(this.LaboratoryBookID);
            foreach (var user in LaboratoryBookUsers)
            {
                user.PropertyChanged += User_PropertyChanged;
            }
            if (LaboratoryBookUsers.Any())
            {
                this.SelectedUser = LaboratoryBookUsers[0];
            }
            //set permissions list
            this.LaboratoryBookPermissions = GetPermissions();

            //set columns list
            this.LaboratoryBookColumns = GetColumns(this.LaboratoryBookID);

            if (LaboratoryBookColumns.Count() > 0)
            {
                this.SelectedColumn = this.LaboratoryBookColumns[0];
            }
            foreach (var column in LaboratoryBookColumns)
            {
                column.PropertyChanged += Column_PropertyChanged;
            }
            this.ColumnListBeforeChange = new List <ColumnModel>();
            foreach (var column in LaboratoryBookColumns)
            {
                var col = new ColumnModel
                {
                    ColumnName = column.ColumnName,
                    ColumnType = column.ColumnType
                };

                ColumnListBeforeChange.Add(col);
            }

            //set laboratory book lists
            this.LaboratoryBookLists = new ObservableCollection <ListModel>();
            var taskList = new List <Task <ListModel> >();

            var materialTask  = Task.Run(() => GetListModel("material", laboratoryBookName));
            var substrateTask = Task.Run(() => GetListModel("substrate", laboratoryBookName));
            var regimeTask    = Task.Run(() => GetListModel("regime", laboratoryBookName));

            taskList.AddRange(new List <Task <ListModel> >()
            {
                materialTask, substrateTask, regimeTask
            });

            while (taskList.Any())
            {
                var finishedTaskIndex = Task.WaitAny(taskList.ToArray());

                var listModel = taskList[finishedTaskIndex].Result;

                foreach (ListValueModel valueModel in listModel.Values)
                {
                    valueModel.PropertyChanged += ListModel_PropertyChanged;
                }

                LaboratoryBookLists.Add(listModel);

                taskList.Remove(taskList[finishedTaskIndex]);
            }

            if (this.LaboratoryBookLists.Any())
            {
                this.SelectedList = this.LaboratoryBookLists[0];
            }
        }