//[OutputCache(Duration = 60 * 60 * 24 * 30, Location = OutputCacheLocation.Downstream)] public ActionResult Image(int assetId, string fileName, string extension, int?cropSizeId) { var qs = Request.Url.Query; var settings = ImageResizeSettings.ParseFromQueryString(qs); var asset = _queryExecutor.GetById <ImageAssetRenderDetails>(assetId); if (asset == null) { return(FileAssetNotFound("Image could not be found")); } if (SlugFormatter.ToSlug(asset.FileName) != fileName) { var url = _imageAssetRouteLibrary.ImageAsset(asset, settings); return(RedirectPermanent(url)); } DateTime lastModified = DateTime.SpecifyKind(asset.UpdateDate, DateTimeKind.Utc); // Round the ticks down (see http://stackoverflow.com/a/1005222/486434), because http headers are only accurate to seconds, so get rounded down lastModified = lastModified.AddTicks(-(lastModified.Ticks % TimeSpan.TicksPerSecond)); if (!string.IsNullOrEmpty(Request.Headers["If-Modified-Since"])) { DateTime ifModifiedSince; if (DateTime.TryParse(Request.Headers["If-Modified-Since"], out ifModifiedSince) && lastModified <= ifModifiedSince.ToUniversalTime()) { return(new HttpStatusCodeResult(304, "Not Modified")); } } Stream stream = null; try { stream = _resizedImageAssetFileService.Get(asset, settings); } catch (FileNotFoundException ex) { // If the file exists but the file has gone missing, log and return a 404 _errorLoggingService.Log(ex); return(FileAssetNotFound("File not found")); } // Expire the image, so browsers always check with the server, but also send a last modified date so we can check for If-Modified-Since on the next request and return a 304 Not Modified. Response.Cache.SetExpires(DateTime.UtcNow.AddMonths(-1)); Response.Cache.SetLastModified(lastModified); var contentType = MimeMapping.GetMimeMapping(asset.Extension); return(new FileStreamResult(stream, contentType)); }
public async Task ExecuteAsync(UpdateCustomEntityUrlCommand command, IExecutionContext executionContext) { var entity = await _dbContext .CustomEntities .Include(c => c.CustomEntityVersions) .Where(e => e.CustomEntityId == command.CustomEntityId) .SingleOrDefaultAsync(); EntityNotFoundException.ThrowIfNull(entity, command.CustomEntityId); _permissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdateUrlPermission>(entity.CustomEntityDefinitionCode); var definition = _queryExecutor.GetById <CustomEntityDefinitionSummary>(entity.CustomEntityDefinitionCode); EntityNotFoundException.ThrowIfNull(definition, entity.CustomEntityDefinitionCode); await ValidateIsUnique(command, definition); Map(command, entity, definition); var isPublished = entity.CustomEntityVersions.Any(v => v.WorkFlowStatusId == (int)WorkFlowStatus.Published); await _dbContext.SaveChangesAsync(); _customEntityCache.Clear(entity.CustomEntityDefinitionCode, command.CustomEntityId); await _messageAggregator.PublishAsync(new CustomEntityUrlChangedMessage() { CustomEntityId = command.CustomEntityId, CustomEntityDefinitionCode = entity.CustomEntityDefinitionCode, HasPublishedVersionChanged = isPublished }); }
private CustomEntityDefinitionSummary GetDefinition(IsCustomEntityPathUniqueQuery query) { var definition = _queryExecutor.GetById <CustomEntityDefinitionSummary>(query.CustomEntityDefinitionCode); EntityNotFoundException.ThrowIfNull(definition, query.CustomEntityDefinitionCode); return(definition); }
/// <summary> /// Creates a data model type from the database id. Throws /// an InvalidOperationException if the requested type is not register /// or has been defined multiple times /// </summary> /// <param name="pageModuleTypeId">Id of the page module type in the database</param> public Type CreateByPageModuleTypeId(int pageModuleTypeId) { var moduleType = _queryExecutor.GetById <PageModuleTypeSummary>(pageModuleTypeId); EntityNotFoundException.ThrowIfNull(moduleType, pageModuleTypeId); return(CreateByPageModuleTypeFileName(moduleType.FileName)); }
private void MapDataModel(GetByIdQuery <CustomEntityDetails> query, CustomEntityVersion dbVersion, CustomEntityVersionDetails version) { var definition = _queryExecutor.GetById <CustomEntityDefinitionSummary>(dbVersion.CustomEntity.CustomEntityDefinitionCode); EntityNotFoundException.ThrowIfNull(definition, dbVersion.CustomEntity.CustomEntityDefinitionCode); version.Model = (ICustomEntityVersionDataModel)_dbUnstructuredDataSerializer.Deserialize(dbVersion.SerializedData, definition.DataModelType); }
private UserMicroSummary InitUser() { if (!Context.UserId.HasValue) { return(null); } return(_queryExecutor.GetById <UserMicroSummary>(Context.UserId.Value)); }
/// <summary> /// Simple but less efficient way of getting a page url if you only know /// the id. Use the overload accepting an IPageRoute if possible to save a /// potential db query if the route isn't cached. /// </summary> public string Page(int?pageId) { if (!pageId.HasValue) { return(string.Empty); } var route = _queryExecutor.GetById <PageRoute>(pageId.Value); return(Page(route)); }
private IQueryable <CustomEntityVersion> GetQuery(SearchCustomEntityRenderSummariesQuery query) { var definition = _queryExecutor.GetById <CustomEntityDefinitionSummary>(query.CustomEntityDefinitionCode); EntityNotFoundException.ThrowIfNull(definition, query.CustomEntityDefinitionCode); var dbQuery = _dbContext .CustomEntityVersions .AsNoTracking() .Where(e => e.CustomEntity.CustomEntityDefinitionCode == query.CustomEntityDefinitionCode) .Where(v => v.WorkFlowStatusId == (int)Domain.WorkFlowStatus.Draft || v.WorkFlowStatusId == (int)Domain.WorkFlowStatus.Published) .GroupBy(e => e.CustomEntityId, (key, g) => g.OrderByDescending(v => v.WorkFlowStatusId == (int)Domain.WorkFlowStatus.Draft).FirstOrDefault()) .Include(e => e.CustomEntity); // Filter by locale if (query.LocaleId > 0) { dbQuery = dbQuery.Where(p => p.CustomEntity.LocaleId == query.LocaleId); } else { dbQuery = dbQuery.Where(p => !p.CustomEntity.LocaleId.HasValue); } switch (query.SortBy) { case CustomEntityQuerySortType.Default: case CustomEntityQuerySortType.Natural: if (definition.Ordering != CustomEntityOrdering.None) { dbQuery = dbQuery .OrderByWithSortDirection(e => !e.CustomEntity.Ordering.HasValue, query.SortDirection) .ThenByWithSortDirection(e => e.CustomEntity.Ordering, query.SortDirection) .ThenByDescendingWithSortDirection(e => e.CreateDate, query.SortDirection); } else { dbQuery = dbQuery .OrderByDescendingWithSortDirection(e => e.CreateDate, query.SortDirection); } break; case CustomEntityQuerySortType.Title: dbQuery = dbQuery .OrderByWithSortDirection(e => e.Title, query.SortDirection); break; case CustomEntityQuerySortType.CreateDate: dbQuery = dbQuery .OrderByDescendingWithSortDirection(e => e.CreateDate, query.SortDirection); break; } return(dbQuery); }
public UserMicroSummary Execute(GetCurrentUserMicroSummaryQuery query, IExecutionContext executionContext) { if (!executionContext.UserContext.UserId.HasValue) { return(null); } var user = _queryExecutor.GetById <UserMicroSummary>(executionContext.UserContext.UserId.Value); return(user); }
/// <summary> /// Simple but less efficient way of getting an image url if you only know /// the id. Use the overload accepting an IImageAssetRenderable if possible to save a /// potential db query if the route isn't cached. /// </summary> /// <param name="imageAssetId">Id of the image asset to get the url for</param> /// <param name="settings">Optional resizing settings for the image</param> public string ImageAsset(int?imageAssetId, IImageResizeSettings settings = null) { if (!imageAssetId.HasValue) { return(string.Empty); } var asset = _queryExecutor.GetById <ImageAssetRenderDetails>(imageAssetId.Value); return(ImageAsset(asset, settings)); }
/// <summary> /// Simple but less efficient way of getting a document url if you only know /// the id. Use the overload accepting an IDocumentAssetRenderable if possible to save a /// potential db query if the asset isn't cached. /// </summary> /// <param name="documentAssetId">Id of the document asset to get the url for</param> public string DocumentAsset(int?documentAssetId) { if (!documentAssetId.HasValue) { return(string.Empty); } var asset = _queryExecutor.GetById <DocumentAssetRenderDetails>(documentAssetId.Value); return(DocumentAsset(asset)); }
private Stream GetFileStream(int imageAssetId) { var result = _queryExecutor.GetById <ImageAssetFile>(imageAssetId); if (result == null || result.ContentStream == null) { throw new FileNotFoundException(imageAssetId.ToString()); } return(result.ContentStream); }
public void SetCache(IEditablePageViewModel vm, PageActionRoutingState state) { var siteViewerMode = state.VisualEditorMode; var workFlowStatusQuery = state.VisualEditorMode.ToWorkFlowStatusQuery(); var pageVersions = state.PageRoutingInfo.PageRoute.Versions; // Force a viewer mode if (siteViewerMode == VisualEditorMode.Any) { var version = state.PageRoutingInfo.GetVersionRoute( state.InputParameters.IsEditingCustomEntity, state.VisualEditorMode.ToWorkFlowStatusQuery(), state.InputParameters.VersionId); switch (version.WorkFlowStatus) { case WorkFlowStatus.Draft: siteViewerMode = VisualEditorMode.Draft; break; case WorkFlowStatus.Published: siteViewerMode = VisualEditorMode.Live; break; default: throw new ApplicationException("WorkFlowStatus." + version.WorkFlowStatus + " is not valid for VisualEditorMode.Any"); } } var pageResponseData = new PageResponseData(); pageResponseData.Page = vm; pageResponseData.VisualEditorMode = siteViewerMode; pageResponseData.PageRoutingInfo = state.PageRoutingInfo; pageResponseData.HasDraftVersion = state.PageRoutingInfo.GetVersionRoute(state.InputParameters.IsEditingCustomEntity, WorkFlowStatusQuery.Draft, null) != null; pageResponseData.Version = state.PageRoutingInfo.GetVersionRoute(state.InputParameters.IsEditingCustomEntity, workFlowStatusQuery, state.InputParameters.VersionId); pageResponseData.IsCustomEntityRoute = pageResponseData.Version is CustomEntityVersionRoute; if (!string.IsNullOrEmpty(state.PageRoutingInfo.PageRoute.CustomEntityDefinitionCode)) { pageResponseData.CustomEntityDefinition = _queryExecutor.GetById <CustomEntityDefinitionSummary>(state.PageRoutingInfo.PageRoute.CustomEntityDefinitionCode); } if (state.InputParameters.IsEditingCustomEntity) { pageResponseData.PageVersion = pageVersions.GetVersionRouting(WorkFlowStatusQuery.Latest); } else { pageResponseData.PageVersion = pageVersions.GetVersionRouting(workFlowStatusQuery, state.InputParameters.VersionId); } _pageRenderDataCache.Set(pageResponseData); }
public async Task ExecuteAsync(AddCustomEntityCommand command, IExecutionContext executionContext) { var definition = _queryExecutor.GetById <CustomEntityDefinitionSummary>(command.CustomEntityDefinitionCode); EntityNotFoundException.ThrowIfNull(definition, command.CustomEntityDefinitionCode); await _commandExecutor.ExecuteAsync(new EnsureCustomEntityDefinitionExistsCommand(definition.CustomEntityDefinitionCode)); // Custom Validation ValidateCommand(command, definition); await ValidateIsUniqueAsync(command, definition); var entity = MapEntity(command, definition, executionContext); _dbContext.CustomEntities.Add(entity); using (var scope = _transactionScopeFactory.Create()) { await _dbContext.SaveChangesAsync(); var dependencyCommand = new UpdateUnstructuredDataDependenciesCommand( CustomEntityVersionEntityDefinition.DefinitionCode, entity.CustomEntityVersions.First().CustomEntityVersionId, command.Model); await _commandExecutor.ExecuteAsync(dependencyCommand); scope.Complete(); } _customEntityCache.ClearRoutes(definition.CustomEntityDefinitionCode); // Set Ouput command.OutputCustomEntityId = entity.CustomEntityId; await _messageAggregator.PublishAsync(new CustomEntityAddedMessage() { CustomEntityId = entity.CustomEntityId, CustomEntityDefinitionCode = definition.CustomEntityDefinitionCode, HasPublishedVersionChanged = command.Publish }); }
private List <PageRoute> Map( List <PageQueryResult> dbPages, List <PageVersionQueryResult> dbPageVersions, Dictionary <int, WebDirectoryRoute> webDirectories, Dictionary <int, PageTemplateQueryResult> templates, IExecutionContext executionContext) { var routes = new List <PageRoute>(); foreach (var dbPage in dbPages) { var pageRoute = dbPage.RoutingInfo; // Web directory will be null if it is inactive or has an inactive parent. pageRoute.WebDirectory = webDirectories.GetOrDefault(dbPage.WebDirectoryId); if (pageRoute.WebDirectory == null) { continue; } // Configure Version Info SetPageVersions(pageRoute, dbPageVersions, templates); if (!pageRoute.Versions.Any()) { continue; } // Configure Locale string directoryPath = null; if (dbPage.LocaleId.HasValue) { pageRoute.Locale = _queryExecutor.GetById <ActiveLocale>(dbPage.LocaleId.Value, executionContext); EntityNotFoundException.ThrowIfNull(pageRoute.Locale, dbPage.LocaleId); directoryPath = pageRoute .WebDirectory .LocaleVariations .Where(v => v.LocaleId == pageRoute.Locale.LocaleId) .Select(v => v.FullUrlPath) .FirstOrDefault(); } if (directoryPath == null) { directoryPath = pageRoute.WebDirectory.FullUrlPath; } // Set Full Path pageRoute.FullPath = CreateFullPath(directoryPath, pageRoute.UrlPath, pageRoute.Locale); routes.Add(pageRoute); } return(routes); }
public async Task <PagedQueryResult <CustomEntitySummary> > ExecuteAsync(SearchCustomEntitySummariesQuery query, IExecutionContext executionContext) { var definition = _queryExecutor.GetById <CustomEntityDefinitionSummary>(query.CustomEntityDefinitionCode); EntityNotFoundException.ThrowIfNull(definition, query.CustomEntityDefinitionCode); // Get Query var dbQuery = GetQuery(query, definition); // Execute Query var dbPagedResult = dbQuery.ToPagedResult(query); var routingsQuery = new GetPageRoutingInfoByCustomEntityIdRangeQuery(dbPagedResult.Items.Select(e => e.CustomEntityId)); var routings = await _queryExecutor.ExecuteAsync(routingsQuery); var allLocales = (await _queryExecutor.GetAllAsync <ActiveLocale>()).ToDictionary(l => l.LocaleId); // Map Items var entities = new List <CustomEntitySummary>(dbPagedResult.Items.Length); foreach (var dbVersion in dbPagedResult.Items) { PageRoutingInfo detailsRouting = null; if (routings.ContainsKey(dbVersion.CustomEntityId)) { detailsRouting = routings[dbVersion.CustomEntityId].FirstOrDefault(r => r.CustomEntityRouteRule != null); } var entity = Mapper.Map <CustomEntitySummary>(dbVersion); if (dbVersion.LocaleId.HasValue) { entity.Locale = allLocales.GetOrDefault(dbVersion.LocaleId.Value); EntityNotFoundException.ThrowIfNull(entity.Locale, dbVersion.LocaleId.Value); } if (detailsRouting != null) { entity.FullPath = detailsRouting.CustomEntityRouteRule.MakeUrl(detailsRouting.PageRoute, detailsRouting.CustomEntityRoute); } entity.Model = (ICustomEntityVersionDataModel)_dbUnstructuredDataSerializer.Deserialize(dbVersion.SerializedData, definition.DataModelType); entity.AuditData.UpdateDate = dbVersion.VersionAuditData.CreateDate; entity.AuditData.Updater = dbVersion.VersionAuditData.Creator; entities.Add(entity); } return(dbPagedResult.ChangeType(entities)); }
private PageVersionModuleRenderDetails Map(PageVersionModule pageVersionModule, string moduleTypeFileName, WorkFlowStatusQuery workFlowStatus) { var moduleType = _queryExecutor.GetById <PageModuleTypeSummary>(pageVersionModule.PageModuleTypeId); EntityNotFoundException.ThrowIfNull(moduleType, pageVersionModule.PageModuleTypeId); var result = new PageVersionModuleRenderDetails(); result.PageVersionModuleId = pageVersionModule.PageVersionModuleId; result.ModuleType = moduleType; result.DisplayModel = _pageVersionModuleModelMapper.MapDisplayModel(moduleTypeFileName, pageVersionModule, workFlowStatus); return(result); }
private CustomEntityVersionPageModuleRenderDetails Map(CustomEntityVersionPageModule versionModule, string moduleTypeFileName, string customEntityDefinitionCode, WorkFlowStatusQuery workflowStatus) { _permissionValidationService.EnforceCustomEntityPermission <CustomEntityReadPermission>(customEntityDefinitionCode); var moduleType = _queryExecutor.GetById <PageModuleTypeSummary>(versionModule.PageModuleTypeId); EntityNotFoundException.ThrowIfNull(moduleType, versionModule.PageModuleTypeId); var result = new CustomEntityVersionPageModuleRenderDetails(); result.CustomEntityVersionPageModuleId = versionModule.CustomEntityVersionPageModuleId; result.ModuleType = moduleType; result.DisplayModel = _pageVersionModuleModelMapper.MapDisplayModel(moduleTypeFileName, versionModule, workflowStatus); return(result); }
public CustomEntityRenderSummary MapSummary( CustomEntityVersion dbResult, IExecutionContext executionContext ) { var routingQuery = GetPageRoutingQuery(dbResult); var routing = _queryExecutor.Execute(routingQuery, executionContext); ActiveLocale locale = null; if (dbResult.CustomEntity.LocaleId.HasValue) { locale = _queryExecutor.GetById <ActiveLocale>(dbResult.CustomEntity.LocaleId.Value, executionContext); } return(MapSingle(dbResult, routing, locale)); }
public PageRenderDetails Execute(GetPageRenderDetailsByIdQuery query, IExecutionContext executionContext) { var dbPage = QueryPage(query).FirstOrDefault(); if (dbPage == null) { return(null); } var page = Mapper.Map <PageRenderDetails>(dbPage); page.PageRoute = _queryExecutor.GetById <PageRoute>(page.PageId, executionContext); var dbModules = QueryModules(page).ToList(); var allModuleTypes = _queryExecutor.GetAll <PageModuleTypeSummary>(executionContext); _entityVersionPageModuleMapper.MapSections(dbModules, page.Sections, allModuleTypes, query.WorkFlowStatus); return(page); }
public ImageAssetFile Execute(GetByIdQuery <ImageAssetFile> query, IExecutionContext executionContext) { var dbResult = _queryExecutor.GetById <ImageAssetRenderDetails>(query.Id); if (dbResult == null) { return(null); } var fileName = Path.ChangeExtension(query.Id.ToString(), dbResult.Extension); var result = Mapper.Map <ImageAssetFile>(dbResult); result.ContentStream = _fileStoreService.Get(ImageAssetConstants.FileContainerName, fileName); if (result.ContentStream == null) { throw new FileNotFoundException("Image asset file could not be found", fileName); } return(result); }
public async Task <PageTemplateDetails> ExecuteAsync(GetByIdQuery <PageTemplateDetails> query, IExecutionContext executionContext) { var template = await _dbContext .PageTemplates .AsNoTracking() .Where(l => l.PageTemplateId == query.Id) .ProjectTo <PageTemplateDetails>() .SingleOrDefaultAsync(); if (template == null) { return(null); } // Re-map to code defined version if (template.CustomEntityDefinition != null) { template.CustomEntityDefinition = _queryExecutor.GetById <CustomEntityDefinitionMicroSummary>(template.CustomEntityDefinition.CustomEntityDefinitionCode); } return(template); }
public async Task <UpdateCustomEntityDraftVersionCommand> ExecuteAsync(GetByIdQuery <UpdateCustomEntityDraftVersionCommand> query, IExecutionContext executionContext) { var dbResult = await _dbContext .CustomEntityVersions .Include(v => v.CustomEntity) .AsNoTracking() .Where(v => v.CustomEntityId == query.Id && v.WorkFlowStatusId == (int)WorkFlowStatus.Draft) .SingleOrDefaultAsync(); if (dbResult == null) { return(null); } _permissionValidationService.EnforceCustomEntityPermission <CustomEntityReadPermission>(dbResult.CustomEntity.CustomEntityDefinitionCode); var command = Mapper.Map <UpdateCustomEntityDraftVersionCommand>(dbResult); var definition = _queryExecutor.GetById <CustomEntityDefinitionSummary>(command.CustomEntityDefinitionCode); EntityNotFoundException.ThrowIfNull(definition, command.CustomEntityDefinitionCode); command.Model = (ICustomEntityVersionDataModel)_dbUnstructuredDataSerializer.Deserialize(dbResult.SerializedData, definition.DataModelType); return(command); }
private PageDetails Map( GetByIdQuery <PageDetails> query, PageVersion dbPageVersion, IEnumerable <PageSectionDetails> sections ) { if (dbPageVersion == null) { return(null); } var page = Mapper.Map <PageDetails>(dbPageVersion.Page); Mapper.Map(dbPageVersion, page); // Custom Mapping page.PageRoute = _queryExecutor.GetById <PageRoute>(query.Id); EntityNotFoundException.ThrowIfNull(page, query.Id); page.LatestVersion.Sections = sections; return(page); }
/// <summary> /// Returns a web directory with the specified id as a WebDirectoryRoute instance. /// </summary> /// <param name="webDirectoryId">WebDirectoryId of the directory to get.</param> /// <param name="executionContext">Optional execution context to use when executing the query. Useful if you need to temporarily elevate your permission level.</param> public WebDirectoryRoute GetWebDirectoryRouteById(int webDirectoryId, IExecutionContext executionContext = null) { return(_queryExecutor.GetById <WebDirectoryRoute>(webDirectoryId, executionContext)); }
public DocumentAssetFile GetDocumentAssetFileByIdQuery(int id, IExecutionContext executionContext = null) { return(_queryExecutor.GetById <DocumentAssetFile>(id, executionContext)); }
public PageDetails GetPageDetailsById(int id, IExecutionContext executionContext = null) { return(_queryExecutor.GetById <PageDetails>(id, executionContext)); }
public PageModuleTypeSummary GetPageModuleTypeSummaryById(int id, IExecutionContext executionContext = null) { return(_queryExecutor.GetById <PageModuleTypeSummary>(id, executionContext)); }
public CustomEntityDefinitionMicroSummary GetCustomEntityDefinitionMicroSummaryById(string customEntityDefinitionCode, IExecutionContext executionContext = null) { return(_queryExecutor.GetById <CustomEntityDefinitionMicroSummary>(customEntityDefinitionCode, executionContext)); }
/// <summary> /// Finds a user by a database id returning a UserDetails object if it /// is found, otherwise null. /// </summary> /// <param name="executionContext">Optional execution context to use when executing the query. Useful if you need to temporarily elevate your permission level.</param> public UserMicroSummary GetUserMicroSummaryById(int userId, IExecutionContext executionContext = null) { return(_queryExecutor.GetById <UserMicroSummary>(userId, executionContext)); }