public static bool GetUser(string email, out UserModel user) { email = Utils.RemoveSpecialCharacters(email.ToLowerInvariant()); user = CosmosDBClient.Query <UserModel>(limitOne: true, crossPartition: true) .Where(u => u.Email == email).AsEnumerable().FirstOrDefault(); return(user != null); }
/// <summary> /// Queries CosmosDB for each of the token types /// sorts results based on location (if provided) and date /// </summary> public static IEnumerable <NoteModel> QueryNotes(string userId, string queryContents, string city, Point userLocation = null) { List <string> uniqueTokens = IndexerBase.GetDistinctTokens(queryContents).ToList(); if (uniqueTokens.Count == 0) { return(new List <NoteModel>()); } IQueryable <NoteModel> notes = CosmosDBClient.Query <NoteModel>().Where(n => n.UserId == userId); foreach (string token in uniqueTokens) { if (string.IsNullOrWhiteSpace(token)) { continue; } // Check each Indexer for membership if (new HashTagIndexer().IsMember(token)) { new HashTagIndexer().FilterNoteKeys(ref notes, token); TransactionModel.AddTransaction(userId, TransactionModel.TransactionType.Search, token, city, userLocation); continue; // index membership is disjoint } // Check each Indexer for membership if (new LocationIndexer().IsMember(token)) { new LocationIndexer().FilterNoteKeys(ref notes, token); continue; // index membership is disjoint } else if (new DateIndexer().IsMember(token)) { new DateIndexer().FilterNoteKeys(ref notes, token); continue; // index membership is disjoint } else if (new DateRangeIndexer().IsMember(token)) { // Date range is an exception where it's not stored as an actual index // but instead a range of indices new DateRangeIndexer().FilterNoteKeys(ref notes, token); continue; // index membership is disjoint } else { // Allow user to forget to add '#' when querying hashtags //new HashTagIndexer().FilterNoteKeys(ref notes, "#" + token); // Word is always the default token new WordIndexer().FilterNoteKeys(ref notes, token); } } return(notes); //// Build the query based on the tokens //if (userLocation != null) //{ // return notes.OrderByDescending(n => n.Location.Distance(userLocation)) // .ThenBy(n => n.LastUpdatedTime); //} //return notes.OrderByDescending(n => n.LastUpdatedTime); }
public static IEnumerable <string> GetRecentLocations(string userId) { // TODO: Include frequency into sorting IQueryable <TransactionModel> transactions = CosmosDBClient.Query <TransactionModel>().Where(t => t.UserId == userId).Where(t => t.City != string.Empty).Where(t => t.City != null); IEnumerable <string> tags = transactions.OrderBy(t => t.TransactionTime).Select(t => t.City).AsEnumerable().Distinct(); return(tags); }
/// <summary> /// Returns the last updated time, null if there has never been an update /// </summary> /// <param name="userId"></param> /// <returns></returns> public static DateTime?GetLastUpdate(string userId) { LastUpdateModel lastUpdate = CosmosDBClient.Query <LastUpdateModel>() .Where(lu => lu.UserId == userId).Where(lu => lu.Id == GetId(userId)).AsEnumerable().FirstOrDefault(); if (lastUpdate == default(LastUpdateModel)) { return(null); } return(lastUpdate.LastUpdateTime); }
public static IEnumerable <string> GetTagsByLocation(string userId, Point userLocation, TransactionType type) { var transactions = CosmosDBClient.Query <TransactionModel>().Where(t => t.UserId == userId && t.Type == type).Select(t => new { transaction = t, distance = ((int)t.Location.Distance(userLocation)) / 10 }); IEnumerable <string> tags = transactions.AsEnumerable().GroupBy(t => t.transaction.Tag) .Select(g => new { g.Key, spaceSort = g.Min(t => t.distance), countSort = g.Count() }) .OrderBy(g => g.spaceSort) .ThenBy(g => g.countSort) .Select(t => t.Key); return(tags); }
/// <summary> /// Updates the note and cleans up removed tags /// </summary> public static NoteModel UpdateNote(string userId, string noteId, string noteContents, string city, float latitude, float longitude, bool completed) { NoteModel note = CosmosDBClient.Query <NoteModel>() .Where(n => n.UserId == userId).Where(n => n.Id == noteId).AsEnumerable().FirstOrDefault(); if (note == default(NoteModel)) { return(null); } IEnumerable <string> removedTags; IEnumerable <string> newTags; note.SetNoteContents(noteContents, out removedTags, out newTags); // cleanup any removed tags (vast majority of the time user will not remove tags) foreach (string tag in removedTags) { TransactionModel.RemoveTransaction(userId, noteId, tag); } foreach (string tag in newTags) { TransactionModel.AddTransaction(userId, TransactionModel.TransactionType.Add, tag, city, latitude, longitude, noteId); } note.LastUpdatedTime = DateTime.UtcNow; if (note.City == null) { note.City = new HashSet <string>(); } if (!string.IsNullOrEmpty(city)) { note.City.Add(city); } note.Completed = completed; if (!CosmosDBClient.Update(note)) { return(null); } return(note); }
public static IEnumerable <TransactionModel> GetTagsByNoteId(string userId, string noteId) { IQueryable <TransactionModel> transactions = CosmosDBClient.Query <TransactionModel>().Where(t => t.UserId == userId && t.NoteId == noteId); return(transactions.AsEnumerable()); }