public static void AddComment(string textOfComment, int articleId, string UserMail) { Comment comment = new Comment() { Author = UserMail, Time = DateTime.Now, TextOfComment = textOfComment }; Article articl = DbLogic.GetArticle(articleId); if (articl != null) { if (articl.Comments == null) { articl.Comments = new List <Comment>() { comment }; } else { articl.Comments.Add(comment); } } BlogContext db = DbLogic.GetDB(); db.Entry(articl).State = EntityState.Modified; db.SaveChanges(); DbLogic.SetDb(db); }
static public int EditArticle(Article article, string UserMail) { BlogContext db = DbLogic.GetDB(); Article changedArticle = db.Articles.Find(article.ArticleId); article.Time = DateTime.Now; article.UserMail = UserMail; db.Articles.Remove(changedArticle); db.Articles.Add(article); db.SaveChanges(); DbLogic.SetDb(db); return(article.ArticleId); }
static public string AddCategory(Category model) { BlogContext db = DbLogic.GetDB(); if (db.Categories.Contains(model)) { return("This category is exists"); } if (model != null) { db.Categories.Add(model); } return("Your category was created"); }
static public void EditCategory(Category category) { BlogContext db = DbLogic.GetDB(); Category changedCategory = db.Categories.Find(category.CategoryId); foreach (var b in db.Articles) { if (changedCategory.CategoryName == b.Category) { b.Category = category.CategoryName; } } db.Categories.Remove(changedCategory); db.Categories.Add(category); db.SaveChanges(); DbLogic.SetDb(db); }
static public int AddArticle(AddArticleViewModel model, HttpPostedFileBase Photo, string UserMail) { byte[] imageDate = null; if (Photo != null) { using (var binaryReader = new BinaryReader(Photo.InputStream)) { imageDate = binaryReader.ReadBytes(Photo.ContentLength); } } string tagsString = null; if (model.TagsList != null) { foreach (var b in model.TagsList) { tagsString += b + " "; } } Article article = new Article() { Name = model.Name, FullDescription = model.FullDescription, Tags = tagsString, ShortDescription = model.ShortDescription, Category = model.Category }; article.Picture = new Picture() { Name = Convert.ToString(article.ArticleId), Image = imageDate, Id = article.ArticleId }; article.Time = DateTime.Now; article.UserMail = UserMail; BlogContext blogContext = DbLogic.GetDB(); blogContext.Articles.Add(article); blogContext.SaveChanges(); DbLogic.SetDb(blogContext); return(article.ArticleId); }
static public List <Article> Search(string articleCat, string searchString, DateTime?fromDate, DateTime?forDate) { BlogContext db = DbLogic.GetDB(); if (fromDate == null) { fromDate = new DateTime(2019, 1, 1); } if (forDate == null) { forDate = new DateTime(2080, 1, 1); } bool isTagsContainsAll = true; if (!String.IsNullOrEmpty(searchString) && articleCat == "All") { var art = new List <Article>(); var tagsToFind = searchString.Split(' ').ToList <string>(); foreach (Article b in db.Articles) { if (!String.IsNullOrEmpty(b.Tags)) { if (b.Time > fromDate && b.Time < forDate) { isTagsContainsAll = true; foreach (var i in tagsToFind) { if (!b.Tags.Contains(i)) { isTagsContainsAll = false; } } if (isTagsContainsAll) { art.Add(b); } } } } return(art); } if (!String.IsNullOrEmpty(searchString)) { var tagsToFind = searchString.Split(' ').ToList <string>(); var art = new List <Article>(); foreach (Article b in db.Articles) { if (!String.IsNullOrEmpty(b.Tags)) { if (b.Tags.Contains(searchString) && articleCat == b.Category && b.Time > fromDate && b.Time < forDate) { isTagsContainsAll = true; foreach (var i in tagsToFind) { if (!b.Tags.Contains(i)) { isTagsContainsAll = false; } } if (isTagsContainsAll) { art.Add(b); } } } } return(art); } var article = new List <Article>(); foreach (Article b in db.Articles) { if (b.Time > fromDate && b.Time < forDate) { article.Add(b); } } return(article); }