Beispiel #1
0
        public SaveEntityResult SaveEntity(SaveEntityParams parameters)
        {
            Throw.IfNull(parameters, nameof(parameters));
            Throw.IfNull(parameters.Entity, nameof(parameters), nameof(parameters.Entity));

            var entityMetadata = _metadataService.GetEntityMetadata(new GetEntityMetadataParams
            {
                EntityName = parameters.EntityName
            });

            var requestEntityType = ((object)parameters.Entity).GetType();

            dynamic convertedEntity = null;

            if (requestEntityType == entityMetadata.Type)
            {
                convertedEntity = parameters.Entity;
            }
            else if (requestEntityType == typeof(JObject))
            {
                convertedEntity = ((JObject)parameters.Entity).ToObject(entityMetadata.Type);
            }
            else
            {
                throw new EafException($"Cannot convert type {requestEntityType.Name} to {entityMetadata.Type.Name} ");
            }

            if (parameters.IsNew)
            {
                _dataContext.Add(convertedEntity);
            }
            else
            {
                var entry = _dataContext.Attach(convertedEntity);
                entry.State = EntityState.Modified;
            }

            _dataContext.SaveChanges();

            var result = new SaveEntityResult();

            if (parameters.IsFillEntityListItemRequired)
            {
                var entityEntry = _dataContext.Entry(convertedEntity);
                var keys        = new List <AttributeValue>();
                foreach (var key in entityMetadata.GetKeys())
                {
                    keys.Add(new AttributeValue(key.Name, entityEntry.Property(key.Name).CurrentValue));
                }

                var query = GetEntityListQuery(entityMetadata);
                query = AddKeyConditions(query, keys);
                result.EntityLisItem = query.FirstOrDefault();
            }

            return(result);
        }
Beispiel #2
0
 public SaveEntityResult SaveEntity([FromBody] SaveEntityParams parameters)
 {
     return(_entityService.SaveEntity(parameters));
 }