Beispiel #1
0
        public async Task <MessageDto> Handle(SendMessageToGroupCommand request, CancellationToken cancellationToken)
        {
            var player = await _securityDataProvider.GetCurrentLoggedInPlayer();

            var groupMember =
                await _repository.GetSingleBySpec(
                    new GetGroupMemberShipSpecification(player.Id, request.GroupId));

            if (groupMember == null)
            {
                throw new InvalidOperationException("Can not send message to a group which the player has not joined yet");
            }

            var messageHistory = new MessageHistory()
            {
                PlayerId = player.Id,
                GroupId  = groupMember.GroupId,
                Message  = request.Message,
                Created  = DateTime.UtcNow,
                Modified = DateTime.UtcNow
            };
            await _repository.Add(messageHistory);

            return(new MessageDto()
            {
                PlayerId = messageHistory.PlayerId,
                PlayerName = player.UserName,
                GroupId = messageHistory.GroupId,
                Message = request.Message,
                SentOn = messageHistory.Created
            });
        }
        public async Task <Unit> Handle(JoinGroupCommand request, CancellationToken cancellationToken)
        {
            var player = await _securityDataProvider.GetCurrentLoggedInPlayer();

            var group = await _repository.GetSingleBySpec(new GroupSearchSpecification(request.GroupId));

            if (group.PlayerGroupMaps.Any(x => x.PlayerId == player.Id))
            {
                throw new InvalidOperationException("Player already joined the group");
            }

            int maxAllowedGroupMemberCount = int.Parse(_configuration["MaxAllowedGroupMemberCount"]);

            if (group.PlayerGroupMaps.Count >= maxAllowedGroupMemberCount)
            {
                throw new InvalidOperationException($"Max group member limit exceeded. Allowed Limit: " + maxAllowedGroupMemberCount);
            }
            var playerGroupMapping = new PlayerGroupMapping
            {
                PlayerId = player.Id,
                GroupId  = group.Id,
                Created  = DateTime.UtcNow,
                Modified = DateTime.UtcNow
            };
            await _repository.Add(playerGroupMapping);

            return(Unit.Value);
        }
        public async Task <IList <GroupDto> > Handle(GetAllGroupQuery request, CancellationToken cancellationToken)
        {
            var groups = await _repository.List(new GetAllGroupSpecification());

            var player = await _securityDataProvider.GetCurrentLoggedInPlayer();

            var result = groups?.Select(x => new GroupDto
            {
                Name     = x.Name,
                GroupId  = x.Id,
                IsMember = x.PlayerGroupMaps.Any(y => y.PlayerId == player.Id)
            }).ToList();

            return(result);
        }
        public async Task <Unit> Handle(LeaveGroupCommand request, CancellationToken cancellationToken)
        {
            var player = await _securityDataProvider.GetCurrentLoggedInPlayer();

            var group = await _repository.GetSingleBySpec(new GroupSearchSpecification(request.GroupId));

            var groupMember = group.PlayerGroupMaps.SingleOrDefault(x => x.PlayerId == player.Id);

            if (groupMember == null)
            {
                throw new InvalidOperationException("Player is not member of the group");
            }

            await _repository.Delete(groupMember);

            return(Unit.Value);
        }
        public async Task <IList <MessageDto> > Handle(GetMessagesOfGroupQuery request, CancellationToken cancellationToken)
        {
            var player = await _securityDataProvider.GetCurrentLoggedInPlayer();

            var memberShip = await _repository.GetSingleBySpec(new GetGroupMemberShipSpecification(player.Id, request.GroupId));

            if (memberShip == null)
            {
                throw new InvalidOperationException("Can not retrieve messages of a group which the player has not joined yet");
            }
            var allMessageHistory =
                (await _repository.List(new GetAllMessageHistorySpecification(request.GroupId))).Select(x => new MessageDto
            {
                PlayerId   = x.PlayerId,
                PlayerName = x.Player.UserName,
                GroupId    = x.GroupId,
                Message    = x.Message,
                SentOn     = x.Created
            }).OrderBy(x => x.SentOn).ToList();

            return(allMessageHistory);
        }