private void UserStatisticsLabel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            UserStatisticsWindow usw = new UserStatisticsWindow()
            {
                WindowState = this.WindowState,
                Top         = this.Top,
                Left        = this.Left,
                Width       = this.Width,
                Height      = this.Height
            };

            usw.Show();
            this.Close();
        }
Beispiel #2
0
        private void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            if (LoginTextBox.Text == "Admin" && PasswordBox.Password == "0000")
            {
                UserStatisticsWindow usw = new UserStatisticsWindow()
                {
                    WindowState = this.WindowState,
                    Top         = this.Top,
                    Left        = this.Left,
                    Width       = this.Width,
                    Height      = this.Height
                };
                usw.Show();
                this.Close();
            }
            else
            {
                foreach (User user in users)
                {
                    if ((LoginTextBox.Text == user.Login || LoginTextBox.Text == user.Email) && PasswordBox.Password == user.Password)
                    {
                        currentUser = user;
                        using (SqlConnection connection = new SqlConnection(connectionString))
                        {
                            connection.Open();
                            command.Connection = connection;

                            if (RememberMeCheckBox.IsChecked == true)
                            {
                                using (FileStream fs = new FileStream($"{Debug}\\Data\\currentUser", FileMode.OpenOrCreate))
                                {
                                    formatter.Serialize(fs, currentUser);
                                }
                            }


                            command.CommandText = $"UPDATE Users SET LastEntrance = N'{DateTime.Now.ToString()}', Status = N'Online' WHERE Login = N'{currentUser.Login}'";
                            command.ExecuteNonQuery();
                            DateTime entryTime = DateTime.Now;

                            command.CommandText = $"SELECT Name, CurrentTopicId, ChoiceOfTopicGridVisibility, SequenceOfIndicesOfMainTopic, IndexOfMainTopic, Id FROM Users WHERE Login = N'{currentUser.Login}'";
                            reader = command.ExecuteReader();
                            reader.Read();
                            currentUser.Name           = reader.GetString(0);
                            currentUser.CurrentTopicId = reader.GetString(1);
                            currentUser.ChoiceOfTopicGridVisibility  = reader.GetString(2);
                            currentUser.SequenceOfIndicesOfMainTopic = ReadIndices(reader.GetString(3));
                            currentUser.IndexOfMainTopic             = reader.GetInt16(4);
                            currentUser.Id = reader.GetInt32(5);
                            reader.Close();

                            List <Topic> oldTopics = new List <Topic>(), newTopics = new List <Topic>();
                            command.CommandText = $"SELECT Id, Name, SequenceOfIndices, CurrentIndex FROM Topics WHERE UserId = {currentUser.Id}";
                            reader = command.ExecuteReader();
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    oldTopics.Add(new Topic
                                    {
                                        Id   = reader.GetInt32(0).ToString(),
                                        Name = reader.GetString(1).Trim(),
                                        SequenceOfIndices = ReadIndices(reader.GetString(2)),
                                        CurrentIndex      = reader.GetInt16(3)
                                    });

                                    newTopics.Add(new Topic
                                    {
                                        Id   = reader.GetInt32(0).ToString(),
                                        Name = reader.GetString(1).Trim(),
                                        SequenceOfIndices = ReadIndices(reader.GetString(2)),
                                        CurrentIndex      = reader.GetInt16(3)
                                    });
                                }
                            }
                            reader.Close();

                            if (oldTopics.Count > 0)
                            {
                                foreach (Topic topic in oldTopics)
                                {
                                    command.CommandText = $"SELECT Id, isQuestionFirst, Questions, Contexts, Translations, WaysToQuestionsVoice, " +
                                                          $"WayToSentenceVoice FROM " +
                                                          $"Sentences WHERE TopicId = {Int32.Parse(topic.Id)}";
                                    reader = command.ExecuteReader();
                                    if (reader.HasRows)
                                    {
                                        while (reader.Read())
                                        {
                                            topic.Sentences.Add(new Sentence
                                            {
                                                id                   = reader.GetInt32(0).ToString(),
                                                topicId              = topic.Id,
                                                isQuestionFirst      = bool.Parse(reader.GetString(1)),
                                                questions            = ReadSequence(reader.GetString(2)),
                                                contexts             = ReadSequence(reader.GetString(3)),
                                                translations         = ReadSequence(reader.GetString(4)),
                                                waysToQuestionsVoice = ReadSequence(reader.GetString(5)),
                                                wayToSentenceVoice   = reader.GetString(6)
                                            });
                                        }
                                    }
                                    reader.Close();
                                }


                                foreach (Topic topic in newTopics)
                                {
                                    command.CommandText = $"SELECT Id, isQuestionFirst, Questions, Contexts, Translations, WaysToQuestionsVoice, " +
                                                          $"WayToSentenceVoice FROM " +
                                                          $"Sentences WHERE TopicId = {Int32.Parse(topic.Id)}";
                                    reader = command.ExecuteReader();
                                    if (reader.HasRows)
                                    {
                                        while (reader.Read())
                                        {
                                            topic.Sentences.Add(new Sentence
                                            {
                                                id                   = reader.GetInt32(0).ToString(),
                                                topicId              = topic.Id,
                                                isQuestionFirst      = bool.Parse(reader.GetString(1)),
                                                questions            = ReadSequence(reader.GetString(2)),
                                                contexts             = ReadSequence(reader.GetString(3)),
                                                translations         = ReadSequence(reader.GetString(4)),
                                                waysToQuestionsVoice = ReadSequence(reader.GetString(5)),
                                                wayToSentenceVoice   = reader.GetString(6)
                                            });
                                        }
                                    }
                                    reader.Close();
                                }
                            }

                            Topic MainTopic = new Topic();
                            command.CommandText = "SELECT Id, isQuestionFirst, Questions, Contexts, Translations, WaysToQuestionsVoice, " +
                                                  $"WayToSentenceVoice FROM MainTopic";
                            reader = command.ExecuteReader();

                            while (reader.Read())
                            {
                                MainTopic.Sentences.Add(new Sentence
                                {
                                    id = reader.GetInt32(0).ToString(),
                                    isQuestionFirst      = bool.Parse(reader.GetString(1)),
                                    questions            = ReadSequence(reader.GetString(2)),
                                    contexts             = ReadSequence(reader.GetString(3)),
                                    translations         = ReadSequence(reader.GetString(4)),
                                    waysToQuestionsVoice = ReadSequence(reader.GetString(5)),
                                    wayToSentenceVoice   = reader.GetString(6)
                                });
                            }
                            reader.Close();

                            if (currentUser.SequenceOfIndicesOfMainTopic.Count == MainTopic.Sentences.Count)
                            {
                                for (int i = 0; i < MainTopic.Sentences.Count; i++)
                                {
                                    MainTopic.currentSentences.Add(MainTopic.Sentences[currentUser.SequenceOfIndicesOfMainTopic[i]]);
                                }
                            }
                            else if (currentUser.SequenceOfIndicesOfMainTopic.Count < MainTopic.Sentences.Count)
                            {
                                int maxIndex = currentUser.SequenceOfIndicesOfMainTopic.Count - 1;
                                if (currentUser.IndexOfMainTopic > maxIndex)
                                {
                                    currentUser.IndexOfMainTopic = maxIndex;
                                }
                                for (int i = 0; i < MainTopic.Sentences.Count; i++)
                                {
                                    if (i <= maxIndex)
                                    {
                                        MainTopic.currentSentences.Add(MainTopic.Sentences[currentUser.SequenceOfIndicesOfMainTopic[i]]);
                                    }
                                    else
                                    {
                                        MainTopic.currentSentences.Add(MainTopic.Sentences[i]);
                                        currentUser.SequenceOfIndicesOfMainTopic.Add(i);
                                    }
                                }
                                command.CommandText = $"UPDATE Users SET SequenceOfIndicesOfMainTopic = N'{User.WriteSequence(currentUser.SequenceOfIndicesOfMainTopic)}' " +
                                                      $"WHERE Id = {currentUser.Id}";
                                command.ExecuteNonQuery();
                            }
                            else
                            {
                                currentUser.SequenceOfIndicesOfMainTopic.Clear();
                                for (int i = 0; i < MainTopic.Sentences.Count; i++)
                                {
                                    MainTopic.currentSentences.Add(MainTopic.Sentences[i]);
                                    currentUser.SequenceOfIndicesOfMainTopic.Add(i);
                                }
                                command.CommandText = $"UPDATE Users SET SequenceOfIndicesOfMainTopic = N'{User.WriteSequence(currentUser.SequenceOfIndicesOfMainTopic)}' " +
                                                      $"WHERE Id = {currentUser.Id}";
                                command.ExecuteNonQuery();
                            }


                            List <int> soiomtOldUser = new List <int>(), soiomtNewUser = new List <int>();
                            for (int i = 0; i < currentUser.SequenceOfIndicesOfMainTopic.Count; i++)
                            {
                                soiomtOldUser.Add(currentUser.SequenceOfIndicesOfMainTopic[i]);
                                soiomtNewUser.Add(currentUser.SequenceOfIndicesOfMainTopic[i]);
                            }

                            MainWindow mw = new MainWindow
                            {
                                needToUpdate = true,
                                WindowState  = this.WindowState,
                                Top          = this.Top,
                                Left         = this.Left,
                                Width        = this.Width,
                                Height       = this.Height,
                                entryTime    = entryTime,

                                oldUser = new User
                                {
                                    Id       = currentUser.Id,
                                    Login    = currentUser.Login,
                                    Password = currentUser.Password,
                                    Name     = currentUser.Name,
                                    Email    = currentUser.Email,
                                    ChoiceOfTopicGridVisibility = currentUser.ChoiceOfTopicGridVisibility,
                                    CurrentTopicId               = currentUser.CurrentTopicId,
                                    IndexOfMainTopic             = currentUser.IndexOfMainTopic,
                                    SequenceOfIndicesOfMainTopic = soiomtOldUser
                                },


                                newUser = new User
                                {
                                    Id       = currentUser.Id,
                                    Login    = currentUser.Login,
                                    Password = currentUser.Password,
                                    Name     = currentUser.Name,
                                    Email    = currentUser.Email,
                                    ChoiceOfTopicGridVisibility = currentUser.ChoiceOfTopicGridVisibility,
                                    CurrentTopicId               = currentUser.CurrentTopicId,
                                    IndexOfMainTopic             = currentUser.IndexOfMainTopic,
                                    SequenceOfIndicesOfMainTopic = soiomtNewUser
                                },

                                MainTopic = MainTopic,
                                oldTopics = oldTopics,
                                newTopics = newTopics
                            };
                            mw.Show();
                            this.Close();
                        }

                        break;
                    }
                    else
                    {
                        reader.Close();
                        WrongDataLabel.Visibility = Visibility.Visible;
                    }
                }

                using (FileStream fs = new FileStream($"{Debug}\\Data\\RememberMeCheckBoxIsChecked", FileMode.OpenOrCreate))
                {
                    formatter.Serialize(fs, RememberMeCheckBox.IsChecked);
                }
            }
        }