Esempio n. 1
0
        /// <summary>
        /// Gets the home page view model for member.
        /// </summary>
        /// <remarks>
        /// This is an attempt to try and apply some better SOLID principals to the
        /// way I build these viewModels...
        /// </remarks>
        /// <param name="memberId">The member identifier.</param>
        /// <returns></returns>
        public HomePageViewModel GetHomePageViewModelForMember(int memberId)
        {
            Member currentUser = ApplicationDbContext.Members
                                 .Where(x => x.MemberId == memberId)
                                 .Include(x => x.Position)
                                 .ThenInclude(x => x.ParentComponent)
                                 .Include(x => x.Rank)
                                 .Include(x => x.Gender)
                                 .Include(x => x.Race)
                                 .Include(x => x.PhoneNumbers)
                                 .ThenInclude(x => x.Type)
                                 .FirstOrDefault();
            HomePageViewModel result = new HomePageViewModel(currentUser,
                                                             ApplicationDbContext.Components.ToList().ConvertAll(x => new ComponentSelectListItem(x)),
                                                             ApplicationDbContext.Genders.ToList().ConvertAll(x => new MemberGenderSelectListItem(x)),
                                                             ApplicationDbContext.Races.ToList().ConvertAll(x => new MemberRaceSelectListItem(x)),
                                                             ApplicationDbContext.Ranks.ToList().ConvertAll(x => new MemberRankSelectListItem(x)));
            SqlParameter param1 = new SqlParameter("@ComponentId", currentUser.Position.ParentComponent.ComponentId);

            List <Component> components = ApplicationDbContext.Components.FromSql("dbo.GetComponentAndChildrenDemo @ComponentId", param1).ToList();

            ApplicationDbContext.Set <Position>().Where(x => components.Contains(x.ParentComponent))
            .Include(y => y.Members).ThenInclude(z => z.Rank)
            .Include(y => y.Members).ThenInclude(z => z.Gender)
            .Include(y => y.Members).ThenInclude(x => x.Race)
            .Include(y => y.Members).ThenInclude(x => x.DutyStatus)
            .Include(y => y.Members).ThenInclude(x => x.PhoneNumbers)
            .Include(y => y.TempMembers).ThenInclude(x => x.Position)
            .ThenInclude(z => z.ParentComponent)
            .Load();

            List <HomePageComponentGroup> initial = components.ConvertAll(x => new HomePageComponentGroup(x));

            result.SetComponentOrder(initial);
            result.GetExceptionToDutyMembers();
            return(result);
        }