public override async Task <TGetModel> OnHandle(RestPutRequest <TEntity, TGetModel> message, CancellationToken cancellationToken)
        {
            var obj = await OpinionatedDbContext.Set <TEntity>()
                      .SingleOrDefaultAsync(o => o.Id == message.Id);

            if (obj == null)
            {
                throw new EntityNotFoundException(typeof(TEntity), message.Id);
            }

            var properties = typeof(TEntity).GetProperties();

            foreach (var prop in message.Parameters)
            {
                var name  = prop.Key;
                var value = prop.Value;

                var property = properties.Single(p => p.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
                if (property == null)
                {
                    continue;
                }

                property.SetValue(obj, Convert.ChangeType(value, property.PropertyType));
            }

            await OpinionatedDbContext.SaveChangesAsync();

            return(Mapper.Map <TGetModel>(obj));
        }
示例#2
0
        public override async Task <TGetModel> OnHandle(RestPostRequest <TEntity, TPostModel, TGetModel> message, CancellationToken cancellationToken)
        {
            var newEntity = Mapper.Map <TEntity>(message.NewEntity);

            OpinionatedDbContext.Add(newEntity);
            await OpinionatedDbContext.SaveChangesAsync(cancellationToken);

            return(Mapper.Map <TGetModel>(newEntity));
        }
        public override async Task <object> OnHandle(RestDeleteRequest <TEntity> message, CancellationToken cancellationToken)
        {
            var objectToRemove = await OpinionatedDbContext.Set <TEntity>().FindAsync(message.Id);

            if (objectToRemove == null)
            {
                throw new EntityNotFoundException(typeof(TEntity), message.Id);
            }

            OpinionatedDbContext.Remove(objectToRemove);
            await OpinionatedDbContext.SaveChangesAsync(cancellationToken);

            return(null);
        }