public async Task <IActionResult> Create([Bind("Id,Message,Created,Subject,IsSeen,SenderId,ReceiverId")] Inbox inbox)
        {
            if (ModelState.IsValid)
            {
                var userId = _userManager.GetUserId(User);
                inbox.SenderId = userId;
                inbox.Created  = DateTime.Now;
                _context.Add(inbox);
                await _context.SaveChangesAsync();

                var    messageSubject = inbox.Subject;
                var    messageContent = inbox.Message;
                var    receiver       = inbox.ReceiverId;
                string devEmail       = (await _userManager.FindByIdAsync(receiver)).Email;
                string subject        = $"New Message From {(await _userManager.FindByIdAsync(userId)).FullName}";
                string message        = $"You have a new Message from {(await _userManager.FindByIdAsync(userId)).FullName} about {messageSubject}, message : {messageContent}";

                await _emailSender.SendEmailAsync(devEmail, subject, message);


                await _context.SaveChangesAsync();

                var notification = new InboxNotification(inbox.Subject, inbox.ReceiverId, inbox.SenderId, inbox.Id);
                _context.Add(notification);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ReceiverId"] = new SelectList(_context.Users, "Id", "FullName", inbox.ReceiverId);
            ViewData["SenderId"]   = new SelectList(_context.Users, "Id", "FullName", inbox.SenderId);
            return(View(inbox));
        }
Beispiel #2
0
        public async Task InboxNotificationShouldCreateDbEntities(int[] playerIds, int expectedCount)
        {
            var fix          = new Fixture();
            var notification = new InboxNotification
            {
                Sender    = fix.Create <string>(),
                Body      = fix.Create <string>(),
                Topic     = fix.Create <string>(),
                PlayerIds = playerIds
            };

            var uow = Substitute.For <IUnitOfWork>();

            await using (var context = _testDbFixture.PlayersContextFactory.Create())
            {
                var messageRepo     = Substitute.For <RepositoryAsync <Message> >(context);
                var messageLinkRepo = Substitute.For <RepositoryAsync <PlayerMessageLink> >(context);

                uow.GetRepositoryAsync <Message>().ReturnsForAnyArgs(messageRepo);
                uow.GetRepositoryAsync <PlayerMessageLink>().ReturnsForAnyArgs(messageLinkRepo);

                var handler = new InboxNotificationHandler(uow);
                await handler.HandleAsync(notification, new CorrelationContext());

                await messageLinkRepo.Received(expectedCount).AddAsync();
            }
        }
Beispiel #3
0
        public async Task HandleAsync(TeamsCreated @event, ICorrelationContext context)
        {
            int[] playerIds = @event.Teams.SelectMany(inner => inner).ToArray();

            var query = new PlayersQuery
            {
                LeagueId  = new [] { @event.LeagueId },
                PlayerId  = playerIds,
                QueryType = PlayersQueryType.Actual,
                Page      = 1,
                Size      = playerIds.Length
            };

            List <PlayerWithRateDto> playersInfo = (await _playersService.GetAsync(query)).ToList();

            int teamNumber = 1;

            foreach (var team in @event.Teams)
            {
                var body = CreateTeamAndRateBody(team, playersInfo, teamNumber);

                InboxNotification notification = InboxNotificationBuilder
                                                 .Create()
                                                 .WithReceiver(team)
                                                 .WithSender("Notification service")
                                                 .WithTopic($"Teams for tour: {@event.TourId} in league: {@event.LeagueId} are formed!")
                                                 .WithBody($"{body}")
                                                 .Build();

                await _busPublisher.SendAsync(notification, context);

                teamNumber++;
            }

            int[] registeredPlayers =
                (await _toursService.GetAsync(new RegisteredOnTourPlayers {
                TourId = @event.TourId
            }))
                .Select(dto => dto.InternalId)
                .ToArray();
            int[] unhappyPlayers = registeredPlayers.Except(playerIds).ToArray();

            if (unhappyPlayers.Length != 0)
            {
                InboxNotification notification = InboxNotificationBuilder
                                                 .Create()
                                                 .WithReceiver(unhappyPlayers)
                                                 .WithSender("Notification service")
                                                 .WithTopic($"Teams for tour: {@event.TourId} in league: {@event.LeagueId} are formed!")
                                                 .WithBody("Sorry you are out of the game. Try next time!")
                                                 .Build();

                await _busPublisher.SendAsync(notification, context);
            }
        }
        public async Task HandleAsync(TourRegistrationOpened @event, ICorrelationContext context)
        {
            IEnumerable <PlayerInternalIdDto> playersDto =
                await _leaguesService.GetLeagueJoinedPlayersAsync(new LeagueJoinedPlayersQuery(@event.LeagueId));

            int[] playerIds = playersDto.Select(p => p.InternalId).ToArray();

            InboxNotification notification = InboxNotificationBuilder
                                             .Create()
                                             .WithReceiver(playerIds)
                                             .WithSender("Notification service")
                                             .WithTopic("Tour registration opened!")
                                             .WithBody($"Tour number: {@event.TourId}. Tour date: {@event.Date}")
                                             .Build();

            await _busPublisher.SendAsync(notification, context);
        }
Beispiel #5
0
        public async Task <IActionResult> Create([Bind("Id,Message,Created,Subject,IsSeen,IsDeleted,SenderId,ReceiverId,InboxId")] Reply reply)
        {
            if (ModelState.IsValid)
            {
                var inboxId = reply.InboxId;

                var inbox = (await _context.Inbox.FirstOrDefaultAsync(i => i.Id == inboxId));

                if (inbox.Replies.FirstOrDefault(t => t.InboxId == inboxId) != null)
                {
                    var list = inbox.Replies.Where(i => i.InboxId == inboxId).ToList();
                    foreach (var item in list)
                    {
                        item.IsSeen = false;
                    }
                }

                await _context.SaveChangesAsync();

                var userId = _userManager.GetUserId(User);

                reply.SenderId = userId;
                reply.Created  = DateTime.Now;
                if ((await _context.Inbox.FirstOrDefaultAsync(r => r.Id == inboxId)).SenderId == userId)
                {
                    reply.ReceiverId = (await _context.Inbox.FirstOrDefaultAsync(r => r.Id == inboxId)).ReceiverId;
                }
                else
                {
                    reply.ReceiverId = (await _context.Inbox.FirstOrDefaultAsync(r => r.Id == inboxId)).SenderId;
                }
                _context.Add(reply);
                await _context.SaveChangesAsync();


                var    messageSubject = inbox.Subject;
                var    messageContent = inbox.Message;
                var    receiver       = inbox.ReceiverId;
                string devEmail       = (await _userManager.FindByIdAsync(receiver)).Email;
                string subject        = $"New Reply Message From {(await _userManager.FindByIdAsync(userId)).FullName}";
                string message        = $"You have a new Reply Message from {(await _userManager.FindByIdAsync(userId)).FullName} about {messageSubject}, message : {messageContent}";

                await _emailSender.SendEmailAsync(devEmail, subject, message);

                await _context.SaveChangesAsync();

                var notification = new InboxNotification(reply.Subject, reply.ReceiverId, reply.SenderId, reply.Id);
                _context.Add(notification);
                await _context.SaveChangesAsync();


                var applicationDbContext = _context.Inbox.Where(i => i.IsDeleted == false).Where(i => i.ReceiverId == userId).Include(i => i.Receiver).Include(i => i.Sender);
                var unread = (await applicationDbContext.Where(i => i.IsSeen == false).Include(i => i.Receiver).Include(i => i.Sender).ToListAsync()).Count;
                var delete = (_context.Inbox.Where(i => i.IsDeleted == true).Where(i => i.ReceiverId == userId).Include(i => i.Receiver).Include(i => i.Sender)).Count();
                var send   = (_context.Inbox.Where(i => i.IsDeleted == false).Where(i => i.SenderId == userId).Include(i => i.Receiver).Include(i => i.Sender)).Count();
                ViewData["send"]   = send;
                ViewData["unread"] = unread;
                ViewData["delete"] = delete;

                return(RedirectToAction("Index", "Inboxes"));
            }
            ViewData["InboxId"]    = new SelectList(_context.Inbox, "Id", "Id", reply.InboxId);
            ViewData["ReceiverId"] = new SelectList(_context.Users, "Id", "Id", reply.ReceiverId);
            ViewData["SenderId"]   = new SelectList(_context.Users, "Id", "Id", reply.SenderId);
            return(View(reply));
        }
Beispiel #6
0
 public async Task <IActionResult> InboxNotification([FromBody] InboxNotification command)
 {
     return(await SendAsync(command));
 }