public void AddArticle(String articleId, Category c, HashSet<Tag> tags) { Article a = _db.Articles.Find(articleId); if (a != null) a.Popularity++; else { if (_db.Categories.Find(c.Name) == null) _db.Categories.Add(c); foreach (Tag t in tags) { t.CategoryId = c.Name; if (_db.Tags.Find(t.Name) == null) _db.Tags.Add(t); } _db.Articles.Add(new Article { Id = articleId, CategoryId = c.Name, Popularity = 0}); } try { _db.SaveChanges(); } catch (System.Data.Entity.Infrastructure.DbUpdateException dbEx) { Exception raise = dbEx; throw dbEx.InnerException; } }
public void UpdatePreference(String userId, Category c, bool b) { //int num = 0; //if (b) // num = 1; //else // num = -1; //foreach (Tag t in c.Tags) //{ // if (_db.Users.Find(userId).Preference.TagRatings.ContainsKey(t)) // _db.Users.Find(userId).Preference.TagRatings[t] += num; // else // _db.Users.Find(userId).Preference.TagRatings.Add(t, num); //} //_db.SaveChanges(); }
public List<Article> GetPopularByCategory(Category c, int num) { return new List<Article>(_db.Articles.Where(x => x.Category.Name == c.Name).OrderByDescending(x => x.Popularity).Take(num)); }