public static void ConfigureAutoMapper() { Mapper.CreateMap <PlanDO, PlanNoChildrenDTO>().ForMember(a => a.Id, opts => opts.ResolveUsing(ad => ad.Id)) .ForMember(a => a.Category, opts => opts.ResolveUsing(ad => ad.Category)) .ForMember(a => a.Description, opts => opts.ResolveUsing(ad => ad.Description)) .ForMember(a => a.LastUpdated, opts => opts.ResolveUsing(ad => ad.LastUpdated)) .ForMember(a => a.Name, opts => opts.ResolveUsing(ad => ad.Name)) .ForMember(a => a.PlanState, opts => opts.ResolveUsing(ad => PlanState.IntToString(ad.PlanState))) .ForMember(a => a.StartingSubPlanId, opts => opts.ResolveUsing(ad => ad.StartingSubPlanId)) .ForMember(a => a.Tag, opts => opts.ResolveUsing(ad => ad.Tag)) .ForMember(a => a.Visibility, opts => opts.ResolveUsing(ad => new PlanVisibilityDTO() { Hidden = ad.Visibility.BooleanValue() })); Mapper.CreateMap <PlanNoChildrenDTO, PlanDO>().ForMember(a => a.Id, opts => opts.ResolveUsing(ad => ad.Id)) .ForMember(a => a.Category, opts => opts.ResolveUsing(ad => ad.Category)) .ForMember(a => a.Description, opts => opts.ResolveUsing(ad => ad.Description)) .ForMember(a => a.LastUpdated, opts => opts.ResolveUsing(ad => ad.LastUpdated)) .ForMember(a => a.Name, opts => opts.ResolveUsing(ad => ad.Name)) .ForMember(a => a.PlanState, opts => opts.ResolveUsing(ad => PlanState.StringToInt(ad.PlanState))) .ForMember(a => a.StartingSubPlanId, opts => opts.ResolveUsing(ad => ad.StartingSubPlanId)) .ForMember(a => a.Tag, opts => opts.ResolveUsing(ad => ad.Tag)) .ForMember(a => a.Visibility, opts => opts.ResolveUsing(ad => ad.Visibility?.PlanVisibilityValue())); Mapper.CreateMap <ActivityNameDTO, ActivityTemplateDO>() .ForMember(activityTemplateDO => activityTemplateDO.Name, opts => opts.ResolveUsing(e => e.Name)) .ForMember(activityTemplateDO => activityTemplateDO.Version, opts => opts.ResolveUsing(e => e.Version)); Mapper.CreateMap <PlanNoChildrenDTO, PlanDO>(); Mapper.CreateMap <PlanDO, PlanNoChildrenDTO>(); Mapper.CreateMap <ActivityDO, ActivityDTO>(); Mapper.CreateMap <ActivityTemplateDTO, ActivityTemplateSummaryDTO>() .ForMember(x => x.Name, opts => opts.MapFrom(src => src.Name)) .ForMember(x => x.Version, opts => opts.MapFrom(src => src.Version)) .ForMember(x => x.TerminalName, opts => opts.MapFrom(src => src.Terminal.Name)) .ForMember(x => x.TerminalVersion, opts => opts.MapFrom(src => src.Terminal.Version)); Mapper.CreateMap <Fr8AccountDO, UserDTO>() .ForMember(dto => dto.EmailAddress, opts => opts.ResolveUsing(e => e.EmailAddress.Address)) .ForMember(dto => dto.Status, opts => opts.ResolveUsing(e => e.State.Value)); }
public PlanResultDTO GetForUser(IUnitOfWork unitOfWork, Fr8AccountDO account, PlanQueryDTO planQueryDTO, bool isAdmin = false) { //lets make sure our inputs are correct planQueryDTO = planQueryDTO ?? new PlanQueryDTO(); planQueryDTO.Page = planQueryDTO.Page ?? 1; planQueryDTO.Page = planQueryDTO.Page < 1 ? 1 : planQueryDTO.Page; planQueryDTO.PlanPerPage = planQueryDTO.PlanPerPage ?? DefaultPlanPageSize; planQueryDTO.PlanPerPage = planQueryDTO.PlanPerPage < MinPlanPageSize ? MinPlanPageSize : planQueryDTO.PlanPerPage; planQueryDTO.IsDescending = planQueryDTO.IsDescending ?? planQueryDTO.OrderBy?.StartsWith("-") ?? true; if (planQueryDTO.OrderBy?.StartsWith("-") == true) { planQueryDTO.OrderBy = planQueryDTO.OrderBy.Substring(1); } var planQuery = unitOfWork.PlanRepository.GetPlanQueryUncached() .Where(x => x.Visibility == PlanVisibility.Standard); if (planQueryDTO.AppsOnly) { planQuery = planQueryDTO.Id == null ? planQuery.Where(pt => pt.IsApp == true) : planQuery.Where(pt => pt.Id == planQueryDTO.Id && pt.IsApp == true); if (account.OrganizationId.HasValue) { // If the current user belongs to some organization, // display all apps for that organization planQuery = planQuery.Where(pt => pt.Fr8Account.OrganizationId == account.OrganizationId); } else { // If user does not belong to an org, just display his/her own apps. planQuery = planQuery.Where(pt => pt.Fr8Account.Id == account.Id); } } else { planQuery = planQueryDTO.Id == null ? planQuery.Where(pt => pt.Fr8Account.Id == account.Id) : planQuery.Where(pt => pt.Id == planQueryDTO.Id && pt.Fr8Account.Id == account.Id); } planQuery = !string.IsNullOrEmpty(planQueryDTO.Category) ? planQuery.Where(c => c.Category == planQueryDTO.Category) : planQuery.Where(c => string.IsNullOrEmpty(c.Category)); if (!string.IsNullOrEmpty(planQueryDTO.Filter)) { planQuery = planQuery.Where(c => c.Name.Contains(planQueryDTO.Filter) || c.Description.Contains(planQueryDTO.Filter)); } if (planQueryDTO.AppsOnly) { planQuery = planQuery.Where(c => c.IsApp == true); } int?planState = null; if (planQueryDTO.Status != null) { planState = PlanState.StringToInt(planQueryDTO.Status); } planQuery = planState == null ? planQuery.Where(pt => pt.PlanState != PlanState.Deleted) : planQuery.Where(pt => pt.PlanState == planState); // Lets allow ordering with just name for now if (planQueryDTO.OrderBy == "name") { planQuery = planQueryDTO.IsDescending.Value ? planQuery.OrderByDescending(p => p.Name) : planQuery.OrderBy(p => p.Name); } else { planQuery = planQueryDTO.IsDescending.Value ? planQuery.OrderByDescending(p => p.LastUpdated) : planQuery.OrderBy(p => p.LastUpdated); } var totalPlanCountForCurrentCriterias = planQuery.Count(); planQuery = planQuery.Skip(planQueryDTO.PlanPerPage.Value * (planQueryDTO.Page.Value - 1)) .Take(planQueryDTO.PlanPerPage.Value); return(new PlanResultDTO { Plans = planQuery.ToList().Select(Mapper.Map <PlanNoChildrenDTO>).ToList(), CurrentPage = planQueryDTO.Page.Value, TotalPlanCount = totalPlanCountForCurrentCriterias }); }
public void ConfigureAutoMapper() { Mapper.CreateMap <PlanDO, PlanNoChildrenDTO>().ForMember(a => a.Id, opts => opts.ResolveUsing(ad => ad.Id)) .ForMember(a => a.Category, opts => opts.ResolveUsing(ad => ad.Category)) .ForMember(a => a.Description, opts => opts.ResolveUsing(ad => ad.Description)) .ForMember(a => a.LastUpdated, opts => opts.ResolveUsing(ad => ad.LastUpdated)) .ForMember(a => a.Name, opts => opts.ResolveUsing(ad => ad.Name)) .ForMember(a => a.PlanState, opts => opts.ResolveUsing(ad => PlanState.IntToString(ad.PlanState))) .ForMember(a => a.StartingSubPlanId, opts => opts.ResolveUsing(ad => ad.StartingSubPlanId)) .ForMember(a => a.Tag, opts => opts.ResolveUsing(ad => ad.Tag)) .ForMember(a => a.Visibility, opts => opts.ResolveUsing(ad => new PlanVisibilityDTO() { Hidden = ad.Visibility.BooleanValue() })); Mapper.CreateMap <PlanNoChildrenDTO, PlanDO>().ForMember(a => a.Id, opts => opts.ResolveUsing(ad => ad.Id)) .ForMember(a => a.Category, opts => opts.ResolveUsing(ad => ad.Category)) .ForMember(a => a.Description, opts => opts.ResolveUsing(ad => ad.Description)) .ForMember(a => a.LastUpdated, opts => opts.ResolveUsing(ad => ad.LastUpdated)) .ForMember(a => a.Name, opts => opts.ResolveUsing(ad => ad.Name)) .ForMember(a => a.PlanState, opts => opts.ResolveUsing(ad => PlanState.StringToInt(ad.PlanState))) .ForMember(a => a.StartingSubPlanId, opts => opts.ResolveUsing(ad => ad.StartingSubPlanId)) .ForMember(a => a.Tag, opts => opts.ResolveUsing(ad => ad.Tag)) .ForMember(a => a.Visibility, opts => opts.ResolveUsing(ad => ad.Visibility?.PlanVisibilityValue())); Mapper.CreateMap <ActivityDO, ActivityDTO>().ForMember(a => a.Id, opts => opts.ResolveUsing(ad => ad.Id)) .ForMember(a => a.RootPlanNodeId, opts => opts.ResolveUsing(ad => ad.RootPlanNodeId)) .ForMember(a => a.ParentPlanNodeId, opts => opts.ResolveUsing(ad => ad.ParentPlanNodeId)) .ForMember(a => a.ChildrenActivities, opts => opts.ResolveUsing(ad => ad.ChildNodes.OfType <ActivityDO>().OrderBy(da => da.Ordering))) .ForMember(a => a.ActivityTemplate, opts => opts.ResolveUsing(GetActivityTemplate)) .ForMember(a => a.AuthToken, opts => opts.ResolveUsing(ad => ad.AuthorizationToken)); Mapper.CreateMap <ActivityDTO, ActivityDO>().ForMember(a => a.Id, opts => opts.ResolveUsing(ad => ad.Id)) .ForMember(a => a.RootPlanNodeId, opts => opts.ResolveUsing(ad => ad.RootPlanNodeId)) .ForMember(a => a.ParentPlanNodeId, opts => opts.ResolveUsing(ad => ad.ParentPlanNodeId)) //.ForMember(a => a.ActivityTemplate, opts => opts.Ignore()) .ForMember(a => a.ActivityTemplateId, opts => opts.ResolveUsing(GetActivityTemplateId)) //.ForMember(a => a.CrateStorage, opts => opts.ResolveUsing(ad => Newtonsoft.Json.JsonConvert.SerializeObject(ad.CrateStorage))) .ForMember(a => a.ChildNodes, opts => opts.ResolveUsing(ad => MapActivities(ad.ChildrenActivities))) .ForMember(a => a.AuthorizationTokenId, opts => opts.ResolveUsing(ad => ad.AuthToken != null && ad.AuthToken.Id != null ? new Guid(ad.AuthToken.Id) : (Guid?)null)); Mapper.CreateMap <ActivityTemplateDO, ActivityTemplateSummaryDTO>() .ForMember(x => x.Name, opts => opts.ResolveUsing(x => x.Name)) .ForMember(x => x.Version, opts => opts.ResolveUsing(x => x.Version)) .ForMember(x => x.TerminalName, opts => opts.ResolveUsing(x => x.Terminal.Name)) .ForMember(x => x.TerminalVersion, opts => opts.ResolveUsing(x => x.Terminal.Version)); Mapper.CreateMap <ActivityTemplateDO, ActivityTemplateDTO>() .ForMember(x => x.Id, opts => opts.ResolveUsing(x => x.Id)) .ForMember(x => x.Name, opts => opts.ResolveUsing(x => x.Name)) .ForMember(x => x.Version, opts => opts.ResolveUsing(x => x.Version)) .ForMember(x => x.Terminal, opts => opts.ResolveUsing(GetTerminal)) .ForMember(x => x.NeedsAuthentication, opts => opts.ResolveUsing(x => x.NeedsAuthentication)); Mapper.CreateMap <Fr8AccountDO, ManageUserVM>() .ForMember(mu => mu.HasLocalPassword, opts => opts.ResolveUsing(account => !string.IsNullOrEmpty(account.PasswordHash))) .ForMember(mu => mu.HasGoogleToken, opts => opts.Ignore()) .ForMember(mu => mu.GoogleSpreadsheets, opts => opts.Ignore()); Mapper.CreateMap <UserVM, EmailAddressDO>() .ForMember(userDO => userDO.Address, opts => opts.ResolveUsing(e => e.EmailAddress)); Mapper.CreateMap <UserVM, Fr8AccountDO>() .ForMember(userDO => userDO.Id, opts => opts.ResolveUsing(e => e.Id)) .ForMember(userDO => userDO.FirstName, opts => opts.ResolveUsing(e => e.FirstName)) .ForMember(userDO => userDO.LastName, opts => opts.ResolveUsing(e => e.LastName)) .ForMember(userDO => userDO.UserName, opts => opts.ResolveUsing(e => e.UserName)) .ForMember(userDO => userDO.EmailAddress, opts => opts.ResolveUsing(e => new EmailAddressDO { Address = e.EmailAddress })) .ForMember(userDO => userDO.Roles, opts => opts.Ignore()); Mapper.CreateMap <PageDefinitionDTO, PageDefinitionDO>() .ForMember(dest => dest.Url, opts => opts.Ignore()) .ForMember(dest => dest.Tags, opts => opts.MapFrom( x => x.Tags.Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries).ToList())); Mapper.CreateMap <PageDefinitionDO, PageDefinitionDTO>() .ForMember(dest => dest.Tags, opts => opts.MapFrom(x => string.Join(", ", x.Tags))); }