Ejemplo n.º 1
0
 public Response SendMessage(string msg, string token, int conId)
 {
     if (token.Length > 0)
     {
         var user = dc.Users.SingleOrDefault(u => u.Token == token);
         if (user != null)
         {
             var data = dc.Conversations.SingleOrDefault(c => c.Id == conId);
             if (data == null)
             {
                 data = new Models.Conversation();
                 dc.Conversations.Add(data);
                 data.User1Id = user.Id;
                 data.User2Id = conId * -1;
             }
             data.Messages.Add(new Message()
             {
                 Text = msg, UserId = user.Id
             });
             dc.SaveChanges();
             return(new Response(data.Id.ToString()));
         }
     }
     return(null);
 }
Ejemplo n.º 2
0
        public ActionResult CreateConversation()
        {
            BarterBot2DbContext db2 = new BarterBot2DbContext();
            Conversation        cc  = new Models.Conversation();

            if (db2.conversations.Count() != 0)
            {
                int concount  = 0;
                int concountr = 0;

                int concount1 = db2.conversations.Count();

                foreach (Conversation cC in db2.conversations)
                {
                    if (cC.SenRevID == c.SenRevID)
                    {
                        db2.conversations.Single(s => s.SenRevID == c.SenRevID).TotalMessages++;
                    }
                    else
                    {
                        concount++;
                    }
                }

                foreach (Conversation cC in db2.conversations)
                {
                    if (cC.SenRevID == reverseId)
                    {
                        db2.conversations.Single(s => s.SenRevID == reverseId).TotalMessages++;
                    }
                    else
                    {
                        concountr++;
                    }
                }

                if (concount == concount1 && concountr == concount1)
                {
                    c.TotalMessages = 1;
                    cc = c;
                    db2.conversations.Add(cc);
                }

                db2.SaveChanges();
            }
            else
            {
                c.TotalMessages = 1;
                cc = c;
                db2.conversations.Add(cc);
                db2.SaveChanges();
            }

            return(RedirectToAction("LoggedIn", "Account"));
        }
        public async Task <ConversationViewModel> CreateConversationAsync(ConversationRequest request)
        {
            var conversation = new Models.Conversation()
            {
                Id = Guid.NewGuid()
            };

            var result = new ConversationViewModel()
            {
                Id = conversation.Id,
            };
            var currentUsername = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;
            var currentUser     = await _userManager.FindByNameAsync(currentUsername);

            if (!request.IsAutoCreate)
            {
                conversation.Name = request.Name;
                result.Name       = request.Name;
            }
            else
            {
                var participant = await _userManager.FindByIdAsync(request.ParticipantIds.FirstOrDefault().ToString());

                result.Name = $"{participant.FirstName} {participant.LastName}";
            }

            _conversationRepository.Add(conversation);
            request.ParticipantIds.Add(currentUser.Id);
            foreach (var participantId in request.ParticipantIds)
            {
                var participant = new Participant()
                {
                    Id             = Guid.NewGuid(),
                    ConversationId = conversation.Id,
                    UserId         = participantId
                };
                _participantRepository.Add(participant);
            }

            await _unitOfWork.SaveChangeAsync();

            return(result);
        }
Ejemplo n.º 4
0
        public ActionResult Conversation(int Rid)
        {
            BarterBot2.Models.User r = db.users.Single(d => d.UserID == Rid);
            HomeController.data3.Add(r.FirstName);

            int sender = Convert.ToInt32(Session["UserId"]);

            Conversation c1 = new Models.Conversation();

            c1.SenderID   = sender;
            c1.ReceiverID = Rid;
            string SR  = sender.ToString() + Rid.ToString();
            string SR2 = Rid.ToString() + sender.ToString();

            reverseId   = Convert.ToInt32(SR2);
            c1.SenRevID = Convert.ToInt32(SR);
            c           = c1;

            return(RedirectToAction("create"));
        }