Exemplo n.º 1
0
        // GET: Admin/JuegoOnlines/Copy
        public ActionResult Copy(int id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            var JuegoOnline = db.JuegoOnlines.Include(s => s.TipoJuegoOnline).SingleOrDefault(s => s.JuegoOnlineId == id);

            if (JuegoOnline == null)
            {
                return(HttpNotFound());
            }

            JuegoOnlinesCopyViewModel viewModel = new JuegoOnlinesCopyViewModel();

            viewModel.JuegoOnline = JuegoOnline;
            viewModel.InicializarDesplegables();

            return(View(viewModel));
        }
Exemplo n.º 2
0
        public ActionResult Copy(JuegoOnlinesCopyViewModel viewModel)
        {
            int id = viewModel.JuegoOnline.JuegoOnlineId;

            viewModel.JuegoOnline = db.JuegoOnlines.Include(s => s.TipoJuegoOnline)
                                    .SingleOrDefault(s => s.JuegoOnlineId == id);
            if (viewModel.JuegoOnline != null)
            {
                viewModel.JuegoOnline.SubTemaId = viewModel.SubTemaCopiarId;

                db.JuegoOnlines.Add(viewModel.JuegoOnline);
                db.SaveChanges();


                //Meter en nuevos clientes
                var clientes = db.ClienteSubTemas.Where(cb => cb.SubTemaId == viewModel.SubTemaCopiarId);
                foreach (var clienteBloque in clientes)
                {
                    db.ClienteJuegoOnlines.Add(new ClienteJuegoOnline()
                    {
                        JuegoOnlineId = viewModel.JuegoOnline.JuegoOnlineId,
                        ClienteId     = clienteBloque.ClienteId
                    });
                }
                db.SaveChanges();


                var JuegoOnline = db.JuegoOnlines.Include(s => s.MemoryGames)
                                  .Include(s => s.BeatTheGoalies).Include(s => s.Ahorcado).Include("SopaLetras.CasillaSopaLetras")
                                  .Include("SopaLetras.VocabularioSopaLetras")
                                  .SingleOrDefault(s => s.JuegoOnlineId == id);



                if (JuegoOnline.BeatTheGoalies.Count > 0)
                {
                    foreach (BeatTheGoalie BeatTheGoalie in JuegoOnline.BeatTheGoalies.ToList())
                    {
                        BeatTheGoalie.JuegoOnlineId = viewModel.JuegoOnline.JuegoOnlineId;
                        BeatTheGoalie.SubTemaId     = viewModel.SubTemaCopiarId;
                        db.BeatTheGoalies.Add(BeatTheGoalie);
                        db.SaveChanges();


                        if (!string.IsNullOrEmpty(BeatTheGoalie.FicheroAudio))
                        {
                            string oldPathAndName = "~/media/upload/BeatTheGoalie/" + BeatTheGoalie.FicheroAudio;
                            if (System.IO.File.Exists(Server.MapPath(oldPathAndName)))
                            {
                                string newPathAndName = "~/media/upload/BeatTheGoalie/" + BeatTheGoalie.Id + ".mp3";

                                if (System.IO.File.Exists(Server.MapPath(newPathAndName)))
                                {
                                    System.IO.File.Delete(Server.MapPath(newPathAndName));
                                }

                                System.IO.File.Copy(Server.MapPath(oldPathAndName), Server.MapPath(newPathAndName));

                                BeatTheGoalie.FicheroAudio = BeatTheGoalie.Id + ".mp3";
                            }
                            else
                            {
                                BeatTheGoalie.FicheroAudio = null;
                            }
                            db.Entry(BeatTheGoalie).State = EntityState.Modified;
                            db.SaveChanges();
                        }
                    }
                }

                if (JuegoOnline.MemoryGames.Count > 0)
                {
                    foreach (MemoryGame MemoryGame in JuegoOnline.MemoryGames.ToList())
                    {
                        MemoryGame.JuegoOnlineId = viewModel.JuegoOnline.JuegoOnlineId;
                        MemoryGame.SubTemaId     = viewModel.SubTemaCopiarId;
                        db.MemoryGames.Add(MemoryGame);
                        db.SaveChanges();


                        if (!string.IsNullOrEmpty(MemoryGame.UrlImagen))
                        {
                            string oldPathAndName = "~/media/upload/MemoryGame/" + MemoryGame.UrlImagen;
                            if (System.IO.File.Exists(Server.MapPath(oldPathAndName)))
                            {
                                string newPathAndName = "~/media/upload/MemoryGame/" + MemoryGame.Id + ".jpg";
                                if (System.IO.File.Exists(Server.MapPath(newPathAndName)))
                                {
                                    System.IO.File.Delete(Server.MapPath(newPathAndName));
                                }

                                System.IO.File.Copy(Server.MapPath(oldPathAndName), Server.MapPath(newPathAndName));

                                MemoryGame.UrlImagen = MemoryGame.Id + ".jpg";
                            }
                            else
                            {
                                MemoryGame.UrlImagen = null;
                            }
                            db.Entry(MemoryGame).State = EntityState.Modified;
                            db.SaveChanges();
                        }
                    }
                }


                if (JuegoOnline.SopaLetras.Count > 0)
                {
                    foreach (SopaLetras SopaLetras in JuegoOnline.SopaLetras.ToList())
                    {
                        var SopaLetrasNuevo = db.SopaLetras
                                              .Include(cr => cr.CasillaSopaLetras).Include(cr => cr.VocabularioSopaLetras)
                                              .AsNoTracking().FirstOrDefault(cr => cr.Id == SopaLetras.Id);

                        SopaLetrasNuevo.JuegoOnlineId = viewModel.JuegoOnline.JuegoOnlineId;
                        SopaLetrasNuevo.SubTemaId     = viewModel.SubTemaCopiarId;
                        db.SopaLetras.Add(SopaLetrasNuevo);
                        db.SaveChanges();
                    }
                }

                if (JuegoOnline.Ahorcado.Count > 0)
                {
                    foreach (Ahorcado Ahorcado in JuegoOnline.Ahorcado.ToList())
                    {
                        Ahorcado.JuegoOnlineId = viewModel.JuegoOnline.JuegoOnlineId;
                        Ahorcado.SubTemaId     = viewModel.SubTemaCopiarId;
                        db.Ahorcado.Add(Ahorcado);
                        db.SaveChanges();


                        if (!string.IsNullOrEmpty(Ahorcado.UrlImagen))
                        {
                            string oldPathAndName = "~/media/upload/Ahorcado/" + Ahorcado.UrlImagen;
                            if (System.IO.File.Exists(Server.MapPath(oldPathAndName)))
                            {
                                string newPathAndName = "~/media/upload/Ahorcado/" + Ahorcado.Id + ".jpg";
                                if (System.IO.File.Exists(newPathAndName))
                                {
                                    System.IO.File.Delete(newPathAndName);
                                }
                                System.IO.File.Copy(Server.MapPath(oldPathAndName), Server.MapPath(newPathAndName));

                                Ahorcado.UrlImagen = Ahorcado.Id + ".jpg";
                            }
                            else
                            {
                                Ahorcado.UrlImagen = null;
                            }
                            db.Entry(Ahorcado).State = EntityState.Modified;
                            db.SaveChanges();
                        }
                        if (!string.IsNullOrEmpty(Ahorcado.Audio))
                        {
                            string oldPathAndName = "~/media/upload/Ahorcado/Audios/" + Ahorcado.Audio;
                            if (System.IO.File.Exists(Server.MapPath(oldPathAndName)))
                            {
                                string newPathAndName = "~/media/upload/Ahorcado/Audios/" + Ahorcado.Id + ".mp3";
                                if (System.IO.File.Exists(newPathAndName))
                                {
                                    System.IO.File.Delete(newPathAndName);
                                }

                                System.IO.File.Copy(Server.MapPath(oldPathAndName), Server.MapPath(newPathAndName));

                                Ahorcado.Audio = Ahorcado.Id + ".mp3";
                            }
                            else
                            {
                                Ahorcado.Audio = null;
                            }
                            db.Entry(Ahorcado).State = EntityState.Modified;
                            db.SaveChanges();
                        }
                    }
                }

                return(RedirectToAction("Create", JuegoOnline.TipoJuegoOnline.Controlador, new { id = viewModel.JuegoOnline.JuegoOnlineId }));
            }

            viewModel.InicializarDesplegables();
            return(View(viewModel));
        }