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)); }
public override async Task <TGetModel> OnHandle(RestSingleGetRequest <TEntity, TGetModel> message, CancellationToken cancellationToken) { var ret = await OpinionatedDbContext.Set <TEntity>() .ProjectTo <TGetModel>(Mapper.ConfigurationProvider) .SingleOrDefaultAsync(o => o.Id == message.Id); if (ret == null) { throw new EntityNotFoundException(typeof(TEntity), message.Id); } return(ret); }
public override async Task <object> OnHandle(RestGetListRequest <TEntity, TGetModel> message, CancellationToken cancellationToken) { IQueryable <TEntity> results = OpinionatedDbContext.Set <TEntity>(); if (message.UsePaging) { var page = await CreatePagedResultsAsync <TEntity, TGetModel>(results, message.PageNumber, message.NumberOfRecords, nameof(RestApiEntity.Id), true); return(page); } return(await results.ProjectTo <TGetModel>(Mapper.ConfigurationProvider).ToArrayAsync()); }
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); }