コード例 #1
0
        public async Task <ConsultancyMessageDTO> SendMultiMediaMessageAsync(SendMessageDTO model, IFormFile file)
        {
            ConsultancyMessageDTO result = null;

            var strategy = _dbContext.Database.CreateExecutionStrategy();

            await strategy.ExecuteAsync(async() =>
            {
                using (var transaction = _dbContext.Database.BeginTransaction())
                {
                    var msg = new ConsultancyMessage
                    {
                        ConsultancyId   = model.ConsultancyId,
                        ServiceSupplyId = model.ServiceSupplyId,
                        PersonId        = model.PersonId,
                        CreatedAt       = DateTime.Now,
                        Content         = "",
                        Status          = ConsultancyMessageStatus.NEW,
                        Sender          = model.Sender,
                        Type            = model.Type,
                    };

                    await _dbContext.ConsultancyMessages.AddAsync(msg);

                    await _dbContext.SaveChangesAsync();

                    if (model.Type == ConsultancyMessageType.PHOTO)
                    {
                        var(newName, thumbName, dirPath, baseUrl) = _uploadService.GenerateConsultancyMessageImageName(model.ConsultancyId, msg.Id, file);

                        var url = $"{baseUrl}/{newName}";

                        var thumbUrl = $"{baseUrl}/{thumbName}";

                        msg.Content = $"{url},{thumbUrl}";

                        _dbContext.ConsultancyMessages.Attach(msg);

                        _dbContext.Entry(msg).State = EntityState.Modified;

                        await _dbContext.SaveChangesAsync();

                        await _uploadService.UploadConsultancyMessageImageAsync(file, dirPath, newName, thumbName);
                    }
                    else if (model.Type == ConsultancyMessageType.VOICE)
                    {
                        var(newName, dirPath, baseUrl) = _uploadService.GenerateConsultancyMessageVoiceName(model.ConsultancyId, msg.Id, file);

                        var url = $"{baseUrl}/{newName}";

                        msg.Content = $"{url}";

                        _dbContext.ConsultancyMessages.Attach(msg);

                        _dbContext.Entry(msg).State = EntityState.Modified;

                        await _dbContext.SaveChangesAsync();

                        await _uploadService.UploadConsultancyMessageVoiceAsync(file, dirPath, newName);
                    }

                    result = new ConsultancyMessageDTO
                    {
                        Id = msg.Id,
                        SenderReceiverType = msg.Sender == ConsultancyMessageSender.CUSTOMER ? MessageSenderReceiverType.SENT : MessageSenderReceiverType.RECEIVED,
                        Message            = msg.Content,
                        Time = msg.CreatedAt.ToString("HH:mm"),
                        Type = msg.Type
                    };

                    transaction.Commit();
                }
            });

            await NotifyMessageReceiverAsync(model, result);

            return(result ?? throw new AwroNoreException(""));
        }