public async Task <Unit> Handle(StartConversationCommand request, CancellationToken cancellationToken) { var toss = await _session.LoadAsync <TossEntity>(_ravenDbIdUtil.GetRavenDbIdFromUrlId <TossEntity>(request.TossId)); // we fail silently, there is no point in managing error when user hacked the desired behavior, this might change in the future if (toss == null) { throw new InvalidOperationException($"Toss does not exists : {request.TossId}"); } var currentUser = await mediator.Send(new CurrentUserQuery()); if (currentUser.Id == toss.UserId) { throw new InvalidOperationException($"Cannot create conversation when toss creator : {request.TossId}"); } if (await _session.Query <TossConversation>().AnyAsync(c => c.CreatorUserId == currentUser.Id && c.TossId == toss.Id)) { throw new InvalidOperationException($"Conversation already exists. User : {currentUser}, Toss: {toss.Id}"); } TossConversation conversation = new TossConversation(toss, currentUser); await _session.StoreAsync(conversation); await mediator.Publish(new ConversationStarted(conversation)); return(Unit.Value); }
public async Task <TossConversationQueryResult> Handle(TossConversationQuery request, CancellationToken cancellationToken) { var currentUser = await mediator.Send(new CurrentUserQuery()); var tossId = _ravenDbIdUtil.GetRavenDbIdFromUrlId <TossEntity>(request.TossId); var toss = await _session.LoadAsync <TossEntity>(tossId); IQueryable <TossConversation> queryable = _session.Query <TossConversation>() .Where(c => c.TossId == tossId); if (!toss.IsCreator(currentUser)) { queryable = queryable.Where(c => c.CreatorUserId == currentUser.Id); } var items = await(from c in queryable let u = RavenQuery.Load <ApplicationUser>(c.CreatorUserId) select new TossConversationQueryResultItem() { Id = c.Id, CreatorUserName = u.UserName, MessageCount = c.Messages.Count() }).ToListAsync(); items.ForEach(c => c.Id = _ravenDbIdUtil.GetUrlId(c.Id)); return(new TossConversationQueryResult() { Conversations = items, TossCreatorUserId = toss.UserId }); }
public async Task <TossDetail> Handle(TossDetailQuery request, CancellationToken cancellationToken) { var t = (await _session.LoadAsync <TossEntity>(ravenDBIdUtil.GetRavenDbIdFromUrlId <TossEntity>(request.TossId))); if (t == null) { return(null); } return(new TossDetail() { Content = t.Content, CreatedOn = t.CreatedOn, UserName = t.UserName, Id = t.Id, Hashtags = t.Tags }); }
public async Task <MessageInConversationQueryResult> Handle(MessageInConversationQuery request, CancellationToken cancellationToken) { string id = _ravenDbIdUtil.GetRavenDbIdFromUrlId <TossConversation>(request.ConversationId); var res = await(from c in _session.Query <TossConversation>() where c.Id == id //let u = select new MessageInConversationQueryResult() { Messages = c.Messages .Select( m => new MessageInConversationQueryResultItem() { Content = m.Content, CreatedOn = m.CreatedOn, UserName = RavenQuery.Load <ApplicationUser>(m.UserId).UserName }) .ToList() } ).FirstAsync(); return(res); }
public async Task <Unit> Handle(SendMessageInConversationCommand request, CancellationToken cancellationToken) { var conversation = await _session.LoadAsync <TossConversation>(_ravenDbIdUtil.GetRavenDbIdFromUrlId <TossConversation>(request.ConversationId)); // we fail silently, there is no point in managing error when user hacked the desired behavior, this might change in the future if (conversation == null) { throw new ApplicationException($"Conversation {conversation.Id} does not exists"); } var toss = await _session.LoadAsync <TossEntity>(conversation.TossId); var currentUser = await mediator.Send(new CurrentUserQuery()); //only conversation creator and toss creator can participate in a conversation if (conversation.CanSendMessage(toss, currentUser)) { throw new ApplicationException($"{currentUser.Id} can't send message in {conversation.Id}"); } var msg = conversation.AddMessage(currentUser, request.Message, this.now.Get()); await mediator.Publish(new ConversationMessageSended(conversation, msg)); return(Unit.Value); }