public User Register(User user, string password) { var pair = PasswordHash(password); user.Password = pair.Item1; user.Salt = pair.Item2; context.Add(user); context.SaveChanges(); return(user); }
public void ReadAllAndStore() { List <NewsSource> newsSources = context.NewsSources.ToList(); //databaseCtx.NewsArticles.RemoveRange(databaseCtx.NewsArticles); List <NewsArticle> newsArticles = new List <NewsArticle>(); // read from all sources for (int i = 0; i < newsSources.Count; i++) { try { NewsSource source = newsSources[i]; List <NewsArticle> articles = Read(source); newsArticles.AddRange(articles); } catch (Exception) { Console.WriteLine("Cannot read from source" + newsSources[i].Name); } } // last group number int lastGroup = context.NewsArticles.Select(x => (int?)x.Group).Max() ?? 0; for (int rep = 0; rep < 31; rep++) // 32 is number of bits of the SimHash fingerprint { // sort newsArticles.Sort((x, y) => x.SimHash.CompareTo(y.SimHash)); // search for possible groups for (int i = 1; i < newsArticles.Count; i++) { var previous = newsArticles[i - 1]; var current = newsArticles[i]; if (HammingDistance(previous.SimHash, current.SimHash) <= 3) { if (previous.Group == 0) { previous.Group = current.Group = ++lastGroup; } else { current.Group = previous.Group; } } } // rotate foreach (var article in newsArticles) { article.SimHash = RotateLeft(article.SimHash, 1); } } context.AddRange(newsArticles); context.SaveChanges(); Console.WriteLine("News read from " + newsSources.Count.ToString() + " sources"); }
public IActionResult SetNewsPreferences([FromBody] int[] newsSourceIds) { int userId = GetUserId(); List <NewsSource> newNewsSources = database.NewsSources.Where(n => newsSourceIds.Contains(n.Id)).ToList(); var userPreferences = database.UserPreferences.Find(userId); if (userPreferences == null) { userPreferences = new UserPreference { UserId = userId, NewsSources = newNewsSources }; } else { database.UserPreferences.Remove(userPreferences); database.SaveChanges(); } userPreferences = new UserPreference { UserId = userId, NewsSources = newNewsSources }; database.UserPreferences.Add(userPreferences); database.SaveChanges(); return(Ok()); }
public IActionResult AddSource([FromBody] NewsSource source) { database.NewsSources.Add(source); database.SaveChanges(); return(Ok()); }