Ejemplo n.º 1
0
        /// <summary>
        /// Get file content
        /// </summary>
        public string GetFileContent()
        {
            var propertyStrings = new List <string>();

            if (FormField.Property.FilterExpression != null)
            {
                var expressionPartial = new CsExpressionPartial(FormField.Property, FormField.Property.FilterExpression);
                var properties        = expressionPartial.GetFilterProperties();

                foreach (var property in properties)
                {
                    propertyStrings.Add(ModelReferenceItemPropertyTemplate.Evaluate(property));
                }
            }

            return($@"using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using MongoDB.Bson;
using Oven.Shared;

namespace {Project.InternalName}.Models.{Screen.InternalName}.Reference
{{
    /// <summary>
    /// {FormField.TitleValue} Reference Request
    /// </summary>
    public class {FormField.ReferenceRequestClass}
    {{
        /// <summary>
        /// Page
        /// </summary>
        [Required]
        [DefaultValue(1)]
        public int Page {{ get; set; }}
        /// <summary>
        /// Page Size
        /// </summary>
        [Required]
        [DefaultValue(10)]
        public int PageSize {{ get; set; }}
        /// <summary>
        /// Query
        /// </summary>
        public string Query {{ get; set; }}{(propertyStrings.Any() ? Environment.NewLine : string.Empty)}{string.Join(Environment.NewLine, propertyStrings)}
    }}
}}");
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Evaluate
        /// </summary>
        internal static string Evaluate(Project project, Screen screen, FormField formField)
        {
            var entity = project.Entities.SingleOrDefault(e => e.Id == formField.Property.ReferenceEntityId);

            var itemClassName     = $"Models.{screen.InternalName}.Reference.{formField.ReferenceItemClass}";
            var responseClassName = $"Models.{screen.InternalName}.Reference.{formField.ReferenceResponseClass}";
            var requestClassName  = $"Models.{screen.InternalName}.Reference.{formField.ReferenceRequestClass}";

            // TODO: this url needs to be formField specific

            var whereString = $@"where request.Query == null ||
                        request.Query == """" ||
                        item.Title.Contains(request.Query)";

            if (formField.Property.FilterExpression != null)
            {
                var expressionPartial = new CsExpressionPartial(formField.Property, formField.Property.FilterExpression);
                var expression        = expressionPartial.GetExpression("item", "request");

                whereString = $@"where ({expression}) &&
                        (request.Query == null ||
                        request.Query == """" ||
                        item.Title.Contains(request.Query))";
            }

            return($@"
        /// <summary>
        /// {formField.TitleValue} Reference Search
        /// </summary>
        [HttpPost(""{formField.Property.InternalName}References"")]
        [ProducesResponseType(typeof({responseClassName}), 200)]
        [ProducesResponseType(typeof(Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary), 400)]
        public async Task<IActionResult> {formField.Property.InternalName}Reference([FromServices] I{screen.Entity.InternalName}Service {screen.Entity.InternalName.Camelize()}Service, [FromBody]{requestClassName} request)
        {{
            if (request == null)
            {{
                return BadRequest();
            }}
            
            if (!ModelState.IsValid)
            {{
                return new BadRequestObjectResult(ModelState);
            }}

            var query = from item in _context.{entity.InternalNamePlural}
                        {whereString}
                        select new
                        {{
                            item.Id,
                            item.Title
                        }};            

            var totalItems = query.Count();
            int totalPages = 0;
            var items = new {itemClassName}[0];

            if (totalItems != 0 && request.PageSize != 0)
            {{
                totalPages = Convert.ToInt32(Math.Ceiling((double)totalItems / request.PageSize));

                var dbItems = await query
                    .OrderBy(p => p.Title) //TODO: From Setting
                    .Skip((request.Page - 1) * request.PageSize)
                    .Take(request.PageSize)
                    .ToArrayAsync();

                // had to do the select outside EF as it was having issues with the AnonymousType
                items = dbItems
                    .Select(item => new {itemClassName}
                    {{
                        Id = item.Id,
                        Title = item.Title
                    }})
                    .ToArray();
            }}
            
            var result = new {responseClassName}
            {{
                TotalItems = totalItems,
                TotalPages = totalPages,
                Items = items
            }};
            return Ok(result);
        }}");
        }