예제 #1
0
        public virtual async Task <ApiActionResult> CreateAsync(TCreateModel model)
        {
            try
            {
                if (model == null)
                {
                    return(null);
                }
                var     userId   = GetUserGuidId();
                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);

                var result = await _repository.CreateEntityAsync(entity, userId);

                var viewModel = new TViewModel();
                viewModel.InjectFrom(result.ResponseData);
                return(await ApiActionResult.SuccessAsync(viewModel));
            }
            catch (Exception ex)
            {
                return(await ApiActionResult.FailedAsync(ex.Message));
            }
        }
예제 #2
0
        public virtual async Task <ApiActionResult> DeleteEntityAsync(object id, object userId)
        {
            try
            {
                if (id == null)
                {
                    return(await ApiActionResult.FailedAsync("Entity can not be null."));
                }
                var entity = await FindEntityById(id);

                if (entity.HasProperty("Deleted", typeof(bool)))
                {
                    entity.TrySetPropertyValueByPropertyName("Deleted", true);
                    entity.TrySetPropertyValueByPropertyName("LastModifiedBy", userId);
                    entity.TrySetPropertyValueByPropertyName("LastModifiedDate", DateTime.Now);
                }
                else
                {
                    Entities.Remove(entity);
                    var result = await SaveChangesAsync();

                    if (result == 1)
                    {
                        return(await ApiActionResult.SuccessAsync());
                    }
                }

                return(await ApiActionResult.FailedAsync("Delete Entity Error."));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #3
0
        public virtual async Task <ApiActionResult> CreateEntityAsync(T entity, object userId)
        {
            try
            {
                if (entity == null)
                {
                    return(await ApiActionResult.FailedAsync("Entity can not be null."));
                }
                var trySetCreatedBy   = entity.TrySetPropertyValueByPropertyName("CreatedBy", userId);
                var trySetCreatedDate = entity.TrySetPropertyValueByPropertyName("CreatedDate", DateTime.Now);
                if (!trySetCreatedBy)
                {
                    //=>Log
                }
                if (!trySetCreatedDate)
                {
                    //=>Log
                }

                await Entities.AddAsync(entity);

                var result = await SaveChangesAsync();

                if (result == 1)
                {
                    return(await ApiActionResult.SuccessAsync());
                }

                return(await ApiActionResult.FailedAsync("Create Entity Error."));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #4
0
        public virtual async Task <ApiActionResult> UpdateEntityAsync(T entity, object userId, LoopInjection ignoreProperties = null)
        {
            try
            {
                if (entity == null)
                {
                    return(await ApiActionResult.FailedAsync("Entity can not be null."));
                }

                var entityDb = await Entities.FindAsync(entity);

                if (ignoreProperties == null)
                {
                    ignoreProperties = new LoopInjection(new[] { "CreatedBy", "CreatedDate", "UniqueId", "Id" });
                }
                entityDb.InjectFrom(ignoreProperties, entity);
                entityDb.TrySetPropertyValueByPropertyName("LastModifiedBy", userId);
                entityDb.TrySetPropertyValueByPropertyName("LastModifiedDate", DateTime.Now);

                var result = await SaveChangesAsync();

                if (result == 1)
                {
                    return(await ApiActionResult.SuccessAsync());
                }

                return(await ApiActionResult.FailedAsync("Update Entities Error."));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #5
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));
            }
        }
예제 #6
0
        public virtual async Task <ApiActionResult> DeleteEntitiesAsync(List <T> entities, object userId)
        {
            try
            {
                if (entities == null)
                {
                    return(await ApiActionResult.FailedAsync("Entities can not be null."));
                }

                Entities.RemoveRange(entities);
                var result = await SaveChangesAsync();

                if (result == 1)
                {
                    return(await ApiActionResult.SuccessAsync());
                }

                return(await ApiActionResult.FailedAsync("Delete Entities Error."));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }