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);
        }
        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);
        }