public void AddArticle(Article article)
 {
     using (var db = new BlogContext())
     {
         db.Articles.Add(article);
         db.SaveChanges();
     }
 }
 public void CreateArticle(Article article, Byte[] ImageBytes)
 {
     if (ImageBytes != null)
     {
         article.PicturePath = BytesImage(ImageBytes);
     }
     else
     {
         article.PicturePath = String.Empty;
     }
     AddArticle(article);
 }
        public async Task<ActionResult> CreateArticle(Article article, HttpPostedFileBase image)
        {
            try
            {
                if (Request.Form["Confirm"] != null)
                {
                    List<Tuple<string, string>> errors = articleManager.ValidateArticle(article);
                    if (errors.Count == 0)
                    {
                        var user = (User)Session["login"];
                        article.AuthorId = user.Id;

                        article.ModificationdDate = DateTime.Now;
                        article.CreationDate = DateTime.Now;

                        if (image != null)
                        {
                            var imageData = new byte[image.ContentLength];
                            image.InputStream.Read(imageData, 0, image.ContentLength);
                            articleManager.CreateArticle(article, imageData);
                        }
                        else
                        {
                            articleManager.CreateArticle(article, null);
                        }
                        return RedirectToAction("ArticleView", new { id = article.Id });
                    }
                    else
                    {
                        foreach (Tuple<string, string> t in errors)
                        {
                            ModelState.AddModelError(t.Item1, t.Item2);
                        }
                        return View(article);
                    }
                }
                else
                {
                    if (Request.Form["Import"] != null)
                    {
                        return RedirectToAction("Import");
                    }
                    return View();
                }
            }
            catch (Exception e)
            {
                ViewBag.ErrorTitle = "Server Error";
                ViewBag.ErrorDescription = "Please try again later";
                return View("~/Views/Shared/ErrorPage.cshtml");
            }
        }
 public void ModifyArticle(Article article)
 {
     using (var db = new BlogContext())
     {
         var query = (from a in db.Articles
                      where a.Id == article.Id
                      select a).First();
         query.Layout = article.Layout;
         query.ModificationdDate = DateTime.Now;
         query.Name = article.Name;
         query.PicturePath = article.PicturePath;
         query.Text = article.Text;
         query.Type = article.Type;
         db.SaveChanges();
     }
 }
        //AGREGAR EL USUARIO CUANDO SE EMPIECE A AUTENTICAR
        public int ImportArticles(XmlDocument xml)
        {
            int errors = 0;
            List<Article> listaArticulos = new List<Article>();
            XmlNodeList articulos = xml.SelectNodes("//Articulos/Articulo");

            foreach (XmlNode nodo in articulos)
            {
                try
                {
                    Article art = new Article
                                        {
                                            Name = nodo.SelectSingleNode("Nombre").InnerText,
                                            Text = nodo.SelectSingleNode("Texto").InnerText
                                        };
                    string upper = nodo.SelectSingleNode("Tipo").InnerText;
                    upper = upper.ToUpper();
                    switch (upper)
                    {
                        case "PUBLICO":
                            art.Type = ArticleType.Public;
                            break;
                        case "PRIVADO":
                            art.Type = ArticleType.Private;
                            break;
                        default: throw new Exception();
                    }
                    upper = nodo.SelectSingleNode("Plantilla").InnerText;
                    upper = upper.ToUpper();
                    switch (upper)
                    {
                        case "INFERIOR":
                            art.Layout = ArticleLayout.Bottom;
                            break;
                        case "IZQUIERDA":
                            art.Layout = ArticleLayout.Left;
                            break;
                        case "":
                            art.Layout = ArticleLayout.NoPicture;
                            break;
                        case "SUPERIOR":
                            art.Layout = ArticleLayout.Top;
                            break;
                        default: throw new Exception();
                    }
                    art.CreationDate = DateTime.Now;
                    art.ModificationdDate = DateTime.Now;
                    art.AuthorId = 1;
                    if (!nodo.SelectSingleNode("Foto").InnerText.Equals(String.Empty))
                    {
                        byte[] imageByte = Convert.FromBase64String(nodo.SelectSingleNode("Foto").InnerText);
                        art.PicturePath = BytesImage(imageByte);
                    }
                    else
                    {
                        art.PicturePath = String.Empty;
                    }
                    articleDataAccess.AddArticle(art);
                }
                catch (Exception)
                {
                    errors++;
                }
            }
            return errors;
        }
 public void AddArticle(Article article)
 {
     articleDataAccess.AddArticle(article);
 }
        public List<Tuple<string, string>> ValidateArticle(Article article)
        {
            List<Tuple<string, string>> errors = new List<Tuple<string, string>>();
            if (article.Name == null || article.Name.Equals(String.Empty))
            {
                errors.Add(new Tuple<string,string>("Name", "The Name field is required"));
            }

            if (article.Text == null || article.Text.Equals(String.Empty))
            {
                errors.Add(new Tuple<string, string>("Text", "The Text field is required"));
            }

            return errors;
        }
 public void UpdateArticle(Article article, Byte[] ImageBytes)
 {
     if (ImageBytes != null)
     {
         article.PicturePath = BytesImage(ImageBytes);
     }
     else //SI TRAE EL PATH EL ART NO ES NECESARIO
     {
         Article artAux = articleDataAccess.GetArticleById(article.Id);
         if (artAux.PicturePath.Equals(String.Empty))
         {
             article.PicturePath = String.Empty;
         }
         else
         {
             article.PicturePath = artAux.PicturePath;
         }
     }
     articleDataAccess.ModifyArticle(article);
 }