Exemplo n.º 1
0
        public OperationResultInfo GetUser(UserReceiveModel userModel, bool authorizationSync = false)
        {
            try
            {
                var user = _userLogic.GetUser(userModel, authorizationSync);

                return(new OperationResultInfo()
                {
                    ErrorInfo = string.Empty,
                    OperationResult = OperationsResults.Successfully,
                    ToListener = ListenerType.UserInfoListener,
                    JsonData = user
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

                return(new OperationResultInfo()
                {
                    ErrorInfo = ex.Message,
                    OperationResult = OperationsResults.Unsuccessfully,
                    ToListener = ListenerType.UserInfoListener
                });
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Updating a user in Users database table
        /// </summary>
        /// <param name="userModel"><see cref="UserReceiveModel"/></param>
        public void Update(UserReceiveModel userModel)
        {
            using (DatabaseContext context = new DatabaseContext())
            {
                if (!userModel.Id.HasValue)
                {
                    throw new Exception("Ошибка передачи данных, поле Id модели не было установлено");
                }

                User usr = context.Users.FirstOrDefault(u => u.Id == userModel.Id.Value);
                if (usr == null)
                {
                    throw new Exception("Такого пользователя нет в БД");
                }

                usr.Password    = userModel.Password ?? usr.Password;
                usr.UserName    = userModel.UserName ?? usr.UserName;
                usr.PhoneNumber = userModel.PhoneNumber ?? usr.PhoneNumber;
                usr.Name        = userModel.Name ?? usr.Name;
                usr.SecondName  = userModel.SecondName ?? usr.SecondName;
                usr.Country     = userModel.Country ?? usr.Country;
                usr.Gender      = userModel.Gender ?? usr.Gender;
                usr.City        = userModel.City ?? usr.City;

                usr.PictureName      = userModel.File?.FileName ?? usr.PictureName;
                usr.PictureExtension = userModel.File?.Extension ?? usr.PictureExtension;
                usr.Picture          = userModel.File?.BinaryForm ?? usr.Picture;

                usr.IsOnline = userModel.IsOnline ?? usr.IsOnline;
                context.SaveChanges();
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Deleting a user from Users database table
 /// </summary>
 /// <param name="userModel"><see cref="UserReceiveModel"/></param>
 public void Delete(UserReceiveModel userModel)
 {
     using (DatabaseContext context = new DatabaseContext())
     {
         User usr = context.Users.FirstOrDefault(u => u.Login.Equals(userModel.Login));
         if (usr == null)
         {
             throw new Exception("Такого пользователя нет в БД");
         }
         context.Users.Remove(usr);
         context.SaveChanges();
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Get a user by Id or login and password
        /// </summary>
        /// <param name="user"><see cref="UserReceiveModel"/></param>
        /// <returns><see cref="UserResponseModel"/></returns>
        public UserResponseModel GetUser(UserReceiveModel user, bool authorizationSync)
        {
            using (var context = new DatabaseContext())
            {
                var userDb = context.Users
                             .FirstOrDefault(u => user.Id.HasValue && u.Id == user.Id.Value ||
                                             u.Login.Equals(user.Login) && u.Password.Equals(user.Password));

                if (userDb == null)
                {
                    throw new Exception("Пользователь не найден, возможно неправильный логин или пароль");
                }

                if (authorizationSync)
                {
                    userDb.IsOnline = true;
                }

                context.SaveChanges();

                return(new UserResponseModel()
                {
                    UserId = userDb.Id,
                    UserName = userDb.UserName,
                    Name = userDb.Name,
                    SecondName = userDb.SecondName,
                    Gender = userDb.Gender,
                    Country = userDb.Country,
                    City = userDb.City,
                    IsOnline = userDb.IsOnline,
                    PhoneNumber = userDb.PhoneNumber,
                    File = new FileModel()
                    {
                        FileName = userDb.PictureName,
                        Extension = userDb.PictureExtension,
                        BinaryForm = userDb.Picture
                    }
                });
            }
        }
Exemplo n.º 5
0
 public OperationResultInfo UserProfileUpdate(UserReceiveModel userModel)
 {
     try
     {
         _userLogic.Update(userModel);
         return(new OperationResultInfo()
         {
             ErrorInfo = string.Empty,
             ToListener = ListenerType.UserUpdateProfileListener,
             OperationResult = OperationsResults.Successfully
         });
     }
     catch (Exception ex)
     {
         return(new OperationResultInfo()
         {
             ErrorInfo = ex.Message,
             ToListener = ListenerType.UserUpdateProfileListener,
             OperationResult = OperationsResults.Unsuccessfully
         });
     }
 }
Exemplo n.º 6
0
 public OperationResultInfo UserRegistration(UserReceiveModel userModel)
 {
     try
     {
         _userLogic.Create(userModel);
         return(new OperationResultInfo()
         {
             ToListener = ListenerType.RegistrationListener,
             ErrorInfo = string.Empty,
             OperationResult = OperationsResults.Successfully
         });
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         return(new OperationResultInfo()
         {
             ToListener = ListenerType.RegistrationListener,
             ErrorInfo = ex.Message,
             OperationResult = OperationsResults.Unsuccessfully
         });
     }
 }
Exemplo n.º 7
0
        /// <summary>
        /// Creating a new user in Users database table
        /// </summary>
        /// <param name="userModel"><see cref="UserReceiveModel"/></param>
        public void Create(UserReceiveModel userModel)
        {
            using (DatabaseContext context = new DatabaseContext())
            {
                if (context.Users.FirstOrDefault(u => u.Login.Equals(userModel.Login)) != null)
                {
                    throw new Exception("Этот логин уже зарегистрирован!");
                }

                context.Users.Add(new User()
                {
                    UserName         = userModel.UserName,
                    Name             = userModel.Name,
                    SecondName       = userModel.SecondName,
                    Login            = userModel.Login,
                    Password         = userModel.Password,
                    PictureExtension = ".jpg",
                    PictureName      = "default",
                    Picture          = File.ReadAllBytes(@"Images\default.jpg")
                });
                context.SaveChanges();
            }
        }