예제 #1
0
        public virtual async Task <ApiActionResult> CreateRangeAsync(List <TCreateModel> models)
        {
            try
            {
                if (models == null)
                {
                    return(null);
                }
                var            userId   = GetUserGuidId();
                List <TEntity> entities = new List <TEntity>();
                foreach (var model in models)
                {
                    string  uniqueId = UniqueIDHelper.GenarateRandomString(12);
                    TEntity entity   = new TEntity();
                    entity.InjectFrom(model);
                    entity.SetValueByNameIfExists("CreatedBy", userId);
                    entity.SetValueByNameIfExists("CreatedDate", DateTime.UtcNow);
                    entity.SetValueByNameIfExists("LastModifiedBy", userId);
                    entity.SetValueByNameIfExists("LastModifiedDate", DateTime.UtcNow);
                    entity.SetValueByNameIfExists("Deleted", false);
                    entity.SetValueByNameIfExists("UniqueId", uniqueId);
                    entities.Add(entity);
                }

                var result = await _repository.CreateEntitiesAsync(entities, userId);

                if (result.Succeeded)
                {
                    return(await ApiActionResult.SuccessAsync());
                }
                return(await ApiActionResult.FailedAsync("Error"));
            }
            catch (Exception ex)
            {
                return(await ApiActionResult.FailedAsync(ex.Message));
            }
        }