public static void AddOrUpdateUserCollectionCard(int collectionId, IEnumerable <CollectionCard> cards, out string error) { error = ""; using (var db = new FortyLifeDbContext()) { var scryfallRequestEngine = new ScryfallRequestEngine(); var collectionCards = db.CollectionCards; var cardList = cards.ToList(); // fill in set codes for entries that don't have one foreach (var card in cardList) { if (string.IsNullOrEmpty(card.SetCode)) { card.SetCode = scryfallRequestEngine.GetCard(card.Name).Set.ToUpper(); } // also fill in the set name card.SetName = new ScryfallRequestEngine().SetRequest(card.SetCode).Name; } var duplicates = cardList.GroupBy(i => new { i.Name, i.SetCode, i.Foil }) .Where(i => i.Count() > 1) .Select(i => i.Key); var duplicateList = duplicates.ToList(); if (!duplicateList.Any()) { DeleteAllCardsInCollection(collectionId); foreach (var card in cardList) { card.CollectionId = collectionId; collectionCards.AddOrUpdate(card); } db.SaveChanges(); } else { var errorCard = duplicateList.First(); var errorCardName = errorCard.Name; // find the second occurence of any duplicated item var firstOcc = cardList.FindIndex(i => i.Name == errorCard.Name && i.SetCode == errorCard.SetCode) + 1; var errorLine = cardList.FindIndex(firstOcc, i => i.Name == errorCard.Name && i.SetCode == errorCard.SetCode) + 1; error = $"Duplicate card detected: {errorCardName}. (Lines {firstOcc} and {errorLine})"; } } }
public static void DeleteAllCardsInCollection(int collectionId) { using (var db = new FortyLifeDbContext()) { foreach (var card in db.CollectionCards.Where(i => i.CollectionId == collectionId)) { db.CollectionCards.Remove(card); } db.SaveChanges(); } }
private static void RegenerateActivationKey(string email) { using (var db = new FortyLifeDbContext()) { var user = db.ApplicationUsers.FirstOrDefault(i => i.Email == email); if (user != null) { user.ActivationKey = RemoveReservedUnsafeCharacters(UserAuthenticator.GetHashString(DateTime.Now.ToString("G"))); db.SaveChanges(); } } }
public static void ActivateUser(string email) { using (var db = new FortyLifeDbContext()) { var user = db.ApplicationUsers.FirstOrDefault(i => i.Email == email); if (user != null) { user.ActivationKey = null; db.SaveChanges(); } } }
public static void AddOrUpdateUserCollection(string email, Collection collection, out string error) { error = ""; using (var db = new FortyLifeDbContext()) { var users = db.ApplicationUsers.Include(i => i.Collections.Select(j => j.Cards)); var user = users.FirstOrDefault(i => i.Email == email); if (user != null) { if (user.Collections == null) { user.Collections = new List <Collection>(); } var collectionToUpdate = user.Collections.FirstOrDefault(i => i.CollectionId == collection.CollectionId); if (collectionToUpdate != null) { collectionToUpdate.CreateDate = collection.CreateDate; collectionToUpdate.LastEditDate = collection.LastEditDate; collectionToUpdate.Name = collection.Name; collectionToUpdate.Description = collection.Description; } else { collection.Cards = null; user.Collections.Add(collection); } db.SaveChanges(); } else { error = "Error! No known user exists with an email address matching the current session claim."; } } }
public static void DeleteUserCollection(string email, Collection collection, out string error) { error = ""; using (var db = new FortyLifeDbContext()) { var users = db.ApplicationUsers.Include(i => i.Collections.Select(j => j.Cards)); var user = users.FirstOrDefault(i => i.Email == email); if (user != null) { if (user.Collections.Exists(i => i.CollectionId == collection.CollectionId)) { // remove it from the object in memory user.Collections.RemoveAll(i => i.CollectionId == collection.CollectionId); // remove all cards associated with the collection in the db foreach (var card in db.CollectionCards.Where(i => i.CollectionId == collection.CollectionId)) { db.CollectionCards.Remove(card); } // remove the collection from the db db.Collections.Remove(db.Collections.First(i => i.CollectionId == collection.CollectionId)); db.SaveChanges(); } else { error = "Error! Collection doesn't exist."; } } else { error = "Error! No known user exists with an email address matching the current session claim."; } } }
public static bool CreateAccount(string email, string password) { if (GetApplicationUser(email) != null) { return(false); } var salt = UserAuthenticator.GetHashString(DateTime.Now.Ticks.ToString()); var passwordHash = UserAuthenticator.ComputeHash(password, salt); var newUser = new ApplicationUser { Email = email, DisplayName = email.Split('@')[0], PasswordHash = passwordHash, PasswordSalt = salt, CreateDate = DateTime.Now, ActivationKey = RemoveReservedUnsafeCharacters(UserAuthenticator.GetHashString(DateTime.Now.ToString("G"))) }; using (var db = new FortyLifeDbContext()) { try { db.ApplicationUsers.AddOrUpdate(newUser); db.SaveChanges(); SendActivationEmail(email); return(true); } catch (Exception e) { // TODO: implement proper logger for exceptions return(false); } } }
public int ProductIdRequest(string cardName, string setName) { setName = SanitizeSetName(setName); using (var db = new FortyLifeDbContext()) { if (db.CardProductIds.Any(i => i.CardName == cardName && i.SetName == setName)) { return(db.CardProductIds.First(i => i.CardName == cardName && i.SetName == setName).ProductId); } // Rate limit to be a good samaritan Thread.Sleep(200); // TODO: find a better way to do this without shutting down the thread // when searching for products in the TCG Player API, it only accepts the name of the front face of double faced cards if (cardName.Contains("//")) { cardName = cardName.Split(new[] { "//" }, StringSplitOptions.RemoveEmptyEntries)[0].Trim(); } var searchCriteria = new CategoryProductsSearchBody { Sort = "name", Limit = 5, Offset = 0, Filters = new List <Filter> { new Filter { Name = Filter.FilterName.ProductName.ToString(), Values = new List <string> { cardName } } } }; //if (!string.IsNullOrEmpty(setName)) //{ // searchCriteria.Filters.Add(new Filter // { // Name = Filter.FilterName.SetName.ToString(), // Values = new List<string> {setName} // }); //} var body = JsonConvert.SerializeObject(searchCriteria); var jsonResult = Post(categoryProductSearchUri, body, RequestBodyType.Json, ReadAccessToken()); if (!string.IsNullOrEmpty(jsonResult)) { var productIdResult = JsonConvert.DeserializeObject <CategoryProductsResult>(jsonResult).Results .OrderBy(i => i).FirstOrDefault(); if (productIdResult > 0) { var newProductId = new CardProductId { CardName = cardName, SetName = setName, ProductId = productIdResult }; db.CardProductIds.AddOrUpdate(newProductId); db.SaveChanges(); return(productIdResult); } } return(0); } }