Example #1
0
        public ActionResult GetChatUpdates(FormCollection collection)
        {
            chats chat = ChatService.GetChatById(Convert.ToInt32(collection["chatId"]));
            // Select all messages with that fulfill the requirement -> id > lastId | and belong to the chat that has the id == chatId
            List <messages> retMessages = ChatService.GetMessages(chat).Where(x => x.C_ID > Convert.ToInt32(collection["lastId"])).ToList();


            int highestId = (from x in retMessages
                             orderby x.C_ID descending
                             select x.C_ID).FirstOrDefault();
            // Create a new anonymous object with all message info to turn into a json string
            var retObj = new
            {
                id = (from message in retMessages
                      select message.C_ID).ToList(),
                sender = (from message in retMessages
                          select message.AspNetUsers.NickName).ToList(),
                message = (from message in retMessages
                           select message.message).ToList(),
                timeStamp = (from message in retMessages
                             select message.time_stamp).ToList(),
                highest = highestId
            };

            return(Json(retObj));
        }
Example #2
0
        public ActionResult GetAllMessagesFromChat(FormCollection collection)
        {
            // Here we get all messeages from the chat with the id chatId
            chats           chat     = ChatService.GetChatById(Convert.ToInt32(collection["chatId"]));
            List <messages> messages = ChatService.GetMessages(chat);

            // Get highest value
            int?highestId = (from x in messages
                             orderby x.C_ID descending
                             select x.C_ID).FirstOrDefault();

            if (!highestId.HasValue)
            {
                highestId = 1;
            }

            // Create an anonymous object to return as a json string
            var retObj = new
            {
                id = (from message in messages
                      select message.C_ID).ToList(),
                sender = (from message in messages
                          select message.AspNetUsers.NickName).ToList(),
                message = (from message in messages
                           select message.message).ToList(),
                timeStamp = (from message in messages
                             select message.time_stamp).ToList(),
                highestId = highestId
            };

            return(Json(retObj));
        }
Example #3
0
        /* <summary>Gets chat users by ID</summary>
         * <param name="chat">Chat model</param>
         * <returns>list of chaat users</returns>
         * <author>Janus</author>
         */
        static public List <AspNetUsers> GetChatUsers(chats chat)
        {
            var db        = new VERK2015_H17Entities1();
            var chatUsers = (from x in db.chat_members.Where(y => y.FK_chat_members_chat == chat.C_ID)
                             select x.AspNetUsers).ToList();

            return(chatUsers);
        }
Example #4
0
        /* <summary>Gets messeages by ID</summary>
         * <param name="chat">Chat model</param>
         * <returns>list of messages</returns>
         * <author>Janus</author>
         */
        static public List <messages> GetMessages(chats chat)
        {
            var db       = new VERK2015_H17Entities1();
            var messages = (from x in db.messages.Where(y => y.FK_messages_chat_id == chat.C_ID)
                            select x).ToList();

            return(messages);
        }
Example #5
0
        /* <summary></summary>
         * <param name="ID"></param>
         * <returns></returns>
         * <author></author>
         */
        public static List <chats> SearchChatByName(chats chat)
        {
            var db      = new VERK2015_H17Entities1();
            var chatRes = (from x in db.chats.Where(y => y.chat_name.Contains(chat.chat_name))
                           select x).ToList();

            return(chatRes);
        }
Example #6
0
        static public void RenameChat(chats chat)
        {
            var db      = new VERK2015_H17Entities1();
            var getChat = (from x in db.chats.Where(y => y.C_ID == chat.C_ID)
                           select x).SingleOrDefault();

            getChat.chat_name = chat.chat_name;
            db.SaveChanges();
        }
Example #7
0
        public ActionResult Create(FormCollection fc)
        {
            // Get both users as AspNetUsers, as required by the CreateChat function in ChatService
            AspNetUsers user   = UserService.GetUserByEmail(User.Identity.Name);
            AspNetUsers friend = UserService.GetUserById(fc["friendId"]);

            // Create a chat with the users and we get the new chat returned to us
            chats chat = ChatService.CreateChat(user, friend);

            // Return the chatId as an Json object
            return(Json(new
            {
                chatId = chat.C_ID
            }));
        }
Example #8
0
        public ActionResult Rename(FormCollection fc)
        {
            chats chat = new chats();

            chat.chat_name = fc["newName"];
            chat.C_ID      = Convert.ToInt32(fc["chatId"]);

            try
            {
                ChatService.RenameChat(chat);
            }
            catch (Exception)
            {
            }
            return(Json(""));
        }
Example #9
0
        /* <summary>Adds user to chat</summary>
         * <param name="chat">The chat we add the user too</param>
         * <param name="user">The user we add to the chat</param>
         * <returns>nothing</returns>
         * <author>Janus</author>
         */
        static public void AddChatUsers(chats chat, AspNetUsers user)
        {
            var db = new VERK2015_H17Entities1();

            List <chat_members> allChatMemberEntries = GetAllChatMemberEntries();

            bool addUser = (from isMember in allChatMemberEntries
                            where isMember.FK_chat_members_user == user.Id && isMember.FK_chat_members_chat == chat.C_ID
                            select false).DefaultIfEmpty(true).Single();

            if (addUser)
            {
                chat_members member = new chat_members();
                member.FK_chat_members_chat = chat.C_ID;
                member.FK_chat_members_user = user.Id;
                db.chat_members.Add(member);
                db.SaveChanges();
            }
        }
Example #10
0
        public ActionResult GetChatUsers(FormCollection collection)
        {
            int   id   = Convert.ToInt32(collection["chatId"]);
            chats chat = ChatService.GetChatById(id);

            //Get the users belonging to the chat we querried above
            List <AspNetUsers> chatUsers = ChatService.GetChatUsers(chat);

            // Create a anonymous object and return it as a json string
            var retObj = new
            {
                userId = (from user in chatUsers
                          select user.Id).ToList(),
                userName = (from user in chatUsers
                            select user.NickName).ToList(),
                profileImage = (from user in chatUsers
                                select user.profile_image).ToList()
            };

            return(Json(retObj));
        }
Example #11
0
        static public chats CreateChat(AspNetUsers user, AspNetUsers friend)
        {
            var db = new VERK2015_H17Entities1();

            chats newChat = new chats();

            newChat.chat_name = friend.NickName;
            db.chats.Add(newChat);

            // Add User
            //public string FK_chat_members_user { get; set; }
            //public int FK_chat_members_chat { get; set; }
            db.chat_members.Add(new chat_members {
                FK_chat_members_user = user.Id, FK_chat_members_chat = newChat.C_ID
            });
            db.chat_members.Add(new chat_members {
                FK_chat_members_user = friend.Id, FK_chat_members_chat = newChat.C_ID
            });

            db.SaveChanges();
            return(newChat);
        }