Пример #1
0
        public ActionResult NewField([FromBody] string fieldJS)
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();

            GlobalVars.ResultGeneric result = new GlobalVars.ResultGeneric()
            {
                Message    = "",
                ReturnCode = 0,
                //ReturnValue = ""
            };
            try
            {
                if (fieldJS == null)
                {
                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    result.ReturnCode   = -1;
                    result.Message      = "Missing argument fieldJS";
                    logger.Warn("NewField API Request ends with an Error.");
                    logger.Warn(result.Message);
                }
                else
                {
                    GlobalVars.Field field = JsonConvert.DeserializeObject <GlobalVars.Field>(fieldJS);
                    logger.Info("NewField API Request.");
                    if (!string.IsNullOrEmpty(field.CPFieldName) & field.FieldID == 0)
                    {
                        logger.Debug("Parameter:" + JsonConvert.SerializeObject(field, Formatting.Indented));

                        result = SQLFunctionsFields.NewField(field);
                        switch (result.ReturnCode)
                        {
                        case 0:
                            logger.Info("NewField API Request was executed Successfully.");
                            Response.StatusCode = (int)HttpStatusCode.OK;
                            break;

                        case -1:
                            logger.Info("NewField API Request ends with a warning.");
                            Response.StatusCode = (int)HttpStatusCode.OK;
                            break;

                        case -2:
                            Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                            logger.Fatal("NewField API Request ends with a Fatal Error.");
                            logger.Debug("Returned value:" + JsonConvert.SerializeObject(result, Formatting.Indented));
                            Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                            break;
                        }
                    }
                    else
                    {
                        Response.StatusCode = (int)HttpStatusCode.BadRequest;
                        result.ReturnCode   = -1;
                        result.Message      = "Field Name and/or Field ID value is not valid.";
                        logger.Warn("NewField API Request ends with an Error.");
                        logger.Warn(result.Message);
                    }
                }
            }
            catch (Exception e)
            {
                logger.Fatal("NewField API Request ends with a Fatal Error.");
                result.ReturnCode = -2;
                result.Message    = e.Message;
                var baseException = e.GetBaseException();
                result.Exception = baseException.ToString();
                logger.Fatal("Returned value:" + JsonConvert.SerializeObject(result, Formatting.Indented));
                Response.StatusCode = (int)HttpStatusCode.InternalServerError;
            }
            Response.ContentType = "application/json";
            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;

            elapsedMs             = elapsedMs / 1000;
            result.ElapsedTime    = elapsedMs.ToString();
            result.HttpStatusCode = Response.StatusCode.ToString();
            var messaje = JsonConvert.SerializeObject(result, Formatting.Indented);

            logger.Info("Leaving NewField API");
            //return Json(messaje);
            return(Content(messaje));
        }