示例#1
0
        public static async Task <MessageSubmission> CascadeDownloadMessageSubmissionAsync(this ApexVisualManager avm, Guid id)
        {
            MessageSubmission ToReturn = await avm.DownloadMessageSubmissionAsync(id);

            string body = await avm.DownloadMessageSubmissionBodyAsync(id);

            ToReturn.Body = body;
            return(ToReturn);
        }
示例#2
0
        public static async Task <Guid> CascadeUploadMessageSubmissionAsync(this ApexVisualManager avm, MessageSubmission msg)
        {
            Guid ToUseAndReturn = Guid.NewGuid();

            //Upload the Message Submission itself to sql
            Guid id_SQL = await avm.UploadMessageSubmissionAsync(msg, ToUseAndReturn);

            //Upload the body
            Guid id_BLOB = await avm.UploadMessageSubmissionBodyAsync(msg.Body, ToUseAndReturn);

            if (id_SQL != id_BLOB)
            {
                throw new Exception("The ID of the MessageSubmission in SQL is not identical to the BLOB that contains the body.");
            }

            return(id_SQL);
        }
示例#3
0
        public async Task <bool> Send(MessageSubmission messageSubmission)
        {
            Guid.TryParseExact(messageSubmission.ConversationId, "D", out var convoId);

            if (convoId == null)
            {
                return(false);
            }

            var convo = ConvoDal.GetById(convoId) as Conversation;

            using (var transaction = DbContext.Database.BeginTransaction())
            {
                try
                {
                    if (convo != null)
                    {   // ConvoDal.Update(convoID);
                        var newMessage = new ConversationMessage
                        {
                            Message      = messageSubmission.Message,
                            User         = CurrentUser,
                            Conversation = convo
                        };

                        ConversationMessageDAL.Add(newMessage);
                        await ConversationMessageDAL.Commit();

                        ConvoDal.Update(convo);
                        convo.LatestMessage = newMessage;
                        await ConvoDal.Commit();

                        // update related entities of the convo some how?
                        // TODO next
                        // Add user to the same conversation?
                    }
                    else
                    {  // Initial Creation
                        var newConvo = new Conversation(convoId);
                        var users    = new List <User>();

                        var newMessage = new ConversationMessage
                        {
                            Message      = messageSubmission.Message,
                            User         = CurrentUser,
                            Conversation = newConvo
                        };

                        var conversationUsers = new List <ConversationUser>();

                        conversationUsers.Add(new ConversationUser
                        {
                            Conversation = newConvo,
                            User         = CurrentUser
                        });

                        foreach (var recipientId in messageSubmission.RecipientIds)
                        {
                            var user = DbContext.Users.Find(recipientId);

                            conversationUsers.Add(new ConversationUser
                            {
                                Conversation = newConvo,
                                User         = user
                            });

                            users.Add(user);
                        }

                        //Update Conversation
                        newConvo.Name = users?.Select(u => u.Name)?.Aggregate((a, b) => a + ", " + b);

                        ;
                        ConvoDal.Add(newConvo);

                        await ConvoDal.Commit();

                        foreach (var cu in conversationUsers)
                        {
                            ConversationUserDAL.Add(cu);
                            await ConversationUserDAL.Commit();
                        }

                        ConversationMessageDAL.Add(newMessage);
                        await ConversationMessageDAL.Commit();

                        // Update Convo after message is created

                        ConvoDal.Update(newConvo);
                        newConvo.LatestMessage = newMessage;

                        await ConvoDal.Commit();
                    }

                    transaction.Commit();
                }
                catch (Exception e)
                {
                    transaction.Dispose();

                    throw new ArgumentException(e.Message);
                }
            }

            return(true);
        }