Esempio n. 1
0
        public static UserViewModel CreateForPi(IRepositoryFactory repositoryFactory, User user = null)
        {
            var viewModel = new UserViewModel()
                {
                    User = user ?? new User(),
                    Universities = repositoryFactory.UniversityRepository.Queryable.OrderBy(a => a.Name)
                                        .Select(a => new SelectListItem() { Text = a.Name, Value = a.Id.ToString() })
                                        .ToList(),
                    Departments = new List<SelectListItem>()
                };

            if (user != null)
            {
                if (user.University != null)
                {
                    viewModel.Departments =
                        repositoryFactory.DepartmentRepository.Queryable.Where(a => a.University == user.University)
                            .OrderBy(a => a.Name)
                            .Select(a => new SelectListItem() {Text = a.Name, Value = a.Id.ToString()})
                            .ToList();

                    var uni = viewModel.Universities.FirstOrDefault(a => a.Value == user.University.Id.ToString());
                    uni.Selected = true;
                }

                if (user.Department != null)
                {
                    var dept = viewModel.Departments.FirstOrDefault(a => a.Value == user.Department.Id.ToString());
                    dept.Selected = true;
                }

                viewModel.IsPi = Roles.IsUserInRole(user.UserName, RoleNames.PI);
                viewModel.IsStaff = Roles.IsUserInRole(user.UserName, RoleNames.Staff);
            }

            return viewModel;
        }
Esempio n. 2
0
        public static UserViewModel CreateForUser(IRepositoryFactory repositoryFactory, string currentUserId, User user = null)
        {
            var viewModel = new UserViewModel()
                {
                    User = user ?? new User()
                    //RechargeAccounts = repositoryFactory.RechargeAccountRepository.Queryable.Where(a => a.User.UserName == currentUserId).ToList()
                };

            var ra = repositoryFactory.RechargeAccountRepository.Queryable.Where(a => a.User.UserName == currentUserId).ToList();
            viewModel.RechargeAccounts = ra.Select(a => new SelectListItem()
                {
                    Selected = viewModel.User.RechargeAccounts.Select(b => b.AccountNum).Contains(a.AccountNum),
                    Value = a.Id.ToString(),
                    Text = a.AccountNum
                }).ToList();

            return viewModel;
        }