Beispiel #1
0
        public ActionResult doimk(doimk doimk)
        {
            User currentUser = (User)HttpContext.Items["currentUser"];
            var  db          = new RealtimeChatDB();

            if (ModelState.IsValid)
            {
                if (currentUser.UserPassword == doimk.mkcu)
                {
                    var a = db.Users.Attach(currentUser);
                    a.UserPassword = doimk.mkmoi;
                    db.SaveChanges();
                    TempData["ChangeInfoorPass"] = "******";
                    return(RedirectToAction("Details", "Users"));
                }
                else
                {
                    ModelState.AddModelError("mkcu", "Wrong Old Password");
                    return(View());
                }
            }
            else
            {
                return(View());
            }
        }
        public void Send(string message)
        {
            User currentUser;
            Room currentRoom;
            int  currentRoomID = joinedUsers[Context.ConnectionId];

            using (var db = new RealtimeChatDB())
            {
                currentUser = db.Users.Where(u => u.UserName == Context.User.Identity.Name).FirstOrDefault();
                currentRoom = db.Rooms.Where(r => r.RoomID == currentRoomID).FirstOrDefault();
                var newMessage = db.ChatMessages.Create();
                newMessage.SenderID    = currentUser.UserID;
                newMessage.RoomID      = currentRoom.RoomID;
                newMessage.MessageText = message;
                db.ChatMessages.Add(newMessage);
                db.SaveChanges();

                Clients.Group("Room_" + joinedUsers[Context.ConnectionId]).broadcastMessage(
                    new SendMessageViewModel
                {
                    UserID      = currentUser.UserID,
                    UserName    = currentUser.UserName,
                    ShowName    = currentUser.ShowName,
                    MessageText = newMessage.MessageText
                }
                    );
            }
        }
Beispiel #3
0
        public ActionResult DeletePost(int id, FormCollection collection)
        {
            // TODO: Add delete logic here
            User currentUser = (User)HttpContext.Items["currentUser"];
            Room currentRoom = (Room)HttpContext.Items["currentRoom"];

            using (var db = new RealtimeChatDB())
            {
                Room deleteRoom = db.Rooms.Attach(currentRoom);
                db.Rooms.Remove(deleteRoom);
                db.SaveChanges();
            }

            TempData["successMessage"] = "Deleted successfully";
            return(RedirectToAction("Index"));
        }
Beispiel #4
0
        public ActionResult doiemail(doiemail doiemail)
        {
            User currentUser = (User)HttpContext.Items["currentUser"];
            var  db          = new RealtimeChatDB();

            if (ModelState.IsValid)
            {
                var a = db.Users.Attach(currentUser);
                a.Email    = doiemail.emailmoi;
                a.ShowName = doiemail.namemoi;
                db.SaveChanges();
                TempData["ChangeInfoorPass"] = "******";
                return(RedirectToAction("Details", "Users"));
            }
            else
            {
                return(View());
            }
        }
Beispiel #5
0
        public ActionResult LeaveRoom(int?id)
        {
            if (id != null)
            {
                int  currentUserID = ((User)HttpContext.Items["currentUser"]).UserID;
                Room joiningRoom;
                using (var db = new RealtimeChatDB())
                {
                    joiningRoom = db.Rooms.Where(r => r.RoomID == id).FirstOrDefault();
                    if (joiningRoom != null)
                    {
                        RoomMember existingMember = db.RoomMembers.Where(m => m.UserID == currentUserID && m.RoomID == joiningRoom.RoomID).FirstOrDefault();
                        if (existingMember == null)
                        {
                            ModelState.AddModelError("LeaveError", "You are not joined this room");
                        }
                        else if (existingMember.MemberLevel == 2)
                        {
                            ModelState.AddModelError("LeaveError", "You cannot leave this room because you are the person who created it");
                        }
                        else
                        {
                            db.RoomMembers.Remove(existingMember);

                            db.SaveChanges();

                            TempData["successMessage"] = "You have leaved the room successfully";
                            return(RedirectToAction("Index"));
                        }
                    }
                }
            }
            else
            {
                ModelState.AddModelError("JoinError", "Room ID param was not found");
            }
            return(View());
        }
Beispiel #6
0
        public ActionResult JoinRoom(int?roomID)
        {
            if (roomID != null)
            {
                int  currentUserID = ((User)HttpContext.Items["currentUser"]).UserID;
                Room joiningRoom;
                using (var db = new RealtimeChatDB())
                {
                    joiningRoom = db.Rooms.Where(r => r.RoomID == roomID).FirstOrDefault();
                    if (joiningRoom != null)
                    {
                        RoomMember existingMember = db.RoomMembers.Where(m => m.UserID == currentUserID && m.RoomID == joiningRoom.RoomID).FirstOrDefault();
                        if (existingMember == null)
                        {
                            var newMember = db.RoomMembers.Create();
                            newMember.UserID      = currentUserID;
                            newMember.RoomID      = joiningRoom.RoomID;
                            newMember.MemberLevel = 0;
                            db.RoomMembers.Add(newMember);

                            db.SaveChanges();

                            TempData["successMessage"] = "You have joined the room successfully";
                            return(RedirectToAction("All"));
                        }
                        else
                        {
                            ModelState.AddModelError("JoinError", "You have already joined this room");
                        }
                    }
                }
            }
            else
            {
                ModelState.AddModelError("JoinError", "Room ID param was not found");
            }
            return(View());
        }
Beispiel #7
0
        public ActionResult Create(RoomCreateViewModel roomCreateInfo)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    int currentUserID = ((User)HttpContext.Items["currentUser"]).UserID;
                    using (var db = new RealtimeChatDB())
                    {
                        var newRoom = db.Rooms.Create();
                        newRoom.RoomName        = roomCreateInfo.RoomName;
                        newRoom.ShowName        = roomCreateInfo.ShowName;
                        newRoom.RoomDescription = roomCreateInfo.Description;
                        newRoom.CreatorID       = ((User)HttpContext.Items["currentUser"]).UserID;
                        db.Rooms.Add(newRoom);

                        var newMember = db.RoomMembers.Create();
                        newMember.UserID      = ((User)HttpContext.Items["currentUser"]).UserID;
                        newMember.RoomID      = newRoom.RoomID;
                        newMember.MemberLevel = 2;
                        db.RoomMembers.Add(newMember);

                        db.SaveChanges();

                        TempData["successMessage"] = "You have successfully created a room";
                        return(RedirectToAction("Index"));
                    }
                }
                else
                {
                    return(View());
                }
            }
            catch
            {
                return(View());
            }
        }
Beispiel #8
0
 public ActionResult Create(RegisterViewModel registerInfo)
 {
     if (ModelState.IsValid)
     {
         using (var db = new RealtimeChatDB())
         {
             var newUser = db.Users.Create();
             newUser.UserName     = registerInfo.UserName;
             newUser.UserPassword = registerInfo.UserPassword;
             newUser.Email        = registerInfo.Email;
             newUser.ShowName     = registerInfo.ShowName;
             newUser.CreatedAt    = DateTime.Now;
             db.Users.Add(newUser);
             db.SaveChanges();
             TempData["successMessage"] = "Signed up successfully. Please login.";
             return(RedirectToAction("Login", "Sessions"));
         }
     }
     else
     {
         return(View());
     }
 }