Esempio n. 1
0
        public virtual async Task UpdateFieldById <TField>(TKey id,
                                                           Expression <Func <TEntity, TField> > memberExpression, TField value)
        {
            var entity = (await DbSet.FirstOrDefaultAsync(q => q.Id.Equals(id)))
                         ?? throw new KeyNotFoundException(
                                   $"there is no item in {typeof(TEntity).Name} with id : {id}");

            var memberName = LambdaExpressions.GetName(memberExpression);

            entity.GetType()
            .GetProperty(memberName)
            ?.SetValue(entity, value);
        }
Esempio n. 2
0
        public virtual async Task UpdateFieldById <TField>(object[] ids,
                                                           Expression <Func <TEntity, TField> > memberExpression, TField value)
        {
            var entity = await DbSet.FindAsync(ids)
                         ?? throw new KeyNotFoundException(
                                   $"there is no item in {typeof(TEntity).Name} with id : {ids}");

            var memberName = LambdaExpressions.GetName(memberExpression);

            entity.GetType()
            .GetProperty(memberName)
            ?.SetValue(entity, value);
        }
Esempio n. 3
0
        /// <inheritdoc />
        public virtual async Task UpdateField <TField>(Expression <Func <TEntity, bool> > filterExpression,
                                                       Expression <Func <TEntity, TField> > memberExpression,
                                                       TField value)
        {
            var entity = (await DbSet.FirstOrDefaultAsync(filterExpression))
                         ?? throw new KeyNotFoundException(
                                   $"there is no item in {typeof(TEntity).Name} with query : {filterExpression}");

            var memberName = LambdaExpressions.GetName(memberExpression);

            entity.GetType()
            .GetProperty(memberName)
            ?.SetValue(entity, value);
        }
        public virtual Result UpdateOne <TField>(TKey id,
                                                 Expression <Func <TEntity, TField> > memberExpression,
                                                 TField value)
        {
            var entity = base.DbSet.FirstOrDefault(q => q.Id.Equals(id));

            if (entity == null)
            {
                return(Result.Error($"there is no item in {typeof(TEntity).Name} with id : {id}"));
            }

            try
            {
                var memberName = LambdaExpressions.GetName(memberExpression);
                entity.GetType().GetProperty(memberName).SetValue(entity, value);
                var item = base.Context.SaveChanges();
                return(Result.Okay(Messages.ItemUpdated));
                //return Result.Okay($"{item} item successfully updated.");
            }
            catch (Exception ex)
            {
                return(Result.Exception(ex));
            }
        }