public IHttpActionResult AddMessage(MessageModel Model)
        {
            long Id = 0;

            try
            {
                Message message = new Message
                {
                    Id          = Model.Id,
                    ThreadId    = Model.ThreadId,
                    MessageText = Model.MessageText,
                    SenderId    = Model.SenderId,
                    RecieverId  = Model.RecieverId,
                    Status      = (int)Constant.MessageStatus.Initial,
                    Time        = DateTime.Now.ToUniversalTime()
                };

                using (AppDBContext context = new AppDBContext())
                {
                    var userRepo  = new UserRepository(context);
                    var sender    = userRepo.GetByUserId(message.SenderId);
                    var recipient = userRepo.GetByUserId(message.RecieverId);

                    if (message.ThreadId == 0)
                    {
                        long agentId = 0;
                        long buyerId = 0;
                        if (sender.UserType == Constant.UserType.BUYER)
                        {
                            buyerId = sender.Id;
                            agentId = recipient.Id;
                        }
                        else
                        {
                            buyerId = recipient.Id;
                            agentId = sender.Id;
                            new AgentServiceRequestRepository(context).UpdateResponseTime(Model.RequestId, agentId);
                        }

                        var existingthread =
                            new MessageThreadRepository(context).GetByAgentId(agentId).Where(
                                t => t.RequestId == Model.RequestId && t.BuyerId == buyerId).FirstOrDefault();

                        if (existingthread == null)
                        {
                            MessageThread thread = new MessageThread();
                            thread.AgentId     = agentId;
                            thread.BuyerId     = buyerId;
                            thread.RequestId   = Model.RequestId;
                            thread.CreatedBy   = Model.SenderId;
                            thread.CreatedTime = DateTime.Now.ToUniversalTime();

                            var  repo = new MessageThreadRepository(context);
                            long tid  = repo.Add(thread);
                            message.ThreadId = tid;
                        }
                        else
                        {
                            message.ThreadId = existingthread.Id;
                        }
                    }

                    if (sender.UserType == Constant.UserType.BUYER)
                    {
                        new ServiceRequestRepository(context).UpdateBuyerResponded(Model.RequestId);
                    }

                    Id = new MessageRepository(context).Add(message);
                    new NotificationRepository(context).Add(
                        recipient.Id,
                        (int)Constant.NotificationType.Message,
                        message.ThreadId, ConfigurationHelper.NOTIFICATION_TITLE,
                        Constant.Notification.NEW_MESSAGE_TEXT);
                }
            }
            catch (Exception ex)
            {
                Logger.Log(typeof(MessageController), ex.Message + ex.StackTrace, LogType.ERROR);
                InternalServerError(ex);
            }

            return(Ok(Id));
        }
        private void AddMessage(MessageModel Model, AppDBContext context)
        {
            long Id = 0;

            try
            {
                Message message = new Message
                {
                    Id          = Model.Id,
                    ThreadId    = Model.ThreadId,
                    MessageText = Model.MessageText,
                    SenderId    = Model.SenderId,
                    RecieverId  = Model.RecieverId,
                    Status      = (int)Constant.MessageStatus.Initial,
                    QuotationId = Model.QuotationId,
                    Time        = DateTime.Now.ToUniversalTime()
                };

                var userRepo  = new UserRepository(context);
                var sender    = userRepo.GetByUserId(message.SenderId);
                var recipient = userRepo.GetByUserId(message.RecieverId);

                if (message.ThreadId == 0)
                {
                    long agentId = 0;
                    long buyerId = 0;
                    if (sender.UserType == Constant.UserType.BUYER)
                    {
                        buyerId = sender.Id;
                        agentId = recipient.Id;
                    }
                    else
                    {
                        buyerId = recipient.Id;
                        agentId = sender.Id;
                    }

                    var existingthread =
                        new MessageThreadRepository(context).GetByAgentId(agentId).Where(
                            t => t.RequestId == Model.RequestId && t.BuyerId == buyerId).FirstOrDefault();

                    if (existingthread == null)
                    {
                        MessageThread thread = new MessageThread();
                        thread.AgentId     = agentId;
                        thread.BuyerId     = buyerId;
                        thread.RequestId   = Model.RequestId;
                        thread.CreatedBy   = Model.SenderId;
                        thread.CreatedTime = DateTime.Now.ToUniversalTime();

                        var  repo = new MessageThreadRepository(context);
                        long tid  = repo.Add(thread);
                        message.ThreadId = tid;
                    }
                    else
                    {
                        message.ThreadId = existingthread.Id;
                    }
                }

                Id = new MessageRepository(context).Add(message);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }