Exemplo n.º 1
0
        public async Task <Entity> CreateEntityAsync(CreateEntityRequest request, CancellationToken cancellationToken = new CancellationToken())
        {
            var fields = request.Fields ?? new Field[] { };

            //Attempt to get the entity definition.
            var entityDefinition = await _storage.GetEntityDefinitionAsync(request.EntityDefinitionId, cancellationToken);

            if (entityDefinition == null)
            {
                throw new EntityDefinitionNotFoundException($"Entity definition '{request.EntityDefinitionId}' not found.");
            }

            foreach (var field in fields)
            {
                //Attempt to get the field definition
                var fieldDefinition = entityDefinition.Fields.FirstOrDefault(f => f.Id == field.FieldDefinitionId);

                if (fieldDefinition == null)
                {
                    throw new FieldDefinitionNotFoundException($"Unable to find field definition '{field.FieldDefinitionId}'.");
                }

                //TODO: Validate the data type.
            }

            //Create the entity
            var entity = new Entity()
            {
                Id = Guid.NewGuid(),
                EntityDefinitionId = request.EntityDefinitionId,
                Name       = request.Name,
                Fields     = fields,
                CreatedUtc = DateTime.UtcNow
            };

            //Add the entity to the backing store.
            await _storage.AddEntityAsync(entity, cancellationToken);

            return(entity);
        }