public async Task <IActionResult> AddToDeck(SearchDetailsViewModel model) { int deckId; if (int.TryParse(model.DeckId, out deckId)) { var card = await _DataAccess.CardDataAccess.GetCardByNameAsync(model.Name); if (card == null) { var apiCard = await _YGOProDeckAccess.GetCardByNameAsync(model.Name); if (apiCard.CardImages != null && apiCard.CardImages.Count > 0) { apiCard.ImageBytes = _YGOProDeckAccess.GetCardImage(apiCard.CardImages.First()); } card = await _DataAccess.CardDataAccess.Upsert(apiCard); } var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); // will give the user's userId if (string.IsNullOrWhiteSpace(userId)) { return(RedirectToAction("Details", model)); } var userInventory = await _DataAccess.UserInventoryDataAccess.GetUserInventoryByUserAsync(userId); var userCardMapping = _DataAccess.CardDataAccess.GetSpecificCardMappingForUserAsync(userInventory.Id, card.Id); if (userCardMapping == null) { var cardmapping = new InventoryCardMapping(); cardmapping.CardId = card.Id; cardmapping.UserInventoryId = userInventory.Id; cardmapping.Count = 1; await _DataAccess.CardDataAccess.UpsertCardMappingAsync(cardmapping); } var mapping = await _DataAccess.DeckDataAccess.GetDeckCardMappingAsync(deckId, card.Id); if (mapping != null) { mapping.Count++; await _DataAccess.DeckDataAccess.UpsertDeckCardMappingAsync(mapping); } else { mapping = new DeckCardMapping(); mapping.CardId = card.Id; mapping.DeckId = deckId; mapping.Count = 1; await _DataAccess.DeckDataAccess.UpsertDeckCardMappingAsync(mapping); } } return(RedirectToAction("Details", model)); }
public async Task <IActionResult> AddCard(string name, string query) { var model = new SearchDetailsViewModel(); model.Query = query; model.Name = name; var cardData = await _DataAccess.CardDataAccess.GetCardByNameAsync(name); if (cardData == null) { var apiCard = await _YGOProDeckAccess.GetCardByNameAsync(name); if (apiCard.CardImages != null && apiCard.CardImages.Count > 0) { apiCard.ImageBytes = _YGOProDeckAccess.GetCardImage(apiCard.CardImages.First()); } cardData = await _DataAccess.CardDataAccess.Upsert(apiCard); } var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); // will give the user's userId if (string.IsNullOrWhiteSpace(userId)) { return(RedirectToAction("Home", "Index")); } var userInventory = await _DataAccess.UserInventoryDataAccess.GetUserInventoryByUserAsync(userId); var cardmapping = await _DataAccess.CardDataAccess.GetSpecificCardMappingForUserAsync(userInventory.Id, cardData.Id); if (cardmapping != null) { cardmapping.Count++; await _DataAccess.CardDataAccess.UpsertCardMappingAsync(cardmapping); } else { cardmapping = new InventoryCardMapping(); cardmapping.CardId = cardData.Id; cardmapping.UserInventoryId = userInventory.Id; cardmapping.Count = 1; await _DataAccess.CardDataAccess.UpsertCardMappingAsync(cardmapping); } return(RedirectToAction("Details", model)); }
public async Task <InventoryCardMapping> UpsertCardMappingAsync(InventoryCardMapping inventoryCardMapping) { var mapping = _context.InventoryCardMappings.FirstOrDefault(x => x.UserInventoryId == inventoryCardMapping.UserInventoryId && x.CardId == inventoryCardMapping.CardId); if (mapping != null) { mapping.Count = inventoryCardMapping.Count; } else { mapping = new InventoryCardMapping(); mapping.Count = 1; mapping.CardId = inventoryCardMapping.CardId; mapping.UserInventoryId = inventoryCardMapping.UserInventoryId; _context.InventoryCardMappings.Add(mapping); } await _context.SaveChangesAsync(); return(mapping); }