public Receta ConvertENToModelUI(RecetaEN en)
        {
            Receta model = new Receta();
            model.id = en.Id;
            model.Nombre = en.Nombre;
            model.Descripcion = en.Descripcion;
            model.Foto = en.Foto;
            model.FechaCreacion = en.FechaCreacion;
            model.Estado = en.Estado;

            model.IdUsuario = en.Usuario.Id;
            model.NombreUsuario = en.Usuario.Nick;

            model.UsuariosFavorito = en.UsuariosFavorito.ToList();
            // Pasos ordenados por numero paso
            model.Pasos = new AssemblerPaso().ConvertListENToModel(en.Pasos);
            model.Pasos = model.Pasos.OrderBy(f => f.NumeroPaso).ToList();

            model.Comentarios = new AssemblerComentario().ConvertListENToModel(en.Comentarios);
            //model.Comentarios = en.Comentarios.ToList();

            model.LineasIngrediente = new AssemblerLineaIngrediente().ConvertListENToModel(en.LineasIngrediente);
            //model.LineasIngrediente = en.LineasIngrediente.ToList();

            model.LineasListaCompra = en.LineasListaCompra.ToList();

            return model;
        }
        public Receta ConvertENToModelUI(RecetaEN en)
        {
            Receta model = new Receta();

            model.id            = en.Id;
            model.Nombre        = en.Nombre;
            model.Descripcion   = en.Descripcion;
            model.Foto          = en.Foto;
            model.FechaCreacion = en.FechaCreacion;
            model.Estado        = en.Estado;

            model.IdUsuario     = en.Usuario.Id;
            model.NombreUsuario = en.Usuario.Nick;

            model.UsuariosFavorito = en.UsuariosFavorito.ToList();
            // Pasos ordenados por numero paso
            model.Pasos = new AssemblerPaso().ConvertListENToModel(en.Pasos);
            model.Pasos = model.Pasos.OrderBy(f => f.NumeroPaso).ToList();

            model.Comentarios = new AssemblerComentario().ConvertListENToModel(en.Comentarios);
            //model.Comentarios = en.Comentarios.ToList();

            model.LineasIngrediente = new AssemblerLineaIngrediente().ConvertListENToModel(en.LineasIngrediente);
            //model.LineasIngrediente = en.LineasIngrediente.ToList();

            model.LineasListaCompra = en.LineasListaCompra.ToList();

            return(model);
        }
        public ActionResult Crear(Receta rec, HttpPostedFileBase file, int[] canIngrediente, UnidadesEnum[] uniIngrediente, string[] nomIngrediente, int[] numPaso, string[] desPaso)
        {
            string imagesDir = "Images/Uploads";

            string fileName = "", path = "";
            // Verify that the user selected a file
            if (file != null && file.ContentLength > 0)
            {
                // extract only the fielname
                fileName = Path.GetFileName(file.FileName);
                // store the file inside ~/App_Data/uploads folder
                path = Path.Combine(Server.MapPath("~/"+imagesDir), fileName);
                //string pathDef = path.Replace(@"\\", @"\");
                file.SaveAs(path);
            }

            try
            {
                fileName = "/"+imagesDir+"/" + fileName;

                // Parametros por defecto
                rec.FechaCreacion = DateTime.Now;
                rec.Estado = EnMiNeveraGenNHibernate.Enumerated.EnMiNevera.EstadosEnum.publicado;
                rec.Foto = fileName;

                // Obtenemos los pasos
                IList<Paso> pasos = new List<Paso>();
                Paso paso = null;
                for(int i=0; i<numPaso.Count(); i++)
                {
                    paso = new Paso();
                    paso.NumeroPaso = numPaso[i];
                    paso.Descripcion = desPaso[i];
                    pasos.Add(paso);
                }

                // Obtenemos las lineas de ingrediente
                IList<LineaIngrediente> lineasIng = new List<LineaIngrediente>();
                LineaIngrediente lin = null;
                for(int i=0; i<canIngrediente.Count(); i++)
                {
                    lin = new LineaIngrediente();
                    lin.Cantidad = canIngrediente[i];
                    lin.NombreIngrediente = nomIngrediente[i];
                    lin.Unidad = uniIngrediente[i];
                    lineasIng.Add(lin);
                }

                SessionInitialize();
                using (var transaction = session.BeginTransaction ())
                {
                    // Obtengo el id del usuario
                    int idUsuario = new UsuarioCAD(session).GetByNick(User.Identity.Name).Id;

                    RecetaCAD cad = new RecetaCAD(session);
                    RecetaCEN cen = new RecetaCEN(cad);

                    int idReceta = cen.New_(rec.Nombre, rec.Descripcion, rec.Foto, idUsuario, rec.FechaCreacion, rec.Estado);

                    // Creamos los pasos
                    PasosCAD pasosCad = new PasosCAD(session);
                    PasosCEN pasosCen = new PasosCEN(pasosCad);
                    foreach(Paso p in pasos)
                    {
                        pasosCen.New_(p.Descripcion, idReceta, p.NumeroPaso);
                    }

                    // Creamos las lineas de ingrediente
                    LineaIngredienteCAD lineaIngredienteCad = new LineaIngredienteCAD(session);
                    LineaIngredienteCEN lineaIngredienteCen = new LineaIngredienteCEN(lineaIngredienteCad);

                    IngredienteCAD ingredienteCad = new IngredienteCAD(session);
                    IngredienteEN ingredienteEn;

                    foreach(LineaIngrediente l in lineasIng)
                    {
                        // por cada linea, si no existe el ingrediente, lo creamos
                        ingredienteEn = ingredienteCad.GetPorNombre(l.NombreIngrediente);
                        if(ingredienteEn==null)
                        {
                            ingredienteEn = new IngredienteEN();
                            ingredienteEn.Nombre = l.NombreIngrediente;
                            ingredienteEn.Id = ingredienteCad.New_(ingredienteEn);
                        }

                        lineaIngredienteCen.New_(l.Cantidad, l.Unidad, ingredienteEn.Id, idReceta);
                    }

                    transaction.Commit ();
                }
                SessionClose();

                //RecetaCEN cen = new RecetaCEN();

                //// Obtengo el id del usuario
                //int idUsuario = new UsuarioCAD().GetByNick(User.Identity.Name).Id;

                //rec.FechaCreacion = DateTime.Now;
                //rec.Estado = EnMiNeveraGenNHibernate.Enumerated.EnMiNevera.EstadosEnum.publicado;

                //cen.New_(rec.Nombre, rec.Descripcion, fileName, idUsuario, rec.FechaCreacion, rec.Estado);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }