public async Task <bool> AddRequestAsync(ContactApplyRequest request, CancellationToken cancellationToken) { var filter = Builders <ContactApplyRequest> .Filter.Where(r => r.UserId == request.UserId && r.ApplierId == request.ApplierId); #if !overTimeVersion if ((await _contactContext.ContactApplyRequests.CountDocumentsAsync(filter, cancellationToken: cancellationToken)) > 0) { var update = Builders <ContactApplyRequest> .Update.Set(r => r.ApplyTime, DateTime.Now).Set(c => c.HandelTime, DateTime.Now); var result = await _contactContext.ContactApplyRequests.UpdateOneAsync(filter, update, null, cancellationToken); return(result.MatchedCount == result.ModifiedCount && result.MatchedCount == 1); } #else var update = Builders <ContactApplyRequest> .Update.Set(r => r.ApplyTime, DateTime.Now); var options = new UpdateOptions { IsUpsert = true }; _contactContext.ContactApplyRequests.UpdateOne(filter, update, options, cancellationToken); #endif await _contactContext.ContactApplyRequests.InsertOneAsync(request, null, cancellationToken); return(true); }
public async Task <bool> Add_ContactApplyRequestAsync(ContactApplyRequest model, CancellationToken cancellationToken) { try { var filter = Builders <ContactApplyRequest> .Filter.And( Builders <ContactApplyRequest> .Filter.In(c => c.ApplierId, new int[] { model.ApplierId, model.UserId }), Builders <ContactApplyRequest> .Filter.In(c => c.UserId, new int[] { model.ApplierId, model.UserId }), Builders <ContactApplyRequest> .Filter.Where(s => s.Approvaled == 1) ); var applyRequest = await _contactContext.ContactApplyRequests.CountDocumentsAsync(filter, null, cancellationToken); if (applyRequest > 0) { var update = Builders <ContactApplyRequest> .Update.Set(s => s.CreateTime, DateTime.Now); var result = await _contactContext.ContactApplyRequests.UpdateOneAsync(filter, update, null, cancellationToken); return(result.ModifiedCount == result.MatchedCount); } await _contactContext.ContactApplyRequests.InsertOneAsync(model, null, cancellationToken); return(true); } catch { return(false); } }
/// <summary> /// 添加好友请求到MongoDB里 /// </summary> /// <param name="request"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task <bool> AddRequestAsync(ContactApplyRequest request, CancellationToken cancellationToken) { //获取这个好友请求是否在MongoDB有记录,如果有,计算个数 var count = await _contactContext.ContactApplyRequests.CountDocumentsAsync <ContactApplyRequest>(r => r.ApplierId == request.ApplierId); //已经发送过好友申请,更新申请时间 if (count > 0) { var filter = Builders <ContactApplyRequest> .Filter.Where(a => a.ApplierId == request.ApplierId && a.UserId == request.UserId); var update = Builders <ContactApplyRequest> .Update.Set(a => a.ApplyTime, DateTime.Now); //UpdateOptions里的IsUpsert,可以用在下面的UpdateOneAsync中, //如果值为没有存在这条记录就会创建(该条记录里的所有值都要一样),如果存在就更新 //var option = new UpdateOptions { IsUpsert = true }; var result = await _contactContext.ContactApplyRequests.UpdateOneAsync(filter, update); //修改的元素和匹配的元素对应,并且MongoDB里只能有一条该用户申请好友的记录 return(result.MatchedCount == result.ModifiedCount && result.MatchedCount == 1); } //创建一个好友申请记录在MongoDB await _contactContext.ContactApplyRequests.InsertOneAsync(request); return(true); }
public async Task <bool> AddRequestAsync(ContactApplyRequest request, CancellationToken cancellationToken) { var filter = Builders <ContactApplyRequest> .Filter.Where(r => r.UserId == request.UserId && r.ApplicantId == request.ApplicantId); if ((await _context.ContactApplyRequests.CountAsync(filter)) > 0) { var updateResult = await _context.ContactApplyRequests.ReplaceOneAsync(c => c.UserId == request.UserId && c.ApplicantId == request.ApplicantId, request); return(updateResult.ModifiedCount > 0); } await _context.ContactApplyRequests.InsertOneAsync(request, null, cancellationToken); //await _elasticClient.IndexAsync(request, idx => idx.Index(ContactSearchConfig.ContactIndexName)); return(true); }
public async Task <bool> AddRequstAsync(ContactApplyRequest request, CancellationToken cancellationToken) { var filter = Builders <ContactApplyRequest> .Filter.Where(r => r.UserId == request.UserId && r.ApplierId == request.ApplierId); if ((await _contactContext.ContactApplyRequests.CountDocumentsAsync(filter)) > 0) { var update = Builders <ContactApplyRequest> .Update.Set(s => s.HandledTime, DateTime.Now).Set(r => r.Approvaled, 1); var result = await _contactContext.ContactApplyRequests.UpdateOneAsync(filter, update, null, cancellationToken); return(result.MatchedCount == result.ModifiedCount && result.ModifiedCount == 1); } await _contactContext.ContactApplyRequests.InsertOneAsync(request, null, cancellationToken); return(true); }
//public MongoContactApplyRequestRepository(ContactContext contactContext) //{ // _contactContext = contactContext; //} /// <summary> /// 请求添加好友 /// </summary> /// <param name="request"></param> /// <returns></returns> public async Task <bool> AddRequestAsync(ContactApplyRequest request, CancellationToken cancellationToken) { var filter = Builders <ContactApplyRequest> .Filter.Where(q => q.UserId == request.UserId && q.AppliedId == request.AppliedId); //如果已经有了该好友请求更新请求时间 if ((await _contactContext.ContactApplyRequests.CountDocumentsAsync(filter)) > 0) { var update = Builders <ContactApplyRequest> .Update.Set(r => r.ApplyTime, DateTime.Now); var updateRes = await _contactContext.ContactApplyRequests.UpdateOneAsync(filter, update); return(updateRes.MatchedCount == updateRes.ModifiedCount && updateRes.MatchedCount == 1); } await _contactContext.ContactApplyRequests.InsertOneAsync(request, null, cancellationToken); return(true); }
public async Task <bool> AddRequestAsync(ContactApplyRequest request, CancellationToken cancellationToken) { var fileter = Builders <ContactApplyRequest> .Filter.Where(r => r.UserId == request.UserId && r.ApplierID == request.ApplierID); if ((await _contactContext.ContactApplyRequest.CountDocumentsAsync(fileter)) > 0) { var update = Builders <ContactApplyRequest> .Update.Set(r => r.ApplyTime, DateTime.Now); //var options = new UpdateOptions { IsUpsert = true }; var reslut = await _contactContext.ContactApplyRequest.UpdateOneAsync(fileter, update, null, cancellationToken); return(reslut.MatchedCount == reslut.ModifiedCount && reslut.MatchedCount == 1); } await _contactContext.ContactApplyRequest.InsertOneAsync(request, null, cancellationToken); return(true); }
public async Task <bool> AddRequestAsync(ContactApplyRequest request, CancellationToken cancellationToken) { //声明是否重复的filterdefinition 和 updatedefinition var filter = Builders <ContactApplyRequest> .Filter.Where(r => r.UserId == request.UserId && r.ApplierId == request.ApplierId); if ((await _context.ContactApplyRequests.CountAsync(filter)) > 0) { var update = Builders <ContactApplyRequest> .Update.Set(r => r.ApplyTime, DateTime.Now); var result = await _context.ContactApplyRequests.UpdateOneAsync(filter, update); return(result.MatchedCount == result.ModifiedCount && result.ModifiedCount == 1); } await _context.ContactApplyRequests.InsertOneAsync(request, null, cancellationToken); return(true); }
public async Task <bool> Approvaled_ContactApplyRequestAsync(ContactApplyRequest model, CancellationToken cancellationToken) { try { var updateDefinition = Builders <ContactApplyRequest> .Update .Set(c => c.HandledTime, model.HandledTime) .Set(c => c.Approvaled, model.Approvaled); var contactApply = await _contactContext.ContactApplyRequests.FindOneAndUpdateAsync(s => s.ApplierId == model.ApplierId && s.UserId == model.UserId, updateDefinition, null, cancellationToken); return(contactApply != null); } catch { return(false); } }
/// <summary> /// 添加好友申请 /// </summary> /// <param name="request"></param> /// <returns></returns> public async Task <bool> AddReqeustAsync(ContactApplyRequest request, CancellationToken cancellationToken) { var filter = Builders <ContactApplyRequest> .Filter.Where(c => c.UserId == request.UserId && c.ApplierId == request.ApplierId); if ((await _contactContext.ContactApplyRequests.CountAsync(filter, null, cancellationToken)) > 0) { var update = Builders <ContactApplyRequest> .Update.Set(r => r.ApplyTime, DateTime.Now); //var options = new UpdateOptions { IsUpsert = true } //没有就插入 var result = await _contactContext.ContactApplyRequests.UpdateOneAsync(filter, update, null, cancellationToken); return(result.MatchedCount == result.ModifiedCount && result.MatchedCount > 0); } await _contactContext.ContactApplyRequests.InsertOneAsync(request, null, cancellationToken); return(true); }
public async Task <bool> AddRequestAsync(ContactApplyRequest request, CancellationToken cancellationToken) { var filter = Builders <ContactApplyRequest> .Filter .Where(c => c.UserId == request.UserId && request.ApplierId == c.ApplierId); if ((await _contactContext.ContactApplyRequests.CountDocumentsAsync(filter, null, cancellationToken)) > 0) { var update = Builders <ContactApplyRequest> .Update.Set(r => r.CreateTime, DateTime.Now); var result = await _contactContext.ContactApplyRequests.UpdateOneAsync(filter, update, null, cancellationToken); return(result.IsAcknowledged && result.MatchedCount == 1 && result.MatchedCount == result.ModifiedCount); } await _contactContext.ContactApplyRequests.InsertOneAsync(request, null, cancellationToken); return(true); }
/// <summary> /// 添加 申请好友的请求 /// </summary> /// <param name="request"></param> /// <returns></returns> public async Task <bool> AddRequestAsync(ContactApplyRequest request) { var filter = Builders <ContactApplyRequest> .Filter.Where(c => c.UserId == request.UserId && c.ApplierId == request.ApplierId); if (_contactContext.ContactApplyRequests.Find(filter).CountDocuments() > 0) { var update = Builders <ContactApplyRequest> .Update.Set(c => c.ApplyTime, DateTime.Now); var result = await _contactContext.ContactApplyRequests.UpdateOneAsync(filter, update); return(result.MatchedCount == result.ModifiedCount && result.ModifiedCount == 1); } await _contactContext.ContactApplyRequests.InsertOneAsync(request); return(true); }
public async Task <bool> AddRequestAsync(ContactApplyRequest contactApplyRequest, CancellationToken cancellationToken) { var filter = Builders <ContactApplyRequest> .Filter.Where(r => r.UserId == contactApplyRequest.UserId && r.ApplierId == contactApplyRequest.ApplierId); if ((_ContactContext.ContactApplyRequest.CountDocuments(filter)) > 0) { var update = Builders <ContactApplyRequest> .Update .Set(r => r.Approvaled, Models.Enum.ApplyStatus.Waiting) .Set(r => r.HandleTime, DateTime.Now); var UpdateResult = await _ContactContext.ContactApplyRequest.UpdateOneAsync(filter, update, null, cancellationToken); return(UpdateResult.MatchedCount == 1 && UpdateResult.MatchedCount == UpdateResult.ModifiedCount); } await _ContactContext.ContactApplyRequest.InsertOneAsync(contactApplyRequest, null, cancellationToken); return(true); }
/// <summary> /// 用户好友申请 /// </summary> /// <param name="request"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task <bool> AddApplyRequestAsync(ContactApplyRequest request) { //1.先查询是否存在 存在即做uodate操作 //2.新增 var filter = Builders <ContactApplyRequest> .Filter.Where(s => s.UserId.Equals(request.UserId) && s.ApplierId.Equals(request.ApplierId)); if ((await _contactContext.ContactApplyRequests.CountDocumentsAsync(filter)) > 0) { var update = Builders <ContactApplyRequest> .Update.Set(s => s.ApplyTime, DateTime.Now); var result = await _contactContext.ContactApplyRequests.UpdateManyAsync(filter, update); return(result.MatchedCount == result.ModifiedCount && result.ModifiedCount == 1); } await _contactContext.ContactApplyRequests.InsertOneAsync(request); return(true); }
/// <summary> /// 添加好友申请 /// </summary> /// <param name="request"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public Task <bool> AddRequestAsync(ContactApplyRequest request, CancellationToken cancellationToken) { ////检查是否已经存在好友申请请求 ////filter //var countFilter = Builders<ContactApplyRequest>.Filter.And( // Builders<ContactApplyRequest>.Filter.Eq(b=>b.UserId,request.UserId), // Builders<ContactApplyRequest>.Filter.Eq(b=>b.ApplierId,request.ApplierId) // ); //if ((await _context.ContactApplyRequests.CountDocumentsAsync(countFilter)==1)) //{ // //条件 // var filter = Builders<ContactApplyRequest>.Filter.And( // ); // //如果存在,更新请求时间 //} ////进行新增操作 //await _context.ContactApplyRequests.InsertOneAsync(request, null, cancellationToken); throw new NotImplementedException(); }
public async Task <bool> AddRequestAsync(ContactApplyRequest request, CancellationToken cancellationToken) { //检查是否已经存在好友申请请求 if ((await _context.ContactApplyRequests.CountAsync(dd => dd.UserId == request.UserId && dd.ApplierId == request.ApplierId) == 1)) { //条件 var filter = Builders <ContactApplyRequest> .Filter.And( Builders <ContactApplyRequest> .Filter.Eq(b => b.ApplierId, request.ApplierId), Builders <ContactApplyRequest> .Filter.Eq(b => b.UserId, request.UserId) ); //如果存在,更新申请时间 var update = Builders <ContactApplyRequest> .Update.Set(b => b.ApplyTime, DateTime.Now); var result = await _context.ContactApplyRequests.UpdateOneAsync(filter, update, null, cancellationToken); return(result.MatchedCount == result.ModifiedCount && result.ModifiedCount == 1); } //进行新增操作 await _context.ContactApplyRequests.InsertOneAsync(request, null, cancellationToken); return(true); }