示例#1
0
        public JsonResult CreateField(List <KeyValue> keyValues)
        {
            try
            {
                var id               = Guid.NewGuid();
                var name             = keyValues.FirstOrDefault(x => x.Key == "Name")?.Value;
                var displayName      = keyValues.FirstOrDefault(x => x.Key == "DisplayName")?.Value;
                var description      = keyValues.FirstOrDefault(x => x.Key == "Description")?.Value;
                var entityId         = keyValues.FirstOrDefault(x => x.Key == "EntityId")?.Value;
                var entityName       = keyValues.FirstOrDefault(x => x.Key == "EntityName")?.Value;
                var fieldTypeId      = keyValues.FirstOrDefault(x => x.Key == "FieldTypeId")?.Value;
                var fieldTypeName    = keyValues.FirstOrDefault(x => x.Key == "FieldTypeName")?.Value;
                var isRequired       = keyValues.FirstOrDefault(x => x.Key == "IsRequired")?.Value;
                var lookupEntityName = keyValues.FirstOrDefault(x => x.Key == "LookupEntityName")?.Value;

                #region Create field record
                var returnValue = SharedService.ExecutePostSqlStoredProcedure("[eduq].[Field_create]",
                                                                              new List <SqlParameter>
                {
                    new SqlParameter("@id", id),
                    new SqlParameter("@name", name),
                    new SqlParameter("@displayName", displayName),
                    new SqlParameter("@description", description),
                    new SqlParameter("@entityId", entityId),
                    new SqlParameter("@fieldTypeId", fieldTypeId),
                    new SqlParameter("@isRequired", isRequired),
                    new SqlParameter("@stateId", 1)
                });

                if (returnValue != 1)
                {
                    return(Json(new { message = "Une erreur s'est produise pendant la creation de l'attribut!", type = "error" }, JsonRequestBehavior.AllowGet));
                }
                var newlyCreatedField = FieldService.QueryFieldById(id);
                if (newlyCreatedField == null)
                {
                    return(Json(new { message = "Une erreur s'est produise pendant la creation de l'attribut!", type = "error" }, JsonRequestBehavior.AllowGet));
                }
                #endregion

                #region Create sql table column
                var returnValueCreateSqlTableColumn = SqlService.CreateSqlTableColumn(entityName, name, fieldTypeName, (isRequired == "true" ? "NOT NULL": ""));
                if (!returnValueCreateSqlTableColumn)
                {
                    return(Json(new { message = "Une erreur s'est produise pendant la creation de l'attribut!", type = "error" }, JsonRequestBehavior.AllowGet));
                }
                #endregion

                #region Create sql foreign key constraint
                if (lookupEntityName == null)
                {
                    return(Json(new { message = id, type = "success" }, JsonRequestBehavior.AllowGet));
                }
                var returnValueCreateSqlForeignKeyConstraint = SqlService.CreateSqlForeignKeyConstraint(entityName, lookupEntityName, name);
                if (!returnValueCreateSqlForeignKeyConstraint)
                {
                    return(Json(new { message = "Une erreur s'est produise pendant la creation de l'attribut!", type = "error" }, JsonRequestBehavior.AllowGet));
                }
                #endregion

                return(Json(new { message = id, type = "success" }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                return(Json(new { message = ex.Message, type = "error" }, JsonRequestBehavior.AllowGet));
            }
        }