示例#1
0
        // Update
        public async Task <Maybe <TEntity> > Update <TEntity>(string id, UpdateDefinition <TEntity> update) where TEntity : IDbRecord
        {
            using (var ctx = GetContext()) {
                await ctx.AuditLogs.InsertOneAsync(new DbAudit { Type = AuditType.RepositoryBase, Message = string.Format("Update: {0}", update.ToString()) });

                var record = await GetById <TEntity>(ctx, id);

                if (record == null)
                {
                    // The record to update does not exist
                    return(Maybe <TEntity> .Fail);
                }
                var filter = GetByIdFilter <TEntity>(id);
                update = update.Set(x => x.LastModified, Now);

                var result = await ctx.GetCollection <TEntity>().UpdateOneAsync(filter, update);

                // The record being updated should be updated. If not, use updatedRecord.
                // var updatedRecord = await ctx.GetCollection<TEntity>().Find(filter).SingleOrDefaultAsync();
                return(ReturnMaybe(record));
            }
        }