From 76f0eac5f8927397bd284cdb8923f355978bd93c Mon Sep 17 00:00:00 2001 From: Jeavon Leopold Date: Sun, 8 Jun 2014 22:39:01 +0100 Subject: [PATCH] Template Query WIP --- .../TemplateQuery/IQueryResultModel.cs | 2 +- .../Editors/TemplateQuery/IResult.cs | 9 +++- .../Editors/TemplateQuery/QueryResultModel.cs | 6 ++- .../TemplateQuery/TemplateQueryController.cs | 51 ++++++++++++++++++- 4 files changed, 62 insertions(+), 6 deletions(-) diff --git a/src/Umbraco.Web/Editors/TemplateQuery/IQueryResultModel.cs b/src/Umbraco.Web/Editors/TemplateQuery/IQueryResultModel.cs index 25239cdf73..182a81b9ef 100644 --- a/src/Umbraco.Web/Editors/TemplateQuery/IQueryResultModel.cs +++ b/src/Umbraco.Web/Editors/TemplateQuery/IQueryResultModel.cs @@ -6,7 +6,7 @@ namespace Umbraco.Web.Editors { string QueryExpression { get; set; } - IEnumerable SampleResults { get; set; } + IEnumerable SampleResults { get; set; } int ResultCount { get; set; } diff --git a/src/Umbraco.Web/Editors/TemplateQuery/IResult.cs b/src/Umbraco.Web/Editors/TemplateQuery/IResult.cs index c96377493c..14d2b13735 100644 --- a/src/Umbraco.Web/Editors/TemplateQuery/IResult.cs +++ b/src/Umbraco.Web/Editors/TemplateQuery/IResult.cs @@ -1,9 +1,16 @@ namespace Umbraco.Web.Editors { - public interface IResult + public interface ITemplateQueryResult { string Icon { get; set; } string Name { get; set; } } + + public class TemplateQueryResult : ITemplateQueryResult + { + public string Icon { get; set; } + + public string Name { get; set; } + } } \ No newline at end of file diff --git a/src/Umbraco.Web/Editors/TemplateQuery/QueryResultModel.cs b/src/Umbraco.Web/Editors/TemplateQuery/QueryResultModel.cs index 0fdfcf2a16..f1b9321a48 100644 --- a/src/Umbraco.Web/Editors/TemplateQuery/QueryResultModel.cs +++ b/src/Umbraco.Web/Editors/TemplateQuery/QueryResultModel.cs @@ -2,20 +2,22 @@ namespace Umbraco.Web.Editors { + using System.Web.UI; + public class QueryResultModel : IQueryResultModel { public QueryResultModel() { Initialize(); } - + private void Initialize() { QueryExpression = "CurrentPage.Site()"; } public string QueryExpression { get; set; } - public IEnumerable SampleResults { get; set; } + public IEnumerable SampleResults { get; set; } public int ResultCount { get; set; } public double ExecutionTime { get; set; } public int Take { get; set; } diff --git a/src/Umbraco.Web/Editors/TemplateQuery/TemplateQueryController.cs b/src/Umbraco.Web/Editors/TemplateQuery/TemplateQueryController.cs index 654695cb54..01baa92281 100644 --- a/src/Umbraco.Web/Editors/TemplateQuery/TemplateQueryController.cs +++ b/src/Umbraco.Web/Editors/TemplateQuery/TemplateQueryController.cs @@ -7,6 +7,10 @@ using Umbraco.Web.WebApi.Filters; namespace Umbraco.Web.Editors { + using System.Diagnostics; + using System.Threading; + using System.Web.Services.Description; + /// /// The API controller used for building content queries within the template /// @@ -59,14 +63,24 @@ namespace Umbraco.Web.Editors var sb = new StringBuilder(); sb.Append(queryResult.QueryExpression); + var timer = new Stopwatch(); + + timer.Start(); + var currentPage = umbraco.TypedContentAtRoot().FirstOrDefault(); + + timer.Stop(); // adjust the "FROM" if (model != null && model.Id > 0) { var fromTypeAlias = umbraco.TypedContent(model.Id).DocumentTypeAlias; + timer.Start(); + currentPage = currentPage.DescendantOrSelf(fromTypeAlias); + + timer.Stop(); sb.AppendFormat(".DescendantOrSelf(\"{0}\")", fromTypeAlias); } @@ -75,17 +89,31 @@ namespace Umbraco.Web.Editors IEnumerable contents; if (model != null && string.IsNullOrEmpty(model.ContentTypeAlias) == false) { + timer.Start(); + contents = currentPage.Descendants(model.ContentTypeAlias); + timer.Stop(); sb.AppendFormat(".Decendants(\"{0}\")", model.ContentTypeAlias); } + else + { + timer.Start(); + contents = currentPage.Descendants(); + timer.Stop(); + sb.Append(".Decendants()"); + } + + var clause = string.Empty; // WHERE foreach (var condition in model.Wheres) { if(string.IsNullOrEmpty( condition.ConstraintValue)) continue; + + var operation = string.Empty; - var operation = ""; + switch (condition.Term.Operathor) { case Operathor.Equals: @@ -123,10 +151,29 @@ namespace Umbraco.Web.Editors break; } - sb.AppendFormat(".Where(\"{0}\")", operation); + clause = string.IsNullOrEmpty(clause) ? operation : string.Concat(new[] { clause, " && ", operation }); + + } + + if(string.IsNullOrEmpty(clause) == false) + { + timer.Start(); + + contents = contents.Where(clause); + + timer.Stop(); + + sb.AppendFormat(".Where(\"{0}\")", clause); } queryResult.QueryExpression = sb.ToString(); + queryResult.ExecutionTime = timer.ElapsedMilliseconds; + queryResult.ResultCount = contents.Count(); + queryResult.SampleResults = contents.Take(20).Select(x => new TemplateQueryResult() + { + Icon = "icon-file", + Name = x.Name + }); return queryResult; }