public async Task <ServerCommandResult> Execute(RequestCommand cmd, ExpandoObject dataToExec)
        {
            if (String.IsNullOrEmpty(cmd.procedure))
            {
                throw new RequestModelException("A procedure must be specified for sql-type command");
            }
            IDataModel model = await _dbContext.LoadModelAsync(cmd.CurrentSource, cmd.CommandProcedure, dataToExec, cmd.commandTimeout);

            //_host.CheckTypes(cmd.Path, cmd.checkTypes, model);

            String invokeTarget = cmd.GetInvokeTarget();

            if (invokeTarget != null)
            {
                var clr = new ClrInvoker();
                clr.EnableThrow();
                clr.Invoke(invokeTarget, dataToExec);                 // after execute
            }
            var r = new ServerCommandResult();

            if (model == null)
            {
                r.Data = "{}";
            }
            else
            {
                r.Data = JsonConvert.SerializeObject(model.Root, JsonHelpers.StandardSerializerSettings);
            }
            return(r);
        }
Exemple #2
0
        internal async Task SaveDataObj(Action <ExpandoObject> setParams, ExpandoObject dataToSave, TextWriter writer)
        {
            String baseUrl = dataToSave.Get <String>("baseUrl");

            if (NormalizeBaseUrl != null)
            {
                baseUrl = NormalizeBaseUrl(baseUrl);
            }
            ExpandoObject data = dataToSave.Get <ExpandoObject>("data");
            var           rm   = await RequestModel.CreateFromBaseUrl(_host, baseUrl);

            RequestView rw   = rm.GetCurrentAction();
            var         prms = new ExpandoObject();

            setParams?.Invoke(prms);
            prms.Append(rw.parameters);
            CheckUserState(prms);
            IDataModel model = null;

            IModelHandler handler      = rw.GetHookHandler();
            String        invokeTarget = rw.GetInvokeTarget();

            if (handler != null)
            {
                Int64 userId  = prms.Get <Int64>("UserId");
                var   handled = await handler.BeforeSave(userId, data);

                if (!handled)
                {
                    model = await _dbContext.SaveModelAsync(rw.CurrentSource, rw.UpdateProcedure, data, prms);

                    await handler.AfterSave(userId, data, model.Root);
                }
            }
            else if (invokeTarget != null)
            {
                model = await _dbContext.SaveModelAsync(rw.CurrentSource, rw.UpdateProcedure, data, prms);

                var clr = new ClrInvoker();
                clr.EnableThrow();
                clr.Invoke(invokeTarget, prms);                 // after save
            }
            else
            {
                model = await _dbContext.SaveModelAsync(rw.CurrentSource, rw.UpdateProcedure, data, prms);
            }
            var eh = rw?.events?.afterSave;

            if (eh != null)
            {
                // new model data
                await _dbContext.SaveModelAsync(eh.CurrentSource(rw), eh.UpdateProcedure(rw), model.Root, prms);
            }
            WriteDataModel(model, writer);
        }
Exemple #3
0
        async Task <Object> DoUploadClr(RequestFile ru, ExpandoObject prms)
        {
            var    invoker = new ClrInvoker();
            Object result;

            if (ru.async)
            {
                result = await invoker.InvokeAsync(ru.clrType, prms);
            }
            else
            {
                result = invoker.Invoke(ru.clrType, prms);
            }
            return(result);
        }
Exemple #4
0
        async Task ExecuteClrCommand(RequestCommand cmd, ExpandoObject dataToInvoke, Guid apiGuid)
        {
            TextWriter writer = Response.Output;

            if (String.IsNullOrEmpty(cmd.clrType))
            {
                throw new RequestModelException($"clrType must be specified for command '{cmd.command}'");
            }
            var invoker = new ClrInvoker();

            invoker.SetRequestInfo(RequestInfo);
            Object result = null;

            if (cmd.async)
            {
                result = await invoker.InvokeAsync(cmd.clrType, dataToInvoke, apiGuid);
            }
            else
            {
                result = invoker.Invoke(cmd.clrType, dataToInvoke, apiGuid);
            }
            if (result == null)
            {
                return;
            }
            if (result is String)
            {
                _logger.LogApi($"response: {result}", Request.UserHostAddress, apiGuid);
                writer.Write(result.ToString());
            }
            else if (result is XDocument xDoc)
            {
                Response.ContentType = "text/xml";
                using (var xmlWriter = XmlWriter.Create(writer))
                {
                    xDoc.WriteTo(xmlWriter);
                }
            }
            else
            {
                String json = JsonConvert.SerializeObject(result, JsonHelpers.StandardSerializerSettings);
                _logger.LogApi($"response: {json}", Request.UserHostAddress, apiGuid);
                writer.Write(json);
            }
        }
        async Task ExecuteSqlCommand(RequestCommand cmd, ExpandoObject dataToExec, TextWriter writer)
        {
            if (String.IsNullOrEmpty(cmd.procedure))
            {
                throw new RequestModelException("A procedure must be specified for sql-type command");
            }
            IDataModel model = await _dbContext.LoadModelAsync(cmd.CurrentSource, cmd.CommandProcedure, dataToExec, cmd.commandTimeout);

            String invokeTarget = cmd.GetInvokeTarget();

            if (invokeTarget != null)
            {
                var clr = new ClrInvoker();
                clr.EnableThrow();
                clr.Invoke(invokeTarget, dataToExec);                 // after execute
            }
            WriteDataModel(model, writer);
        }
        async Task ExecuteClrCommand(RequestCommand cmd, ExpandoObject dataToExec, TextWriter writer)
        {
            if (String.IsNullOrEmpty(cmd.clrType))
            {
                throw new RequestModelException($"clrType must be specified for command '{cmd.command}'");
            }
            var    invoker = new ClrInvoker();
            Object result;

            if (cmd.async)
            {
                result = await invoker.InvokeAsync(cmd.clrType, dataToExec);
            }
            else
            {
                result = invoker.Invoke(cmd.clrType, dataToExec);
            }
            writer.Write(JsonConvert.SerializeObject(result, StandardSerializerSettings));
        }
Exemple #7
0
        public async Task <ServerCommandResult> Execute(RequestCommand cmd, ExpandoObject dataToExec)
        {
            if (String.IsNullOrEmpty(cmd.clrType))
            {
                throw new RequestModelException($"clrType must be specified for command '{cmd.command}'");
            }
            var    invoker = new ClrInvoker();
            Object result;

            if (cmd.async)
            {
                result = await invoker.InvokeAsync(cmd.clrType, dataToExec);
            }
            else
            {
                result = invoker.Invoke(cmd.clrType, dataToExec);
            }
            return(new ServerCommandResult(JsonConvert.SerializeObject(result, JsonHelpers.StandardSerializerSettings)));
        }
        async Task ExecuteClrCommand(RequestCommand cmd, ExpandoObject dataToInvoke)
        {
            TextWriter writer = Response.Output;

            if (String.IsNullOrEmpty(cmd.clrType))
            {
                throw new RequestModelException($"clrType must be specified for command '{cmd.command}'");
            }
            var    invoker = new ClrInvoker();
            Object result  = null;

            if (cmd.async)
            {
                result = await invoker.InvokeAsync(cmd.clrType, dataToInvoke);
            }
            else
            {
                result = invoker.Invoke(cmd.clrType, dataToInvoke);
            }
            if (result == null)
            {
                return;
            }
            if (result is String)
            {
                writer.Write(result.ToString());
            }
            else if (result is XDocument xDoc)
            {
                Response.ContentType = "text/xml";
                using (var xmlWriter = XmlWriter.Create(writer))
                {
                    xDoc.WriteTo(xmlWriter);
                }
            }
            else
            {
                writer.Write(JsonConvert.SerializeObject(result, BaseController.StandardSerializerSettings));
            }
        }