Ejemplo n.º 1
0
        public async Task <ChatMessageResult> SendFile([FromBody] ChatMessageFileDto file)
        {
            var chatRoom = await _chatRoomRepository.GetAsync(file.chatRoomId);

            if (chatRoom == null)
            {
                var messageDto = new MessageDto()
                {
                    account    = file.account,
                    chatRoomId = file.chatRoomId,
                    message    = ""
                };
                await _chatMessageWriteCommand.CreateForEmptyRoomAsync(file.profileId, messageDto);

                return(await _chatMessageFileWriteCommand.CreateFile(file));
            }
            return(await _chatMessageFileWriteCommand.CreateFile(file));
        }
        /// <summary>
        /// Функция создания сообщения
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task <ChatMessageResult> CreateFile(ChatMessageFileDto file)
        {
            var user = await _userRepository.GetWithId(file.profileId);

            var userTo = await _userRepository.GetProfileForUserName(file.account);

            var message = new ChatMessage
            {
                ChatRoomId = file.chatRoomId,
                DateTime   = DateTime.Now,
                Message    = "",
                UserId     = user.Id
            };
            var fileBase64String = Convert.FromBase64String(file.fileBinary);
            var chatMessageFile  = new ChatMessageFile
            {
                ChatMessageId = message.Id,
                FileBinary    = fileBase64String,
                fileName      = file.fileName
            };
            await _chatMessageRepository.CreateAsync(message);

            await _chatMessageFileRepository.Create(chatMessageFile);

            await _domainContext.SaveChangesAsync();

            return(new ChatMessageResult
            {
                DateTime = message.DateTime,
                fileId = chatMessageFile.Id,
                fileName = chatMessageFile.fileName,
                IsFile = true,
                Message = "",
                profileId = file.profileId,
                profileTo = userTo.ProfileId,
            });
        }