Exemplo n.º 1
0
        public void AddGetUpdateDeleteUser()
        {
            userRepository = new SimpleChatRepository <Users>(databaseSettings);
            userRepository.CreatDatabase();
            user = new Users
            {
                //Id = 1,
                Name = "Serge"
            };

            userRepository.Add(user);
            userRepository.Save();
            var userFromDb = userRepository.GetItem(1);

            Assert.IsNotNull(userFromDb);
            Assert.AreEqual(userFromDb.Id, 1);
            Assert.AreEqual(userFromDb.Name, "Serge");

            user.Name = "Andrei";
            userRepository.Update(user);
            userRepository.Save();
            userFromDb = userRepository.GetItem(1);

            Assert.IsNotNull(userFromDb);
            Assert.AreEqual(userFromDb.Id, 1);
            Assert.AreEqual(userFromDb.Name, "Andrei");

            userRepository.DeleteItem(1);
            userRepository.Save();
            userFromDb = userRepository.GetItem(1);;
            Assert.IsNull(userFromDb);

            userRepository.ContextDispose();
        }
Exemplo n.º 2
0
        public void AddGetUpdateDeleteRole()
        {
            roleRepository = new SimpleChatRepository <Roles>(databaseSettings);

            role = new Roles
            {
                NameRole = "main",
                Id       = 1
            };

            roleRepository.Add(role);
            roleRepository.Save();
            var roleFromDb = roleRepository.GetItem(1);

            Assert.IsNotNull(role);
            Assert.AreEqual(roleFromDb.Id, 1);
            Assert.AreEqual(roleFromDb.NameRole, "main");

            role.NameRole = "admin";
            roleRepository.Update(role);
            roleRepository.Save();
            roleFromDb = roleRepository.GetItem(1);

            Assert.IsNotNull(roleFromDb);
            Assert.AreEqual(roleFromDb.Id, 1);
            Assert.AreEqual(roleFromDb.NameRole, "admin");

            roleRepository.DeleteItem(1);
            roleRepository.Save();
            roleFromDb = roleRepository.GetItem(1);
            Assert.IsNull(roleFromDb);

            roleRepository.ContextDispose();
        }
Exemplo n.º 3
0
 public void ShowMessages()
 {
     try
     {
         messageTable.Rows.Clear();
         messageId = messageRepository.Count();
         i         = 1;
         if (messageId < 11)
         {
             while (i <= messageId)
             {
                 var messageFromDb = messageRepository.GetItem(i);
                 var userFromDb    = userRepository.GetItem(messageFromDb.UserId);
                 row             = messageTable.NewRow();
                 row["NameUser"] = userFromDb.Name;
                 row["Messages"] = messageFromDb.Text;
                 messageTable.Rows.Add(row);
                 i++;
             }
         }
         else
         {
             i = messageId - 9;
             while (i <= messageId)
             {
                 var messageFromDb = messageRepository.GetItem(i);
                 var userFromDb    = userRepository.GetItem(messageFromDb.UserId);
                 row             = messageTable.NewRow();
                 row["NameUser"] = userFromDb.Name;
                 row["Messages"] = messageFromDb.Text;
                 messageTable.Rows.Add(row);
                 i++;
             }
         }
     }
     catch (Exception e)
     {
         log.Error("Ошибка показа сообщений", e);
     }
 }
Exemplo n.º 4
0
        public void AddGetUpdateDeleteMessage()
        {
            messageRepository = new SimpleChatRepository <Messages>(databaseSettings);

            message = new Messages
            {
                Id     = 1,
                UserId = 1,
                Text   = "textofmessage"
            };

            messageRepository.Add(message);
            messageRepository.Save();
            var messageFromDb = messageRepository.GetItem(1);

            Assert.IsNotNull(message);
            Assert.AreEqual(messageFromDb.Id, 1);
            Assert.AreEqual(messageFromDb.UserId, 1);
            Assert.AreEqual(messageFromDb.Text, "textofmessage");

            message.Text = "anotherText";
            messageRepository.Update(message);
            messageRepository.Save();
            messageFromDb = messageRepository.GetItem(1);

            Assert.IsNotNull(messageFromDb);
            Assert.AreEqual(messageFromDb.Id, 1);
            Assert.AreEqual(messageFromDb.UserId, 1);
            Assert.AreEqual(messageFromDb.Text, "anotherText");

            messageRepository.DeleteItem(1);
            messageRepository.Save();
            messageFromDb = messageRepository.GetItem(1);
            Assert.IsNull(messageFromDb);

            messageRepository.ContextDispose();
        }
Exemplo n.º 5
0
        public string Authorization(string name, string pass)
        {
            try
            {
                SetSettings();
                userRepository = new SimpleChatRepository <Users>(databaseSettings);
                userRepository.CreatDatabase();
                user = new Users();

                i = 0;
                while (i < userRepository.Count())
                {
                    i++;
                    var userFromDb = userRepository.GetItem(i);
                    if (userFromDb.Name == name)
                    {
                        user = userFromDb; break;
                    }
                }
                if (user == null)
                {
                    otvet = "Пользователь не найден";
                }
                else if (user.Pass == Convert.ToInt32(pass))
                {
                    otvet = "Пользователь авторизован";
                    log.Info($"Пользователь {name} авторизован");
                }
                else
                {
                    otvet = "Пароль неверный";
                    log.Info($"Попытка авторизации {name} не удалась");
                }
            }
            catch (Exception e)
            {
                otvet = "Ошибка авторизации";
                log.Error(otvet, e);
            }
            return(otvet);
        }