public static string ImportUsers(VaporStoreDbContext context, string jsonString) { var users = JsonConvert.DeserializeObject <List <UserDTO> >(jsonString); var sb = new StringBuilder(); var userfullNameRegex = new Regex(@"^[A-Z][a-z]+ [A-Z][a-z]+$"); var cardNumberRegex = new Regex(@"^[\d]{4} [\d]{4} [\d]{4} [\d]{4}$"); var cardCvcRegex = new Regex(@"^[\d]{3}$"); foreach (var user in users) { if (user.Username.Length < 3 || user.Username.Length > 20 || !userfullNameRegex.IsMatch(user.FullName) || user.Age < 3 || user.Age > 103 || string.IsNullOrEmpty(user.Email) || user.Cards.Count() == 0 || !user.Cards.All(x => cardNumberRegex.IsMatch(x.Number) && cardCvcRegex.IsMatch(x.Cvc) || !user.Cards.All(x => x.Type == CardType.Credit || x.Type == CardType.Debit))) { sb.AppendLine("Invalid Data"); continue; } var currentUser = new User() { Age = user.Age, Email = user.Email, FullName = user.FullName, Username = user.Username }; var cards = new List <Card>(); foreach (var card in user.Cards) { var currentCard = new Card() { Cvc = card.Cvc, Number = card.Number, Type = card.Type, User = currentUser }; if (!context.Cards.Any(x => x.Number == card.Number && x.Cvc == card.Cvc && x.Type == card.Type)) { context.Cards.Add(currentCard); } cards.Add(currentCard); currentUser.Cards.Add(currentCard); } context.Add(currentUser); sb.AppendLine($"Imported {currentUser.Username} with {currentUser.Cards.Count()} cards"); } context.SaveChanges(); return(sb.ToString().TrimEnd()); }
private static Developer GetDevelopoer(VaporStoreDbContext context, string gameDtoDeveloper) { var developer = context.Developers.FirstOrDefault(x => x.Name == gameDtoDeveloper); if (developer == null) { developer = new Developer { Name = gameDtoDeveloper }; context.Add(developer); context.SaveChanges(); } return(developer); }
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 serializer = new XmlSerializer(typeof(PurchaseImportDto[]), new XmlRootAttribute("Purchases")); var purchases = (PurchaseImportDto[])serializer.Deserialize(new StringReader(xmlString)); var sb = new StringBuilder(); var hasError = false; foreach (var purchase in purchases) { var productType = (PurchaseType)Enum.Parse(typeof(PurchaseType), purchase.Type); if (IsValid(purchase)) { var card = context.Cards.Where(x => x.Number == purchase.Card).FirstOrDefault(); var game = context.Games.Where(x => x.Name == purchase.GameName).FirstOrDefault(); var date = DateTime.ParseExact(purchase.Date, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture); var newPurchase = new Purchase() { ProductKey = purchase.ProductKey, Date = date, Card = card, Game = game, Type = productType }; var userName = context.Users.Where(x => x.Cards.Any(y => y.Number == card.Number)).Select(x => x.Username).FirstOrDefault(); context.Add(newPurchase); sb.AppendLine($"Imported {game.Name} for {userName}"); } else { hasError = true; } if (hasError) { sb.AppendLine(ErrorMessage); hasError = false; } } context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportGames(VaporStoreDbContext context, string jsonString) { var sb = new StringBuilder(); var deserializedGameDtos = JsonConvert.DeserializeObject <GameDto[]>(jsonString, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); var validGames = new List <Game>(); foreach (var gameDto in deserializedGameDtos) { var developerExists = validGames.Any(d => d.Developer.Name == gameDto.Developer); var genreExists = validGames.Any(g => g.Name == gameDto.Genre); if (!genreExists) { var currentGenre = new Genre { Name = gameDto.Genre }; context.Add(currentGenre); } if (!developerExists) { var currentDeveloper = new Developer { Name = gameDto.Developer }; context.Add(currentDeveloper); } //foreach (var tag in gameDto.Tags) //{ // var TagExists = validGames.Any(t => t.GameTags == gameDto.Tags); //} if (!IsValid(gameDto) || gameDto.Tags.Any(c => !IsValid(c))) { sb.AppendLine(FailureMessage); continue; } var releaseDate = DateTime.ParseExact(gameDto.ReleaseDate, "yyyy-MM-dd", CultureInfo.InvariantCulture); var game = new Game { Name = gameDto.Name, Price = gameDto.Price, ReleaseDate = releaseDate //, // Developer = gameDto.Developer, // Genre = gameDto.Genre }; foreach (var tagsDto in gameDto.Tags) { var tag = new Tag { Name = tagsDto.Name }; // game.GameTags.Add(tag); // cellsCount++; } // departments.Add(department); // sb.AppendLine($"Imported {department.Name} with {cellsCount} cells"); } // context.Departments.AddRange(departments); // context.SaveChanges(); var result = sb.ToString(); return(result); }