Пример #1
0
        public GameInstanceUserMessageDto Map(GameInstanceUserMessage source)
        {
            var target = new GameInstanceUserMessageDto();

            target.DateCreated = source.DateCreated;
            target.Id          = source.Id;
            target.Text        = source.Text;
            target.UserEmail   = source.User?.Email;
            target.UserId      = source.UserId;
            return(target);
        }
        public GameInstanceUserMessage Save(GameInstanceUserMessage modelToSave)
        {
            var entity = ModelContext.GameInstanceUserMessages.SingleOrDefault(x => x.Id == modelToSave.Id);

            if (entity == null)
            {
                entity                = new GameInstanceUserMessage();
                entity.DateCreated    = DateTimeOffset.Now;
                entity.UserId         = modelToSave.UserId;
                entity.GameInstanceId = modelToSave.GameInstanceId;

                ModelContext.GameInstanceUserMessages.Add(entity);
            }

            entity.Text = modelToSave.Text;

            ModelContext.SaveChanges();
            return(entity);
        }
Пример #3
0
        public async Task SendMessage(ChatMessageDto dto)
        {
            var validationResults = GameInstanceValidator.Validate(dto);

            if (!validationResults.IsValid)
            {
                throw new Exception(string.Join(',', validationResults.Errors));
            }

            var messageToSave = new GameInstanceUserMessage();

            messageToSave.Text           = dto.Text;
            messageToSave.UserId         = RequestContext.UserId;
            messageToSave.GameInstanceId = dto.Id;
            var entity      = GameInstanceUserMessageLogic.Save(messageToSave);
            var responseDto = GameInstanceMapper.Map(entity);

            responseDto.UserEmail = RequestContext.Email;

            await Clients.Group(dto.Id.ToString()).SendAsync("ReceiveMessage", responseDto);
        }