示例#1
0
        public List <UserModel> GetAllUsers()
        {
            List <UserModel> users = DB.Users.Select(p => new UserModel(p)).ToList();

            for (int i = 0; i < users.Count; i++)
            {
                Adress adress = DB.Adresses.SingleOrDefault(p => p.AdressId == DB.UsersAdresses.SingleOrDefault(p => p.UserId == users[i].UserId).AdressId);
                if (adress != null)
                {
                    users[i].SetAdress(adress);
                }
                BranchUser branchToCheck = DB.BranchUsers.SingleOrDefault(p => p.UserId == users[i].UserId);
                if (branchToCheck != null)
                {
                    users[i].BranchId = branchToCheck.BranchId;
                }
            }
            return(users);
        }
示例#2
0
        public UserModel AddUser(UserModel userModel)
        {
            if (userModel.Image != null)
            {
                string extension = Path.GetExtension(userModel.Image.FileName);

                userModel.ImageFileName = Guid.NewGuid() + extension;

                using (FileStream fileStream = File.Create("Uploads/" + userModel.ImageFileName))
                {
                    userModel.Image.CopyTo(fileStream);
                }
                userModel.Image = null;
            }
            using (DB.Database.BeginTransaction())
            {
                if (userModel.Role == null)
                {
                    userModel.Role = "user";
                }

                byte[] salt = new byte[128 / 8];
                using (var rng = RandomNumberGenerator.Create())
                {
                    rng.GetBytes(salt);
                }
                userModel.Salt     = Convert.ToBase64String(salt);
                userModel.Password = Convert.ToBase64String(KeyDerivation.Pbkdf2(
                                                                password: userModel.Password,
                                                                salt: salt,
                                                                prf: KeyDerivationPrf.HMACSHA1,
                                                                iterationCount: 10000,
                                                                numBytesRequested: 256 / 8));

                User addedUser = userModel.ConvertToUser();
                DB.Users.Add(addedUser);
                DB.SaveChanges();

                if (userModel.City != null && userModel.AdressLine != null)
                {
                    Adress addedAdress = userModel.ConvertToAdress();
                    DB.Adresses.Add(addedAdress);
                    DB.SaveChanges();

                    UsersAdress usersAdress = new UsersAdress
                    {
                        UserId   = addedUser.UserId,
                        AdressId = addedAdress.AdressId
                    };
                    DB.SaveChanges();
                }

                BranchUser branchUserToAdd = new BranchUser
                {
                    UserId   = addedUser.UserId,
                    BranchId = userModel.BranchId
                };
                DB.BranchUsers.Add(branchUserToAdd);
                DB.SaveChanges();

                DB.Database.CommitTransaction();

                userModel.UserId = addedUser.UserId;
            }
            return(userModel);
        }
示例#3
0
        public UserModel UpdateUser(UserModel userModel)
        {
            if (userModel.Image != null)
            {
                string extension = Path.GetExtension(userModel.Image.FileName);

                userModel.ImageFileName = Guid.NewGuid() + extension;

                using (FileStream fileStream = File.Create("Uploads/" + userModel.ImageFileName))
                {
                    userModel.Image.CopyTo(fileStream);
                }
                userModel.Image = null;
            }
            User user = DB.Users.SingleOrDefault(p => p.UserId == userModel.UserId);

            if (user.FirstName != userModel.FirstName)
            {
                user.FirstName = userModel.FirstName;
            }

            if (user.LastName != userModel.LastName)
            {
                user.LastName = userModel.LastName;
            }

            if (user.Email != userModel.Email)
            {
                user.Email = userModel.Email;
            }

            if (user.Username != userModel.Username)
            {
                user.Username = userModel.Username;
            }

            if (user.Password != userModel.Password)
            {
                user.Password = userModel.Password;
            }

            if (user.ImageFileName != userModel.ImageFileName)
            {
                user.ImageFileName = userModel.ImageFileName;
            }

            Adress adress = DB.Adresses.SingleOrDefault(p => p.AdressId == DB.UsersAdresses.FirstOrDefault(p => p.UserId == userModel.UserId).AdressId);

            if (adress != null)
            {
                if (userModel.Country != adress.Country || userModel.City != adress.City || userModel.AdressLine != adress.AdressLine)
                {
                    if (adress.Country != userModel.Country)
                    {
                        adress.Country = userModel.Country;
                    }
                    if (adress.City != userModel.City)
                    {
                        adress.City = userModel.City;
                    }
                    if (adress.AdressLine != userModel.AdressLine)
                    {
                        adress.AdressLine = userModel.AdressLine;
                    }
                    if (adress.PostalZipCode != userModel.PostalZipCode)
                    {
                        adress.PostalZipCode = userModel.PostalZipCode;
                    }
                }
            }

            BranchUser branchUser = DB.BranchUsers.SingleOrDefault(p => p.UserId == userModel.UserId);

            if (branchUser == null)
            {
                BranchUser branchUserToAdd = new BranchUser
                {
                    BranchId = userModel.BranchId,
                    UserId   = userModel.UserId
                };
                DB.BranchUsers.Add(branchUserToAdd);
            }
            else
            {
                if (branchUser.BranchId != userModel.BranchId)
                {
                    branchUser.BranchId = userModel.BranchId;
                }
            }
            DB.SaveChanges();

            return(userModel);
        }
 public BranchUserModel(BranchUser branchUser)
 {
     BranchUserId = branchUser.BranchUserId;
     BranchId     = branchUser.BranchId;
     UserId       = branchUser.UserId;
 }