public async Task <RemotingResult> UpdateStatusByIdAsync(string id, UploadStatus status) { if (string.IsNullOrEmpty(id)) { throw new ArgumentException("IsNullOrEmpty", nameof(id)); } var(dictList, dictIndexContextId) = await GetDicts(); using (var tx = StateManager.CreateTransaction()) { var wrap = await dictList.TryGetValueAsync(tx, id); if (!wrap.HasValue) { return(RemotingResult.Fail(1)); } if (status < wrap.Value.Status) { return(RemotingResult.Fail(2)); } var entity = wrap.Value.DeepCopy(); entity.Status = status; if (status == UploadStatus.Uploaded) { entity.Uploaded = DateTimeOffset.UtcNow; } await dictList.TryUpdateAsync(tx, id, entity, wrap.Value); await tx.CommitAsync(); return(RemotingResult.Success()); } }
public async Task TestQuit() { var groupAppService = Substitute.For <IGroupAppService>(); var target = new GroupController( CreateMemoryCache(), CreateMapper(), Substitute.For <IDepartmentAppService>(), Substitute.For <IPositionAppService>(), groupAppService); target.ControllerContext = CreateMockContext(); groupAppService.QuitAsync(Arg.Any <GroupInput>()) .ReturnsForAnyArgs(RemotingResult.Success()); var result = await target.Quit(Guid.NewGuid()); result.Value.Should().NotBeNull(); result.Value.Status.Should().Be(0); groupAppService.QuitAsync(Arg.Any <GroupInput>()) .ReturnsForAnyArgs(RemotingResult.Fail()); result = await target.Quit(Guid.NewGuid()); result.Value.Status.Should().NotBe(0); }
public async Task <RemotingResult <string> > CreateScanJoinCodeAsync(Guid id, Guid userId) { if (id == Guid.Empty) { throw new ArgumentException("must not be empty", nameof(id)); } if (userId == Guid.Empty) { throw new ArgumentException("must not be empty", nameof(userId)); } var createdBy = Guid.Empty; using (var db = new ServiceDbContext(_dbOptions)) { var query = from entity in db.Groups where entity.Id == id && entity.Type == GroupType.CustomChat select entity.CreatedBy; createdBy = await query.FirstOrDefaultAsync(); if (createdBy == Guid.Empty) { return(RemotingResult <string> .Fail(1)); } if (createdBy != userId) { return(RemotingResult <string> .Fail(2)); } } var fingerprint = Guid.NewGuid().ToString("N"); await _simpleKeyValueService.AddOrUpdate(SKVContainer_ScanJoinCode, id.ToString("N"), fingerprint); return(RemotingResult <string> .Success(id.ToString("N") + "," + fingerprint)); }
public async Task <RemotingResult> AddDepAsync(AddDepConversationInput input) { var userIds = await _employeeAppService.GetUserIdsByDepartmentIdAsync(input.DepartmentId); if (!userIds.Contains(input.UserId)) { return(RemotingResult.Fail(1)); } if (userIds.Count < 2) { return(RemotingResult.Fail(2)); } await _manager.AddOrUpdateAsync(input.DepartmentId, userIds, ConversationType.DepartmentGroup); return(RemotingResult.Success()); }
public async Task <RemotingResult> RemoveByContextIdAsync(Guid contextId) { var(dictList, dictIndexContextId) = await GetDicts(); using (var tx = StateManager.CreateTransaction()) { var indexWrap = await dictIndexContextId.TryGetValueAsync(tx, contextId); if (!indexWrap.HasValue) { return(RemotingResult.Fail(1)); } foreach (var item in indexWrap.Value) { await dictList.TryRemoveAsync(tx, item); } await dictIndexContextId.TryRemoveAsync(tx, contextId); await tx.CommitAsync(); return(RemotingResult.Success()); } }
public async Task CreateDepConverationAsync() { var conversationCtrlAppService = Substitute.For <IConversationCtrlAppService>(); conversationCtrlAppService.GetByIdAsync(Arg.Any <Guid>()) .Returns(default(Conversation?)); conversationCtrlAppService.AddDepAsync(Arg.Any <AddDepConversationInput>()) .Returns(RemotingResult.Success()); Task <IEnumerable <IConversationMsgAppService> > getConversationMsgAppServices() { var conversationMsgAppServices = new List <IConversationMsgAppService> { Substitute.For <IConversationMsgAppService>() }; return(Task.FromResult(conversationMsgAppServices.AsEnumerable())); } var target = new InstantMessageController(conversationCtrlAppService, _ => Substitute.For <IConversationMsgAppService>(), getConversationMsgAppServices, Substitute.For <IEmployeeCacheService>(), Substitute.For <IGroupAppService>(), Substitute.For <IDepartmentAppService>()) { ControllerContext = CreateMockContext() }; var result = await target.CreateDepConverationAsync(Guid.NewGuid()); result.Value.Should().BeOfType <ResponseData>(); result.Value.Status.Should().Be(0); var depId = Guid.NewGuid(); conversationCtrlAppService.GetByIdAsync(depId) .Returns(new Conversation { Id = depId, Participants = new List <Guid> { User_Id }, Type = ConversationType.DepartmentGroup }); result = await target.CreateDepConverationAsync(depId); result.Value.Status.Should().Be(0); conversationCtrlAppService.GetByIdAsync(depId) .Returns(new Conversation { Id = depId, Participants = new List <Guid> { Guid.NewGuid() }, Type = ConversationType.DepartmentGroup }); result = await target.CreateDepConverationAsync(depId); result.Value.Status.Should().Be(1); conversationCtrlAppService.GetByIdAsync(depId) .Returns(default(Conversation?)); conversationCtrlAppService.AddDepAsync(Arg.Is <AddDepConversationInput>(x => x.DepartmentId == depId && x.UserId == User_Id)) .Returns(RemotingResult.Fail(1)); result = await target.CreateDepConverationAsync(depId); result.Value.Status.Should().Be(2); conversationCtrlAppService.AddDepAsync(Arg.Is <AddDepConversationInput>(x => x.DepartmentId == depId && x.UserId == User_Id)) .Returns(RemotingResult.Fail(2)); result = await target.CreateDepConverationAsync(depId); result.Value.Status.Should().Be(3); }
public async Task <RemotingResult> ScanJoinAsync(string code, Guid employeeId, string employeeName) { if (code == null) { throw new ArgumentNullException(nameof(code)); } if (employeeId == Guid.Empty) { throw new ArgumentException("must not be empty", nameof(employeeId)); } var splited = code.Split(','); if (splited.Length != 2) { return(RemotingResult.Fail(1)); } if (!Guid.TryParse(splited[0], out Guid id)) { return(RemotingResult.Fail(1)); } if (!Guid.TryParse(splited[1], out Guid _)) { return(RemotingResult.Fail(1)); } var originalFingerprint = await _simpleKeyValueService.CheckAndGet(SKVContainer_ScanJoinCode, id.ToString("N"), TimeSpan.FromDays(1)); if (splited[1] != originalFingerprint) { return(RemotingResult.Fail(1)); } using (var db = new ServiceDbContext(_dbOptions)) { using (var tx = db.Database.BeginTransaction()) { try { var entity = await db.Groups.Include(o => o.Members) .Where(o => o.Type == GroupType.CustomChat) .FirstOrDefaultAsync(o => o.Id == id); if (entity == null) { return(RemotingResult.Fail(2)); } if (entity.Members.Exists(o => o.EmployeeId == employeeId)) { return(RemotingResult.Fail(3)); } entity.Members.Add(new GroupMember { EmployeeId = employeeId, GroupId = id, Joined = DateTimeOffset.UtcNow, }); await db.SaveChangesAsync(); await _busMs.Publish(new GroupMembersUpdated { Id = entity.Id }); await _busMs.Publish(new GroupNotified { Id = entity.Id, Notifies = new List <EventNotifyDto> { new EventNotifyDto { Target = NotifyTargetType.Conversation, TargetId = entity.Id.ToString(), Created = DateTimeOffset.UtcNow, Text = employeeName, TargetCategory = (int)GroupEventNotifyType.ParticipantAdded } } }); tx.Commit(); return(RemotingResult.Success()); } catch (Exception ex) { tx.Rollback(); _logger.Error(ex, "UpdateAsync Transaction rollback"); return(RemotingResult.Fail()); } } } }
public async Task <RemotingResult> QuitAsync(GroupInput input) { if (!input.Id.HasValue) { throw new ArgumentNullException("must not be null", nameof(input.Id)); } if (!input.CurrentEmployeeId.HasValue) { throw new ArgumentNullException("must not be null", nameof(input.CurrentEmployeeId)); } using (var db = new ServiceDbContext(_dbOptions)) { using (var tx = db.Database.BeginTransaction()) { try { var entity = await db.Groups .Include(o => o.Members) .FirstOrDefaultAsync(o => o.Id == input.Id); if (entity == null) { return(RemotingResult.Fail()); } var member = entity.Members.Find(o => o.EmployeeId == input.CurrentEmployeeId.Value); if (member == null) { return(RemotingResult.Fail()); } if (member.IsOwner) { return(RemotingResult.Fail(FailedCodes.Group_OwnerCannotQuit)); } entity.Members.Remove(member); await db.SaveChangesAsync(); await _busMs.Publish(new GroupMembersUpdated { Id = entity.Id }); await _busMs.Publish(new GroupNotified { Id = entity.Id, Notifies = new List <EventNotifyDto> { new EventNotifyDto { Target = NotifyTargetType.Conversation, TargetId = input.Id.ToString(), Created = DateTimeOffset.UtcNow, Text = input.CurrentEmployeeName, TargetCategory = (int)GroupEventNotifyType.ParticipantQuited } } }); tx.Commit(); return(RemotingResult.Success()); } catch (Exception ex) { tx.Rollback(); _logger.Error(ex, "QuitAsync Transaction rollback"); return(RemotingResult.Fail()); } } } }
public async Task <RemotingResult> DeleteAsync(GroupInput input) { if (!input.Id.HasValue) { throw new ArgumentNullException("must not be null", nameof(input.Id)); } if (input.CurrentUserId == Guid.Empty) { throw new ArgumentNullException("must not be empty", nameof(input.CurrentUserId)); } using (var db = new ServiceDbContext(_dbOptions)) { using (var tx = db.Database.BeginTransaction()) { try { var entity = await db.Groups.FirstOrDefaultAsync(o => o.Id == input.Id); if (entity == null) { return(RemotingResult.Fail()); } if (entity.CreatedBy != input.CurrentUserId) { return(RemotingResult.Fail(FailedCodes.Group_NotCreatedBy)); } db.Groups.Remove(entity); await db.SaveChangesAsync(); await _busMs.Publish(new GroupDeleted { Id = entity.Id }); await _busMs.Publish(new GroupNotified { Id = entity.Id, Notifies = new List <EventNotifyDto> { new EventNotifyDto { Target = NotifyTargetType.Conversation, TargetId = entity.Id.ToString(), Created = DateTimeOffset.UtcNow, Text = entity.Name, TargetCategory = (int)GroupEventNotifyType.GroupDismissed } } }); tx.Commit(); return(RemotingResult.Success()); } catch (Exception ex) { tx.Rollback(); _logger.Error(ex, "DeleteAsync Transaction rollback"); return(RemotingResult.Fail()); } } } }
public async Task <RemotingResult> UpdateAsync(GroupInput input) { if (!input.Id.HasValue) { throw new ArgumentNullException("must not be null", nameof(input.Id)); } if (input.CurrentUserId == Guid.Empty) { throw new ArgumentNullException("must not be empty", nameof(input.CurrentUserId)); } if (input.RemovingMemberIds?.Count > 0) { //如果需要删除成员,那么就需要传递当前员工Id if (!input.CurrentEmployeeId.HasValue) { throw new ArgumentNullException("must not be null", nameof(input.CurrentEmployeeId)); } if (input.RemovingMemberIds.Contains(input.CurrentEmployeeId.Value)) { return(RemotingResult.Fail(FailedCodes.Group_CannotRemoveOwner)); } } using (var db = new ServiceDbContext(_dbOptions)) { using (var tx = db.Database.BeginTransaction()) { try { var entity = await db.Groups.FirstOrDefaultAsync(o => o.Id == input.Id); if (entity == null) { return(RemotingResult.Fail()); } if (entity.CreatedBy != input.CurrentUserId) { return(RemotingResult.Fail(FailedCodes.Group_NotCreatedBy)); } var nameUpdated = false; if (!string.IsNullOrWhiteSpace(input.Name) && entity.Name != input.Name) { entity.Name = input.Name; nameUpdated = true; } if (input.Remark != null) { entity.Remark = input.Remark; } var membersUpdated = false; if (input.AddingMemberIds?.Count > 0 || input.RemovingMemberIds?.Count > 0) { //需要编辑成员,就显式加载成员集合 await db.Entry(entity).Collection(o => o.Members).LoadAsync(); membersUpdated = true; } if (input.AddingMemberIds != null) { foreach (var employeeId in input.AddingMemberIds) { if (!entity.Members.Exists(o => o.EmployeeId == employeeId)) { entity.Members.Add(new GroupMember { EmployeeId = employeeId, GroupId = input.Id.Value, Joined = DateTimeOffset.UtcNow, }); } } } if (input.RemovingMemberIds != null) { var willRemoving = new List <GroupMember>(); foreach (var employeeId in input.RemovingMemberIds) { var m = entity.Members.Find(o => o.EmployeeId == employeeId); willRemoving.Add(m); } willRemoving.ForEach(o => entity.Members.Remove(o)); } await db.SaveChangesAsync(); if (membersUpdated) { await _busMs.Publish(new GroupMembersUpdated { Id = entity.Id }); } await SendEventNotifyForUpdateAsync(nameUpdated, input); tx.Commit(); return(RemotingResult.Success()); } catch (Exception ex) { tx.Rollback(); _logger.Error(ex, "UpdateAsync Transaction rollback"); return(RemotingResult.Fail()); } } } }