Esempio n. 1
0
        public string lastMessage(int id)
        {
            string lastmsg = "";

            using (var context = new ChatAPIContext())
            {
                try
                {
                    var msges = (from msg in context.Messages
                                 .Include(s => s.ChatRoom)
                                 where msg.ChatRoom.ChatRoomId == id
                                 select msg.text).ToList().Last();
                    if (msges != null)
                    {
                        return(msges);
                    }
                }
                catch
                {
                }


                return(null);
            }
        }
Esempio n. 2
0
 public List <User> Search(string name)
 {
     using (var context = new ChatAPIContext())
     {
         return(context.Users.Where(x => x.FullName == name).ToList());
     }
 }
Esempio n. 3
0
 //public int getId(string email)
 //{
 //    using(var context = new ChatAPIContext())
 //    {
 //        return context.Users.Single(x => x.Email == email).UserId;
 //    }
 //}
 public User userData(string email)
 {
     using (var context = new ChatAPIContext())
     {
         return(context.Users.Single(x => x.Email == email));
     }
 }
Esempio n. 4
0
        public void AddMessage(MessageModel message, int roomId)
        {
            using (var context = new ChatAPIContext())
            {
                Message msg = new Message();
                msg.text        = message.Message;
                msg.MessageTime = message.DateSent;
                // msg.To = context.Users.Single(x =>x.UserId==message.To);
                ChatRoom room = new ChatRoom();
                room = context.ChatRoom.Single(x => x.ChatRoomId == roomId);
                room = (from chatroom in context.ChatRoom
                        .Include(a => a.User1)
                        .Include(a => a.User2)
                        where chatroom.ChatRoomId == roomId
                        select chatroom).First();
                if (!(room.User1.UserId == message.From))
                {
                    msg.To = room.User1;
                }
                else
                {
                    msg.To = room.User2;
                }

                msg.from     = context.Users.Single(x => x.UserId == message.From);
                msg.ChatRoom = context.ChatRoom.Single(x => x.ChatRoomId == roomId);
                context.Messages.Add(msg);
                context.SaveChanges();
            }
        }
Esempio n. 5
0
//############################################################################################################################
        public List <User> FriendList(string email)
        {
            List <User> a    = new List <User>();
            List <User> b    = new List <User>();
            User        user = new User();

            try
            {
                using (var context = new ChatAPIContext())
                {
                    user = context.Users.Single(u => u.Email == email);

                    a = (from f in context.Friend
                         .Include(s => s.User2)
                         where user.UserId == f.User1.UserId && f.IsFriend == true
                         select f.User2
                         ).ToList();
                    b = (from f in context.Friend
                         .Include(s => s.User1)
                         where user.UserId == f.User2.UserId && f.IsFriend == true
                         select f.User1
                         ).ToList();
                    a.AddRange(b);
                    return(a);
                }
            }
            catch
            {
                return(null);
            }
        }
Esempio n. 6
0
 public List <User> AllUsers(int id)
 {
     using (var context = new ChatAPIContext())
     {
         return((from u in context.Users
                 where u.UserId != id
                 select u).ToList());
     }
 }
Esempio n. 7
0
 //############################################################################################################################
 public List <User> RequestList(int id)
 {
     using (var context = new ChatAPIContext())
     {
         return((from f in context.Friend
                 .Include(x => x.User1)
                 .Include(x => x.User2)
                 where f.User2.UserId == id && f.IsFriend == false
                 select f.User1).ToList());
     }
 }
Esempio n. 8
0
 public Message lastRcvMsg(int roomId, int myId)
 {
     using (var context = new ChatAPIContext())
     {
         return((from msg in context.Messages
                 .Include(x => x.ChatRoom)
                 .Include(x => x.To)
                 .Include(x => x.@from)
                 where msg.ChatRoom.ChatRoomId == roomId && msg.To.UserId == myId
                 select msg).ToList().Last());
     }
 }
Esempio n. 9
0
//############################################################################################################################
        public void SendRequest(string myEmail, string fEmail)
        {
            using (var context = new ChatAPIContext())
            {
                Friend friend = new Friend();
                friend.IsFriend = false;
                friend.User1    = context.Users.Single(u => u.Email == myEmail);
                friend.User2    = context.Users.Single(u => u.Email == fEmail);
                context.Friend.Add(friend);
                context.SaveChanges();
            }
        }
Esempio n. 10
0
//############################################################################################################################

        public void AcceptRequest(int myid, string fEmail)
        {
            using (var context = new ChatAPIContext())
            {
                var friend = context.Friend.Single(a => a.User2.UserId == myid && a.User1.Email == fEmail);
                friend.IsFriend = true;
                ChatRoom chatRoom = new ChatRoom();
                chatRoom.User2 = context.Users.Single(u => u.UserId == myid);
                chatRoom.User1 = context.Users.Single(u => u.Email == fEmail);
                context.ChatRoom.Add(chatRoom);
                context.SaveChanges();
            }
        }
Esempio n. 11
0
 public bool ExistingUser(string email)
 {
     using (var context = new ChatAPIContext())
     {
         foreach (var item in context.Users)
         {
             if (item.Email.Equals(email))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Esempio n. 12
0
        public IHttpActionResult Get(int id)
        {
            using (var context = new ChatAPIContext())
            {
                var user = (from u in context.Users
                            where  u.UserId == id
                            select u).SingleOrDefault();
                //Delete it from memory
                context.Users.Remove(user);
                //Save to database
                context.SaveChanges();
            }

            return(Ok());
        }
Esempio n. 13
0
        //######### Update Name ###################
        public IHttpActionResult Post(int id, string newName)
        {
            using (var context = new ChatAPIContext())
            {
                var user = (from u in context.Users
                            where u.UserId == id
                            select u).SingleOrDefault();
                if (user != null)
                {
                    user.FullName = newName;
                    context.SaveChanges();
                }
            }

            return(Ok());
        }
Esempio n. 14
0
//############################################################################################################################3
        public User friendData(int chatroomid, int myid)
        {
            using (var context = new ChatAPIContext())
            {
                var chatroom = (from c in context.ChatRoom
                                .Include(x => x.User1)
                                .Include(x => x.User2)
                                where c.ChatRoomId == chatroomid
                                select c).Single();
                if (chatroom.User1.UserId == myid)
                {
                    return(chatroom.User2);
                }
                return(chatroom.User1);
            }
        }
Esempio n. 15
0
 public List <Message> GetMessages(int roomid)
 {
     try
     {
         using (var context = new ChatAPIContext())
         {
             return((from msg in context.Messages
                     .Include(s => s.ChatRoom)
                     .Include(s => s.@from)
                     .Include(s => s.To)
                     where msg.ChatRoom.ChatRoomId == roomid
                     select msg).ToList());
         }
     }
     catch
     {
         return(null);
     }
 }
Esempio n. 16
0
 public IHttpActionResult Get(string email, int questionNo, string questionAns)
 {
     try
     {
         using (var context = new ChatAPIContext())
         {
             var user = (from u in context.Users
                         where u.Email == email && questionNo == u.SecurityQuestion && questionAns == u.SecurityAnswer.ToLower()
                         select u).SingleOrDefault();
             if (user != null)
             {
                 return(Json(true));
             }
         }
     }
     catch
     {
     }
     return(Json(false));
 }
Esempio n. 17
0
        public List <ChatRoom> chatRooms(int id)
        {
            using (var context = new ChatAPIContext())
            {
                List <ChatRoom> a = new List <ChatRoom>();
                List <ChatRoom> b = new List <ChatRoom>();

                a = (from chat in context.ChatRoom
                     .Include(s => s.User2)
                     where chat.User1.UserId == id
                     select chat
                     ).ToList();
                b = (from chat in context.ChatRoom
                     .Include(s => s.User1)
                     where chat.User2.UserId == id
                     select chat
                     ).ToList();
                a.AddRange(b);
                return(a);
            }
        }
Esempio n. 18
0
//############################################################################################################################


        public List <User> requestList(int myid)
        {
            List <User> a = new List <User>();
            List <User> b = new List <User>();

            using (var context = new ChatAPIContext())
            {
                a = (from f in context.Friend
                     .Include(x => x.User1)
                     .Include(x => x.User2)
                     where f.User2.UserId == myid
                     select f.User1).ToList();
                b = (from f in context.Friend
                     .Include(x => x.User1)
                     .Include(x => x.User2)
                     where f.User1.UserId == myid
                     select f.User2).ToList();
            }
            a.AddRange(b);
            return(a);
        }
Esempio n. 19
0
 public DateTime lastMsgTime(string msg, int id)
 {
     using (var context = new ChatAPIContext())
     {
         try
         {
             var time = (from m in context.Messages
                         .Include(s => s.ChatRoom)
                         where m.ChatRoom.ChatRoomId == id & m.text == msg
                         select m.MessageTime).ToList().Last();
             if (time != null)
             {
                 return(time);
             }
         }
         catch
         {
         }
         return(DateTime.Now);
     }
 }
Esempio n. 20
0
 //#################################### Update Password #########################################################################
 public IHttpActionResult Get(string email, string newPassword)
 {
     try
     {
         using (var context = new ChatAPIContext())
         {
             var user = (from u in context.Users
                         where u.Email == email
                         select u).SingleOrDefault();
             if (user != null)
             {
                 user.Password = newPassword;
                 context.SaveChanges();
                 return(Json(true));
             }
         }
     }
     catch
     {
     }
     return(Json(false));
 }
Esempio n. 21
0
//############################################################################################################################

        public void RejectRequest(int myid, string fEmail)
        {
            using (var context = new ChatAPIContext())
            {
                var friend = context.Friend.Single(a =>
                                                   (a.User2.UserId == myid && a.User1.Email == fEmail) ||
                                                   (a.User1.UserId == myid && a.User2.Email == fEmail));
                //try
                //{
                //    var chatroom = context.ChatRoom.Single(a =>
                //    (a.User2.UserId == myid && a.User1.Email == fEmail) ||
                //    (a.User1.UserId == myid && a.User2.Email == fEmail));
                //    context.ChatRoom.Remove(chatroom);
                //}
                //catch
                //{

                //}
                context.Friend.Remove(friend);
                context.SaveChanges();
            }
        }
Esempio n. 22
0
 public bool AddUser(UserModel model)
 {
     if (!(ExistingUser(model.Email)))
     {
         using (var context = new ChatAPIContext())
         {
             User user = new User()
             {
                 FullName         = model.FullName,
                 Email            = model.Email,
                 Password         = model.Password,
                 Gender           = model.Gender,
                 SecurityQuestion = model.SecurityQuestion,
                 SecurityAnswer   = model.SecurityAnswer,
                 ProfilePicture   = "",
             };
             context.Users.Add(user);
             context.SaveChanges();
             return(true);
         }
     }
     return(false);
 }
Esempio n. 23
0
        public string Registered(LoginModel login)
        {
            if (ExistingUser(login.Email))
            {
                using (var context = new ChatAPIContext())
                {
                    foreach (var item in context.Users)
                    {
                        if (item.Email.Equals(login.Email))
                        {
                            if (item.Password.Equals(login.Password))
                            {
                                return("Logged in Successfully");
                                //return true;
                            }
                        }
                    }
                    return("Email Or Password not matched");
                    //return false;
                }
            }

            return("Not Registered");
        }