public static string ImportPurchases(VaporStoreDbContext context, string xmlString) { StringBuilder sb = new StringBuilder(); var purchaseDtos = XmlConverter.Deserializer <PurchaseDto>(xmlString, "Purchases"); List <Purchase> purchases = new List <Purchase>(); foreach (var purchaseDto in purchaseDtos) { if (!IsValid(purchaseDto)) { sb.AppendLine(ErrorMessage); continue; } Purchase purchase = new Purchase() { Game = context.Games.First(x => x.Name == purchaseDto.title), Type = Enum.Parse <PurchaseType>(purchaseDto.Type), ProductKey = purchaseDto.Key, Card = context.Cards.First(x => x.Number == purchaseDto.Card), Date = DateTime.ParseExact(purchaseDto.Date, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture) }; purchases.Add(purchase); sb.AppendLine($"Imported {purchase.Game.Name} for {purchase.Card.User.Username}"); } context.Purchases.AddRange(purchases); context.SaveChanges(); return(sb.ToString()); }
public static string ImportPurchases(VaporStoreDbContext context, string xmlString) { var outputResult = new StringBuilder(); var validPurchasesToAdd = new List <Purchase>(); var purchasesDtos = XmlConverter.Deserializer <ImportPurchaseDto>(xmlString, "Purchases"); foreach (var purchaseDto in purchasesDtos) { PurchaseType purchaseType; var isGenreValid = Enum.TryParse <PurchaseType>(purchaseDto.Type, true, out purchaseType); var targetGame = context.Games.FirstOrDefault(x => x.Name == purchaseDto.Title); var targetCard = context.Cards.FirstOrDefault(x => x.Number == purchaseDto.Card); if (!IsValid(purchaseDto) || targetGame == null || !isGenreValid || targetCard == null) { outputResult.AppendLine(ErrorMessage); continue; } var newPurchase = new Purchase { Game = targetGame, Type = purchaseType, ProductKey = purchaseDto.ProductKey, Date = DateTime.ParseExact(purchaseDto.Date, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture), Card = targetCard }; validPurchasesToAdd.Add(newPurchase); outputResult.AppendLine(string.Format(SuccessfullyImportedPurchase, newPurchase.Game.Name, newPurchase.Card.User.Username)); } context.AddRange(validPurchasesToAdd); context.SaveChanges(); return(outputResult.ToString().TrimEnd()); }
public static string ImportPurchases(VaporStoreDbContext context, string xmlString) { StringBuilder sb = new StringBuilder(); ImportPurchaseDto[] purchasesDto = XmlConverter.Deserializer <ImportPurchaseDto>(xmlString, "Purchases"); List <Purchase> purchases = new List <Purchase>(); foreach (var pDto in purchasesDto) { if (!IsValid(pDto)) { sb.AppendLine(ERROR_MESSAGE); continue; } DateTime date; bool isValidDate = DateTime.TryParseExact(pDto.Date, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out date); if (!isValidDate) { sb.AppendLine(ERROR_MESSAGE); continue; } Game game = context.Games.FirstOrDefault(x => x.Name == pDto.Title); if (game == null) { sb.AppendLine(ERROR_MESSAGE); continue; } Card card = context.Cards.FirstOrDefault(x => x.Number == pDto.Card); if (card == null) { sb.AppendLine(ERROR_MESSAGE); continue; } Purchase purchase = new Purchase { Type = pDto.Type.Value, ProductKey = pDto.Key, Date = date, Card = card, Game = game }; purchases.Add(purchase); sb.AppendLine(string.Format(SUCCESSFULL_PURCHASE_IMPORT, game.Name, card.User.Username)); } context.Purchases.AddRange(purchases); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportPurchases(VaporStoreDbContext context, string xmlString) { var sb = new StringBuilder(); var purchases = new List <Purchase>(); var purchasesDto = XmlConverter.Deserializer <PurchaseImportModel>(xmlString, "Purchases"); foreach (var currPurchase in purchasesDto) { if (!IsValid(currPurchase)) { sb.AppendLine("Invalid Data"); continue; } DateTime date; var isDateValid = DateTime.TryParseExact(currPurchase.Date, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out date); if (!isDateValid) { sb.AppendLine("Invalid Data"); continue; } var card = context.Cards.FirstOrDefault(x => x.Number == currPurchase.Card); var game = context.Games.FirstOrDefault(x => x.Name == currPurchase.GameName); if (card == null || game == null) { sb.AppendLine("Invalid Data"); continue; } var purchase = new Purchase { Type = Enum.Parse <PurchaseType>(currPurchase.Type), ProductKey = currPurchase.ProductKey, Card = card, Date = date, Game = game }; purchases.Add(purchase); sb.AppendLine($"Imported {game.Name} for {card.User.Username}"); } context.Purchases.AddRange(purchases); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportPurchases(VaporStoreDbContext context, string xmlString) { StringBuilder sb = new StringBuilder(); var purchasesToImport = XmlConverter.Deserializer <PurchaseInputModel>(xmlString, "Purchases"); foreach (var purchase in purchasesToImport) { var isValidDate = DateTime.TryParseExact( purchase.Date, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime currentDate); if (!IsValid(purchase) || !isValidDate) { sb.AppendLine(ErrorMessage); continue; } Card currentCard = context.Cards.FirstOrDefault(c => c.Number == purchase.Card); if (currentCard == null) { sb.AppendLine(ErrorMessage); continue; } Game currentGame = context.Games.FirstOrDefault(g => g.Name == purchase.Title); if (currentGame == null) { sb.AppendLine(ErrorMessage); continue; } var purchaseToAdd = new Purchase { Type = Enum.Parse <PurchaseType>(purchase.Type), ProductKey = purchase.ProductKey, Date = currentDate, Card = currentCard, Game = currentGame }; context.Purchases.Add(purchaseToAdd); sb.AppendLine($"Imported {currentGame.Name} for {purchaseToAdd.Card.User.Username}"); } context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportPurchases(VaporStoreDbContext context, string xmlString) { var sb = new StringBuilder(); List <Purchase> purchasesToAdd = new List <Purchase>(); PurchaseXmlInputModel[] purchases = XmlConverter.Deserializer <PurchaseXmlInputModel>(xmlString, "Purchases"); foreach (var currentPurchase in purchases) { if (!IsValid(currentPurchase)) { sb.AppendLine("Invalid Data"); continue; } Card card = context.Cards.FirstOrDefault(c => c.Number == currentPurchase.Card); Game game = context.Games.FirstOrDefault(g => g.Name == currentPurchase.gameTitle); DateTime date; bool isDateValid = DateTime.TryParseExact( currentPurchase.Date, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out date); if (!isDateValid) { sb.AppendLine("Invalid Data"); continue; } Purchase purchase = new Purchase { ProductKey = currentPurchase.Key, Type = Enum.Parse <PurchaseType>(currentPurchase.Type), Game = game, Card = card, Date = date }; purchasesToAdd.Add(purchase); sb.AppendLine($"Imported {purchase.Game.Name} for {purchase.Card.User.Username}"); } context.Purchases.AddRange(purchasesToAdd); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportPurchases(VaporStoreDbContext context, string xmlString) { var xml = XmlConverter.Deserializer <PurchasesXmlInputModel>(xmlString, "Purchases"); var sb = new StringBuilder(); foreach (var purchase in xml) { if (!IsValid(purchase)) { sb.AppendLine(ERROR_MSG); continue; } var date = DateTime.ParseExact(purchase.Date, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None); var card = context.Cards.FirstOrDefault(x => x.Number == purchase.Card); var game = context.Games.FirstOrDefault(x => x.Name == purchase.Title); var purchaseToAdd = new Purchase() { Game = game, Type = (PurchaseType)Enum.Parse(typeof(PurchaseType), purchase.Type), ProductKey = purchase.Key, Date = date, Card = card, }; var user = context.Users .SelectMany(x => x.Cards, (a, d) => new { a.Username, d.Number, }).FirstOrDefault(x => x.Number == purchase.Card); sb.AppendLine($"Imported {purchase.Title} for {user.Username}"); context.Add(purchaseToAdd); context.SaveChanges(); } return(sb.ToString().TrimEnd()); }
public static string ImportPurchases(VaporStoreDbContext context, string xmlString) { var sb = new StringBuilder(); var purchaseDtos = XmlConverter.Deserializer <ImportPurchases>(xmlString, "Purchases"); var purchases = new List <Purchase>(); foreach (var purchaseDto in purchaseDtos) { if (!IsValid(purchaseDto)) { sb.AppendLine(ErrorMessage); continue; } DateTime date; bool isdateValid = DateTime.TryParseExact(purchaseDto.Date, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out date); PurchaseType type; bool IsTypeValid = Enum.TryParse(purchaseDto.Type, out type); var card = context.Cards.FirstOrDefault(x => x.Number == purchaseDto.CardNumber); var game = context.Games.FirstOrDefault(x => x.Name == purchaseDto.Title); var purchase = new Purchase { Type = type, ProductKey = purchaseDto.Key, Card = card, Date = date, Game = game }; purchases.Add(purchase); sb.AppendLine($"Imported {purchase.Game.Name} for {card.User.Username}"); } context.Purchases.AddRange(purchases); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportPurchases(VaporStoreDbContext context, string xmlString) { StringBuilder sb = new StringBuilder(); var data = XmlConverter.Deserializer <ImportPurchasesDTO>(xmlString, "Purchases"); List <Purchase> purchases = new List <Purchase>(); foreach (var item in data) { var isValidDate = DateTime.TryParseExact(item.Date, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime date); if (!isValidDate) { sb.AppendLine("Invalid Data"); continue; } var card = context.Cards.FirstOrDefault(c => c.Number == item.Card); var username = card.User; var game = context.Games.FirstOrDefault(g => g.Name == item.Title); var purchase = new Purchase { Game = game, ProductKey = item.Key, Card = card, Date = date, }; purchases.Add(purchase); sb.AppendLine(string.Format("Imported {0} for {1}", game.Name, username.Username)); } context.Purchases.AddRange(purchases); context.SaveChanges(); return(sb.ToString()); }
public static string ImportPurchases(VaporStoreDbContext context, string xmlString) { StringBuilder sb = new StringBuilder(); var purcasesXml = XmlConverter.Deserializer <PurchaseInputModel>(xmlString, "Purchases"); List <Purchase> purchases = new List <Purchase>(); foreach (var xmlPurchase in purcasesXml) { if (!IsValid(xmlPurchase)) { sb.AppendLine(ErrorMessage); continue; } object purchaseTypeObj; bool isPurchaseTypeValid = Enum.TryParse(typeof(PurchaseType), xmlPurchase.PurchaiseType, out purchaseTypeObj); if (!isPurchaseTypeValid) { sb.AppendLine(ErrorMessage); continue; } PurchaseType purchaseType = (PurchaseType)purchaseTypeObj; DateTime date; bool isDateValid = DateTime.TryParseExact(xmlPurchase.Date, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out date); if (!isDateValid) { sb.AppendLine(ErrorMessage); continue; } var card = context.Cards.FirstOrDefault(x => x.Number == xmlPurchase.Card); var game = context.Games.FirstOrDefault(x => x.Name == xmlPurchase.GameTitle); if (card == null || game == null) { sb.AppendLine(ErrorMessage); continue; } var purchase = new Purchase { Type = purchaseType, ProductKey = xmlPurchase.Key, Date = date, Card = card, Game = game, }; purchases.Add(purchase); var username = context.Users. Where(x => x.Id == purchase.Card.UserId).Select(x => x.Username).FirstOrDefault(); //sb.AppendLine($"Imported {xmlPurchase.GameTitle} for {purchase.Card.User.Username}"); sb.AppendLine($"Imported {xmlPurchase.GameTitle} for {username}"); } context.Purchases.AddRange(purchases); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportPurchases(VaporStoreDbContext context, string xmlString) { StringBuilder sb = new StringBuilder(); string xmlRoot = "Purchases"; var purchases = XmlConverter.Deserializer <PurchaseDto>(xmlString, xmlRoot); List <Purchase> dbPurchases = new List <Purchase>(); var games = context.Games.ToList(); var cards = context.Cards .AsQueryable() .Include(c => c.User) .ToList(); foreach (var purchase in purchases) { if (!IsValid(purchase)) { sb.AppendLine("Invalid Data"); continue; } Purchase dbPurchase = new Purchase() { ProductKey = purchase.Key }; if (!DateTime .TryParseExact(purchase.Date, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime purchaseDate)) { sb.AppendLine("Invalid Data"); continue; } dbPurchase.Date = purchaseDate; if (!Enum.TryParse(typeof(PurchaseType), purchase.Type, out object purchaseType)) { sb.AppendLine("Invalid Data"); continue; } dbPurchase.Type = (PurchaseType)purchaseType; var card = cards.FirstOrDefault(c => c.Number == purchase.Card); if (card == null) { sb.AppendLine("Invalid Data"); continue; } dbPurchase.Card = card; var game = games.FirstOrDefault(g => g.Name == purchase.Title); if (game == null) { sb.AppendLine("Invalid Data"); continue; } dbPurchase.Game = game; dbPurchases.Add(dbPurchase); sb.AppendLine($"Imported {purchase.Title} for {card.User.Username}"); } context.Purchases.AddRange(dbPurchases); context.SaveChanges(); return(sb.ToString()); }