Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Stefano Beretta 101 posts 246 karma points
    Oct 09, 2019 @ 13:46
    Stefano Beretta
    0

    Working on Umbraco Cloud

    Hi!

    I find your package very useful and I used it with almost all my apps, but I had an issue using it with umbraco cloud (v7).

    I noticed that in the json stored in the db there's a property called "dataTypeId", used to retrieve the configuration of the umbraco's datatype.

    I don't know if it's normal behaviour, but the ids of the datatypes are different between each environments on the cloud, the guids are the same instead.

    So I played a bit with the source code and I've added another prop "dataTypeGuid", used to retrieve the datatype information instead of the "dataTypeId"

    It's a fix, but it seems work well for now.

    FILE: SectionApiController.cs

    Add a new method "AddGuidToObject"

    private void AddGuidToObject(JObject data)
    {
        if (data != null)
        {
            JToken groups;
            if (data.TryGetValue("groups", out groups))
            {
                foreach (var group in groups)
                {
                    var properties = group.Value<JArray>("properties");
                    if (properties != null)
                    {
                        foreach (var property in properties)
                        {
    
                            var dataTypeDefinition =
                                Services.DataTypeService.GetDataTypeDefinitionById(property.Value<int>("dataTypeId"));
                            property["dataTypeGuid"] = dataTypeDefinition.Key;
                        }
                    }
                }
            }
        }
    }
    

    Edit the foreach inside the "AddSomeProperiesToObject"

    foreach (var property in properties)
    {
        //edited code - START
        IDataTypeDefinition dataTypeDefinition = null;
        if (property.Value<string>("dataTypeGuid") == null)
        {
            dataTypeDefinition =
           Services.DataTypeService.GetDataTypeDefinitionById(property.Value<int>("dataTypeId"));
        }
        else
        {
            dataTypeDefinition =
            Services.DataTypeService.GetDataTypeDefinitionById(Guid.Parse(property.Value<string>("dataTypeGuid")));
            property["dataTypeId"] = dataTypeDefinition.Id;
        }
        //edited code - END
    
        var propEditor =
            PropertyEditorResolver.Current.GetByAlias(dataTypeDefinition.PropertyEditorAlias);
        property["editor"] = propEditor?.Alias;
        property["view"] = propEditor?.ValueEditor?.View;
        property["dataTypeName"] = dataTypeDefinition.Name;
    
        var dataTypeDisplay =
            Mapper.Map<IDataTypeDefinition, Umbraco.Web.Models.ContentEditing.DataTypeDisplay>(
                dataTypeDefinition);
        var configDictionairy = dataTypeDisplay.PreValues.ToDictionary(pv => pv.Key, pv => pv.Value);
        AddIdTypeForSomeProperies(configDictionairy, dataTypeDefinition.PropertyEditorAlias);
        property["config"] = JObject.FromObject(configDictionairy);
    }
    

    Call the "AddGuidToObject" inside the "SaveData"

    public HttpResponseMessage SaveData(JObject model)
    {
        IEnumerable<string> result;
        if (!_services.ValidationService.IsValid(model, out result))
        {
            return Request.CreateNotificationValidationErrorResponse(string.Join(",", result));
        }
    
        //new call
        AddGuidToObject(model);
    
    
        var entity = _services.StorageService.Save(model);
        return entity != null
            ? Request.CreateResponse(HttpStatusCode.OK, entity.Value<string>("id"))
            : Request.CreateErrorResponse(HttpStatusCode.BadRequest, "An error occurred while trying to save data");
    }
    

    Thank you for your work, I hope my suggestion can be useful :D

    S

Please Sign in or register to post replies

Write your reply to:

Draft