Exemplo n.º 1
0
        public void Send(Guid channelId, string message)
        {
            if (message == "")
            {
                return;
            }

            if (!CurrentUserMemberOfChannel(channelId))
            {
                return;
            }

            CommunicationChannel communicationChannel = db.CommunicationChannels.Where(ch => ch.Id == channelId).Include(ch => ch.ApplicationUser).First();

            if (communicationChannel.Open == false)
            {
                return;
            }

            var user = db.Users.Where(u => u.UserName == Context.User.Identity.Name).First();

            CommunicationChannelMessage communicationChannelMessage = new CommunicationChannelMessage()
            {
                ApplicationUserId = user.Id,
                ChannelId         = channelId,
                Created           = DateTime.Now,
                Message           = message,
                Id = Guid.NewGuid()
            };

            db.CommunicationChannelMessages.Add(communicationChannelMessage);
            db.SaveChanges();

            Clients.Group(channelId.ToString()).addNewMessage(user.Name, message);
        }
Exemplo n.º 2
0
        public async Task <CommunicationChannelMessageDto> Handle(CreateCommunicationChannelMessageCommand request, CancellationToken cancellationToken)
        {
            // Pokusím se získat uživatele podle Id.
            // Ověřím si, zda-li uživatel existuje, pokud neexistuje, vyhodím výjimku.
            var user = await _userRepository.GetByIdAsync(request.UserId)
                       ?? throw new NotFoundException(nameof(User), request.UserId);

            // Ověřím si, zda-li komunikační kanál skutečně existuje.
            var channel = await _communicationChannelRepository.GetByIdAsync(request.ChannelId)
                          ?? throw new NotFoundException(nameof(Channel), request.ChannelId);

            // TODO: ověřit, že má na akci právo.

            // Ověřím si, zda-li je uživatel součástí komunikačního kanálu.
            if (!channel.Users.Contains(user))
            {
                throw new NotFoundException(
                          $"User with Id {request.UserId} is not a part of communication channel with Id {request.ChannelId}."
                          );
            }

            // Vytvořím entitu naplněnou daty z příkazu.
            var entity = new CommunicationChannelMessage()
            {
                User      = user,
                UserId    = request.UserId,
                Channel   = channel,
                ChannelId = request.ChannelId,
                Content   = request.Content,
                Type      = request.Type
            };

            // Přidám záznam do datového zdroje a uložím.
            await _communicationChannelMessageRepository.AddOrUpdateAsync(entity, cancellationToken);

            await _unitOfWork.SaveChangesAsync(cancellationToken);

            // Vrátím Id vytvořeného záznamu.
            return(_mapper.Map <CommunicationChannelMessageDto>(entity));
        }