Beispiel #1
0
 public ActionResult RemoveUser(int idUser)
 {
     using (var context = new JayaModelContainer())
     {
         using (var transaction = new TransactionScope())
         {
             var user = context.Users.FirstOrDefault(u => u.IdUser == idUser);
             user.WasRemoved = true;
             user.IsActive   = false;
             context.SaveChanges();
             ViewBag.nickname = user.Nick;
             var rooms = context.Rooms.Where(r => r.IsActive && r.Chats.Any(c => c.IdUser == idUser) && r.Chats.Count(c => c.IdUser == idUser && c.IsEvent) % 2 != 0);
             foreach (var room in rooms)
             {
                 ViewBag.idRoom = room.IdRoom;
                 room.CurrentUsers--;
                 if (room.CurrentUsers == 0)
                 {
                     room.IsActive = false;
                 }
                 context.Chats.Add(new Chat {
                     IdRoom = room.IdRoom, IdUser = idUser, IsEvent = true, Message = $"{user.Nick} Ha sido expulsado"
                 });
                 context.SaveChanges();
             }
             transaction.Complete();
         }
         return(View());
     }
 }
Beispiel #2
0
 public ActionResult Index(string nickname)
 {
     if (string.IsNullOrWhiteSpace(nickname))
     {
         return(Index());
     }
     ViewData["NickName"] = nickname == "" ? null : nickname;
     using (var context = new JayaModelContainer())
     {
         if (context.Users.Any(u => u.Nick == nickname.ToLower() && u.IsActive))
         {
             ModelState.AddModelError("Nick", "El nickname ya existe.");
         }
         else
         {
             var user = new User {
                 IsActive = true, Nick = nickname.ToLower()
             };
             context.Users.Add(user);
             context.SaveChanges();
             return(RedirectToAction("Room", new { nickname, id = user.IdUser }));
         }
         return(View());
     }
 }
Beispiel #3
0
 public JsonResult SaveMessage(string message, int idUser, int idRoom)
 {
     using (var context = new JayaModelContainer())
     {
         context.Chats.Add(new Chat {
             IdRoom = idRoom, IdUser = idUser, Message = message
         });
         context.SaveChanges();
     }
     return(null);
 }
Beispiel #4
0
 public ActionResult Getout(string nickname, int idUser, int idRoom)
 {
     using (var context = new JayaModelContainer())
     {
         using (var transaction = new TransactionScope())
         {
             var room = context.Rooms.FirstOrDefault(r => r.IdRoom == idRoom);
             room.CurrentUsers--;
             if (room.CurrentUsers == 0)
             {
                 room.IsActive = false;
             }
             context.SaveChanges();
             context.Chats.Add(new Chat {
                 IdRoom = idRoom, IdUser = idUser, IsEvent = true, Message = $"{nickname} Ha salido"
             });
             context.SaveChanges();
             transaction.Complete();
         }
         return(RedirectToAction("Room", new { nickname, id = idUser }));
     }
 }
Beispiel #5
0
        public ActionResult ChatWithRegister(string nickname, int idUser, int idRoom)
        {
            ViewBag.nickname = nickname;
            ViewBag.idUser   = idUser;
            ViewBag.idRoom   = idRoom;
            using (var context = new JayaModelContainer())
            {
                using (var transaction = new TransactionScope())
                {
                    var room = context.Rooms.FirstOrDefault(r => r.IdRoom == idRoom);

                    room.CurrentUsers++;
                    context.SaveChanges();
                    context.Chats.Add(new Chat {
                        IdRoom = idRoom, IdUser = idUser, IsEvent = true, Message = $"{nickname} Ha ingresado"
                    });
                    context.SaveChanges();
                    transaction.Complete();
                }
                return(View());
                //return RedirectToAction("Chat", new { nickname, idUser, idRoom });
            }
        }
Beispiel #6
0
 public ActionResult Create(Room room, string nickname, int id)
 {
     if (!ModelState.IsValid)
     {
         return(View(room));
     }
     using (var context = new JayaModelContainer())
     {
         var newRoom = new Room {
             CurrentUsers = 1, IsActive = true, Name = room.Name, Chats = new List <Chat> {
                 new Chat {
                     IsEvent = true, IdUser = id, Message = $"{nickname} Ha ingresado"
                 }
             }
         };
         context.Rooms.Add(newRoom);
         context.SaveChanges();
         return(RedirectToAction("Chat", new { nickname, idUser = id, idRoom = newRoom.IdRoom }));
     }
 }