Example #1
0
        public IActionResult PatchEntity(string StringId, [FromBody]JObject submitObj)
        {
            FieldResponse response = new FieldResponse();
            InputEntity entity = new InputEntity();

            try
            {
                Guid entityId;
                if (!Guid.TryParse(StringId, out entityId))
                {
                    response.Errors.Add(new ErrorModel("id", StringId, "id parameter is not valid Guid value"));
                    return DoResponse(response);
                }

                DbEntity storageEntity = DbContext.Current.EntityRepository.Read(entityId);
                if (storageEntity == null)
                {
                    response.Timestamp = DateTime.UtcNow;
                    response.Success = false;
                    response.Message = "Entity with such Name does not exist!";
                    return DoBadRequestResponse(response);
                }
                entity = storageEntity.MapTo<Entity>().MapTo<InputEntity>();

                Type inputEntityType = entity.GetType();

                foreach (var prop in submitObj.Properties())
                {
                    int count = inputEntityType.GetProperties().Where(n => n.Name.ToLower() == prop.Name.ToLower()).Count();
                    if (count < 1)
                        response.Errors.Add(new ErrorModel(prop.Name, prop.Value.ToString(), "Input object contains property that is not part of the object model."));
                }

                if (response.Errors.Count > 0)
                    return DoBadRequestResponse(response);

                InputEntity inputEntity = submitObj.ToObject<InputEntity>();

                foreach (var prop in submitObj.Properties())
                {
                    if (prop.Name.ToLower() == "label")
                        entity.Label = inputEntity.Label;
                    if (prop.Name.ToLower() == "labelplural")
                        entity.LabelPlural = inputEntity.LabelPlural;
                    if (prop.Name.ToLower() == "system")
                        entity.System = inputEntity.System;
                    if (prop.Name.ToLower() == "iconname")
                        entity.IconName = inputEntity.IconName;
                    if (prop.Name.ToLower() == "weight")
                        entity.Weight = inputEntity.Weight;
                    if (prop.Name.ToLower() == "recordpermissions")
                        entity.RecordPermissions = inputEntity.RecordPermissions;
                }
            }
            catch (Exception e)
            {
                return DoBadRequestResponse(response, "Input object is not in valid format! It cannot be converted.", e);
            }

            return DoResponse(entMan.UpdateEntity(entity));
        }