Example #1
0
        public UserResponse Update(UserViewModel user)
        {
            UserResponse response = new UserResponse();

            try
            {
                response.User = unitOfWork.GetUserRepository().Update(user.ConvertToUser())
                                .ConvertToUserViewModel();
                unitOfWork.Save();
                response.Success = true;
            }
            catch (Exception ex)
            {
                response.User    = null;
                response.Success = false;
                response.Message = ex.Message;
            }

            return(response);
        }
Example #2
0
        public UserResponse Create(UserViewModel re)
        {
            UserResponse response = new UserResponse();

            try
            {
                var companyUsers = re.CompanyUsers?.ConvertToCompanyUserList();

                re.CompanyUsers = null;

                User addedUser = unitOfWork.GetUserRepository().Create(re.ConvertToUser());

                var itemsFromDB = unitOfWork.GetCompanyUserRepository().GetCompanyUsersByUser(addedUser.Id);
                foreach (var item in itemsFromDB)
                {
                    if (!companyUsers.Select(x => x.Identifier).Contains(item.Identifier))
                    {
                        unitOfWork.GetCompanyUserRepository().Delete(item.Identifier);
                    }
                }

                foreach (var compUser in companyUsers)
                {
                    compUser.UserId = addedUser.Id;

                    unitOfWork.GetCompanyUserRepository().Create(compUser);
                }

                unitOfWork.Save();
                response.User    = addedUser.ConvertToUserViewModel();
                response.Success = true;
            }
            catch (Exception ex)
            {
                response.User    = new UserViewModel();
                response.Success = false;
                response.Message = ex.Message;
            }

            return(response);
        }