Example #1
0
        public virtual async Task <ApiActionResult> UpdateAsync(TUpdateModel model)
        {
            try
            {
                if (model == null)
                {
                    return(null);
                }

                if (!model.HasProperty("Id"))
                {
                    return(await ApiActionResult.FailedAsync("Data not found."));
                }
                var userid   = GetUserGuidId();
                var id       = model.GetValueByNameIfExists("Id");
                var entityDb = await _repository.FindEntityByIdAsync(id);

                if (entityDb == null)
                {
                    return(await ApiActionResult.FailedAsync("Data not found."));
                }
                var ignoreProperties = new LoopInjection(new[] { "CreatedBy", "CreatedDate", "UniqueId", "UniqueCode", "Id" });

                entityDb.InjectFrom(ignoreProperties, model);
                entityDb.SetValueByNameIfExists("LastModifiedBy", userid);
                entityDb.SetValueByNameIfExists("LastModifiedDate", DateTime.UtcNow);

                entityDb.InjectFrom(model);
                return(await _repository.UpdateEntityAsync(entityDb, userid));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #2
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;
            }
        }