public void ToExpando()
        {
            // Type
            var @this = new Dictionary<string, object> {{"Fizz", "Buzz"}};

            // Exemples
            dynamic result = @this.ToExpando(); // return an expando object;

            // Unit Test
            Assert.AreEqual("Buzz", result.Fizz);
        }
Example #2
0
 public void Initialise(Dictionary<string, object> parameters)
 {
     this.parameters = parameters.ToExpando();
 }
        public IHttpActionResult Get()
        {
            string key, name;
            Guid guid;

            try
            {
                key = this.Request.Headers.GetValues("X-Secret-Key").FirstOrDefault();
                name = this.Request.Headers.GetValues("X-Table-Name").FirstOrDefault();
                guid = new Guid(key);
            }
            catch (FormatException ex)
            {
                return this.BadRequest("Ivalid Secret Key format!");
            }
            catch (Exception ex)
            {
                return this.BadRequest("Not all headers provided!");
            }

            var app = this.apps.All().Where(x => x.UniqueKey == guid);
            var table = this.tables.All().FirstOrDefault(x => x.App.UniqueKey == guid && x.Name.ToLower() == name.ToLower());

            if (table == null || app == null)
            {
                return this.BadRequest("Invalid table name / secret key!");
            }

            foreach (var item in app)
            {
                item.RequestsCount++;
            }

            this.apps.Save();

            var respone = new List<ExpandoObject>();
            var count = 0;

            if (table.Fields.Any())
            {
                var members = table.Fields.FirstOrDefault(x => x.Name == "Id").Members;
                count = members.Count;

                for (int i = 0; i < count; i++)
                {
                    var row = new Dictionary<string, object>();
                    foreach (var field in table.Fields.OrderBy(x => x.Id))
                    {
                        var type = Type.GetType(field.Type);
                        var data = field.Members.OrderBy(x => x.Id).ToList()[i].Data;
                        object converted;

                        if (string.IsNullOrEmpty(data))
                        {
                            converted = "null";
                        }
                        else
                        {
                            converted = field.Type == "System.Guid" ? data : Convert.ChangeType(data, type);
                        }

                        row.Add(field.Name, converted);
                    }

                    respone.Add(row.ToExpando());
                }
            }

            return this.Ok(respone.AsQueryable());
        }
        public async Task<ActionResult> GetTemplateModelAsync(int companyId, string stateId)
        {
            try
            {
                var company = await _dbContext.Companies.FirstOrDefaultAsync(c => c.Id == companyId);
                if (company == null)
                {
                    throw new InvalidOperationException("Company not found");
                }

                var workflowDef = _workflowDefinition.GetWorkflowDefinition(company.Name);
                if (!workflowDef.States.Keys.Any(k => k == stateId))
                {
                    throw new InvalidOperationException($"Workfloew state '{stateId}' not found");
                }

                IDictionary<string, object> dictionaryModel = new Dictionary<string, object>();
                foreach (var state in workflowDef.States.Values)
                {
                    if (state.GetModel().GetType() != typeof(ExternalActionState))
                    {
                        var modelName = state.GetModel().GetType().Name;
                        if (!dictionaryModel.ContainsKey(modelName))
                        {
                            dictionaryModel.Add(modelName, state.GetModel());
                        }
                    }
                    if (state.Id == stateId)
                    {
                        break;
                    }
                }
                ExpandoObject model = dictionaryModel.ToExpando();

                var result = new Dictionary<string, List<string>>();
                foreach (var key in model.Select(k => k.Key))
                {
                    var list = new List<string>();
                    object obj = model.First(k => k.Key == key).Value;
                    foreach (var prop in obj.GetType().GetProperties())
                    {
                        if (prop.PropertyType.GetInterfaces().Any(i => i.Name == typeof(IEntity).Name) 
                            || prop.PropertyType.GetInterfaces().Any(i => i.Name == typeof(IChildEntity).Name))
                        {                            
                            foreach (var objprop in prop.PropertyType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance))
                            {
                                if (objprop.GetCustomAttribute<JsonIgnoreAttribute>() != null)
                                {
                                    continue;
                                }
                                list.Add($"@Model.{key}.{prop.Name}.{objprop.Name}");
                            }
                        }
                        else
                        {
                            list.Add($"@Model.{key}.{prop.Name}");
                        }
                    }
                    result.Add(key, list);
                }
                return ActionResult.Success(result);
            }
            catch(Exception ex)
            {
                return ActionResult.Failed(ex);
            }
        }