public static string ImportUsers(VaporStoreDbContext context, string jsonString) { StringBuilder sb = new StringBuilder(); var usersDto = JsonConvert.DeserializeObject <IEnumerable <UserImportModel> >(jsonString); foreach (var currentUser in usersDto) { if (!IsValid(currentUser) || !currentUser.Cards.All(IsValid)) { sb.AppendLine("Invalid Data"); continue; } var user = new User { FullName = currentUser.FullName, Username = currentUser.Username, Age = currentUser.Age, Email = currentUser.Email, Cards = currentUser.Cards.Select(x => new Card { Number = x.Number, Cvc = x.Cvc, Type = Enum.Parse <CardType>(x.Type) }).ToList() }; context.Users.Add(user); sb.AppendLine($"Imported {user.Username} with {user.Cards.Count} cards"); } context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportPurchases(VaporStoreDbContext context, string xmlString) { XmlSerializer serializer = new XmlSerializer(typeof(PurchaseDto[]), new XmlRootAttribute("Purchases")); PurchaseDto[] purchaseDtos = (PurchaseDto[])serializer.Deserialize(new StringReader(xmlString)); StringBuilder sb = new StringBuilder(); List <Purchase> validPurchases = new List <Purchase>(); foreach (var purchaseDto in purchaseDtos) { if (!IsValid(purchaseDto)) { sb.AppendLine(ERROR_MESSAGE); continue; } Purchase purchase = Mapper.Map <Purchase>(purchaseDto); purchase.CardId = context.Cards.SingleOrDefault(c => c.Number == purchaseDto.Card).Id; purchase.GameId = context.Games.SingleOrDefault(c => c.Name == purchaseDto.Title).Id; purchase.Card = context.Cards.SingleOrDefault(c => c.Number == purchaseDto.Card); purchase.Game = context.Games.SingleOrDefault(c => c.Name == purchaseDto.Title); validPurchases.Add(purchase); sb.AppendLine($"Imported {purchaseDto.Title} for {purchase.Card.User.Username}"); } context.Purchases.AddRange(validPurchases); context.SaveChanges(); var result = sb.ToString(); return(result); }
public static string ImportPurchases(VaporStoreDbContext context, string xmlString) { var xmlSerializer = new XmlSerializer(typeof(PurchaseDto[]), new XmlRootAttribute("Purchases")); var deserializedPurchases = (PurchaseDto[])xmlSerializer.Deserialize(new StringReader(xmlString)); var sb = new StringBuilder(); var purchases = new List <Purchase>(); foreach (var purchaseDto in deserializedPurchases) { if (!IsValid(purchaseDto)) { sb.AppendLine(FailureMsg); continue; } var isTypeValid = Enum.TryParse(purchaseDto.Type, out PurchaseType purType); if (!isTypeValid) { sb.AppendLine(FailureMsg); continue; } var card = context.Cards.FirstOrDefault(c => c.Number == purchaseDto.Card); if (card == null) { sb.AppendLine(FailureMsg); continue; } var game = context.Games.FirstOrDefault(g => g.Name == purchaseDto.Title); if (game == null) { sb.AppendLine(FailureMsg); continue; } var date = DateTime.ParseExact(purchaseDto.Date, XmlDateFormat, CultureInfo.InvariantCulture); var purchase = new Purchase() { Game = game, Card = card, Date = date, ProductKey = purchaseDto.Key, Type = purType }; purchases.Add(purchase); sb.AppendLine($"Imported {purchaseDto.Title} for {card.User.Username}"); } context.AddRange(purchases); context.SaveChanges(); return(sb.ToString().Trim()); }
public static string ImportPurchases(VaporStoreDbContext context, string xmlString) { var serializer = new XmlSerializer(typeof(PurchaseDto[]), new XmlRootAttribute("Purchases")); var deserialized = (PurchaseDto[])serializer.Deserialize(new StringReader(xmlString)); StringBuilder result = new StringBuilder(); foreach (var purchaseDto in deserialized) { if (!IsValid(purchaseDto)) { result.AppendLine("Invalid Data"); continue; } var purchase = new Purchase { Type = purchaseDto.Type, ProductKey = purchaseDto.Key, Card = context.Cards.FirstOrDefault(c => c.Number == purchaseDto.Card), Date = DateTime.ParseExact(purchaseDto.Date, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture), Game = context.Games.FirstOrDefault(x => x.Name == purchaseDto.Title) }; context.Purchases.Add(purchase); result.AppendLine($"Imported {purchaseDto.Title} for {purchase.Card.User.Username}"); } context.SaveChanges(); return(result.ToString().Trim()); }
public static string ImportUsers(VaporStoreDbContext context, string jsonString) { UserDto[] userDtos = JsonConvert.DeserializeObject <UserDto[]>(jsonString); StringBuilder sb = new StringBuilder(); List <User> validUsers = new List <User>(); foreach (var userDto in userDtos) { bool cardsAreValid = userDto.Cards.All(IsValid); if (!IsValid(userDto) || !cardsAreValid) { sb.AppendLine(ERROR_MESSAGE); continue; } User user = Mapper.Map <User>(userDto); userDto.Cards.ToList().ForEach(c => Mapper.Map <Card>(c)); validUsers.Add(user); sb.AppendLine($"Imported {user.Username} with {user.Cards.Count} cards"); } context.Users.AddRange(validUsers); context.SaveChanges(); var result = sb.ToString(); return(result); }
public static string ImportUsers(VaporStoreDbContext context, string jsonString) { var userDtos = JsonConvert.DeserializeObject <UserImportDto[]>(jsonString); var users = new HashSet <User>(); var result = new StringBuilder(); foreach (var dto in userDtos) { if (IsValid(dto) == false) { result.AppendLine(ErrorMsg); continue; } var user = AutoMapper.Mapper.Map <User>(dto); users.Add(user); result.AppendLine(String.Format(UserAddedMsg, user.Username, user.Cards.Count)); } context.Users.AddRange(users); context.SaveChanges(); return(result.ToString().TrimEnd()); }
public static string UpdateEmail(VaporStoreDbContext context, string username, string newEmail) { StringBuilder sb = new StringBuilder(); var user = context .Users .SingleOrDefault(u => u.Username == username); if (user == null) { return$ "User {username} not found"; } var emailAlreadyTaken = context.Users.Any(u => u.Email == newEmail); if (emailAlreadyTaken) { return($"Email {newEmail} is already taken"); } user.Email = newEmail; context.SaveChanges(); return($"Changed {user.Username}'s email successfully"); return(sb.ToString()); }
public static string ImportPurchases(VaporStoreDbContext context, string xmlString) { var sb = new StringBuilder(); var serializer = new XmlSerializer(typeof(PurchaseImportDto[]), new XmlRootAttribute("Purchases")); var deserialized = (PurchaseImportDto[])serializer.Deserialize(new StringReader(xmlString)); var validPurchases = new List <Purchase>(); foreach (var purchaseDto in deserialized) { if (!IsValid(purchaseDto)) { sb.AppendLine("Invalid Data"); continue; } var game = context.Games.Single(g => g.Name == purchaseDto.Title); var card = context.Cards.Include(c => c.User).Single(c => c.Number == purchaseDto.Card); var date = DateTime.ParseExact(purchaseDto.Date, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture); var purchase = new Purchase(game, purchaseDto.Type, card, purchaseDto.Key, date); validPurchases.Add(purchase); sb.AppendLine($"Imported {purchase.Game.Name} for {purchase.Card.User.Username}"); } context.Purchases.AddRange(validPurchases); context.SaveChanges(); var result = sb.ToString(); return(result); }
public static string UpdateEmail(VaporStoreDbContext context, string username, string newEmail) { var user = context .Users .FirstOrDefault(u => u.Username == username); if (user == null) { return($"User {username} not found"); } var userWithEmail = context .Users .FirstOrDefault(u => u.Email == newEmail); if (userWithEmail != null) { return($"Email {newEmail} is already taken"); } user.Email = newEmail; context.SaveChanges(); return($"Changed {username}'s email successfully"); }
public static string ImportUsers(VaporStoreDbContext context, string jsonString) { var deserializedUsers = JsonConvert.DeserializeObject <UserDto[]>(jsonString); var users = new List <User>(); StringBuilder sb = new StringBuilder(); foreach (var dto in deserializedUsers) { if (!IsValid(dto) || !dto.Cards.All(IsValid)) { sb.AppendLine(ErrorMessage); continue; } var user = new User(dto.FullName, dto.Username, dto.Email, dto.Age, dto.Cards); users.Add(user); sb.AppendLine(string.Format(SuccessImportUsers, user.Username, user.Cards.Count)); } context.Users.AddRange(users); context.SaveChanges(); string result = sb.ToString(); return(result); }
public static string UpdateEmail(VaporStoreDbContext context, string username, string newEmail) { var targetUser = context.Users.FirstOrDefault(x => x.Username == username); string result; if (targetUser != null) { var isTaken = context.Users.FirstOrDefault(x => x.Email == newEmail) != null; if (!isTaken) { targetUser.Email = newEmail; result = String.Format(SuccessChangeOfEmailMessage, username); context.SaveChanges(); } else { result = String.Format(TakenEmailMessage, newEmail); } } else { result = String.Format(InvalidUserMessage, username); } return(result); }
public static string ImportUsers(VaporStoreDbContext context, string jsonString) { StringBuilder sb = new StringBuilder(); var usersDto = JsonConvert.DeserializeObject <IEnumerable <ImportUserDTO> >(jsonString); List <User> users = new List <User>(); foreach (var userDTO in usersDto) { if (!IsValid(userDTO) || !userDTO.Cards.All(IsValid)) { sb.AppendLine("Invalid Data"); continue; } bool isValidEnum = false; var currUser = new User { Username = userDTO.Username, FullName = userDTO.FullName, Email = userDTO.Email, Age = userDTO.Age }; foreach (var card in userDTO.Cards) { bool isParsed = Enum.TryParse <CardType>(card.Type, out CardType result); if (!isParsed) { isValidEnum = true; break; } if (isValidEnum) { sb.AppendLine("Invalid Data"); continue; } currUser.Cards.Add(new Card { Number = card.Number, Cvc = card.CVC, Type = Enum.Parse <CardType>(card.Type) }); } users.Add(currUser); sb.AppendLine($"Imported {userDTO.Username} with {userDTO.Cards.Count()} cards"); } context.Users.AddRange(users); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportUsers(VaporStoreDbContext context, string jsonString) { jsonUser_inp_dto[] usersDTOs = JsonConvert.DeserializeObject <jsonUser_inp_dto[]>(jsonString); StringBuilder sb = new StringBuilder(); List <User> users = new List <User>(); foreach (var dto in usersDTOs) { if (!AttributeValidation.IsValid(dto)) { sb.AppendLine("Invalid Data"); continue; } var user = new User() { FullName = dto.FullName, Age = dto.Age, Email = dto.Email, Username = dto.Username, Cards = dto.Cards.Select(x => new Card() { Cvc = x.Cvc, Number = x.Number, Type = (CardType)Enum.Parse(typeof(CardType), x.Type, true) }).ToArray() }; sb.AppendLine($"Imported {user.Username} with {user.Cards.Count} cards"); users.Add(user); } context.Users.AddRange(users); context.SaveChanges(); return(sb.ToString().Trim()); }
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 ImportUsers(VaporStoreDbContext context, string jsonString) { var sb = new StringBuilder(); var usersDtos = JsonConvert.DeserializeObject <ImportUserDto[]>(jsonString); List <User> users = new List <User>(); foreach (var userDto in usersDtos) { if (!IsValid(userDto)) { sb.AppendLine(ErrorMessage); continue; } var validUser = new User { FullName = userDto.FullName, Username = userDto.Username, Email = userDto.Email, Age = userDto.Age, }; foreach (var cardDto in userDto.Cards) { if (!IsValid(cardDto)) { sb.AppendLine(ErrorMessage); break; } CardType type; bool isValidCardType = Enum.TryParse(cardDto.Type, out type); if (!isValidCardType) { sb.AppendLine(ErrorMessage); break; } Card card = new Card { Number = cardDto.Number, Cvc = cardDto.CVC, Type = type, User = validUser }; validUser.Cards.Add(card); } users.Add(validUser); sb.AppendLine($"Imported {validUser.Username} with {validUser.Cards.Count} cards"); } context.Users.AddRange(users); context.SaveChanges(); return(sb.ToString().Trim()); }
public static string ImportUsers(VaporStoreDbContext context, string jsonString) { var userDtos = JsonConvert.DeserializeObject <UserDto[]>(jsonString); var validUsers = new List <User>(); StringBuilder sb = new StringBuilder(); foreach (var userDto in userDtos) { if (!IsValid(userDto) || userDto.Cards.Length == 0 || userDto.Cards.Any(c => !IsValid(c))) { sb.AppendLine("Invalid Data"); continue; } var user = Mapper.Map <User>(userDto); validUsers.Add(user); sb.AppendLine($"Imported {user.Username} with {user.Cards.Count} cards"); } context.Users.AddRange(validUsers); context.SaveChanges(); return(sb.ToString()); }
public static string ImportPurchases(VaporStoreDbContext context, string xmlString) { var serializer = new XmlSerializer(typeof(List <PurchaseDto>), new XmlRootAttribute("Purchases")); var importedDtos = (List <PurchaseDto>)serializer.Deserialize(new StringReader(xmlString)); var result = new StringBuilder(); var validPurchases = new List <Purchase>(); foreach (var purchaseDto in importedDtos) { if (!IsValid(purchaseDto)) { result.AppendLine(ErrorMessage); continue; } if (!Enum.TryParse <PurchaseType>(purchaseDto.Type, out PurchaseType type)) { result.AppendLine(ErrorMessage); continue; } var card = context.Cards.Include(c => c.User).FirstOrDefault(c => c.Number == purchaseDto.Card); if (card is null) { result.AppendLine(ErrorMessage); continue; } var date = DateTime.ParseExact(purchaseDto.Date, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture); var game = context.Games.FirstOrDefault(g => g.Name == purchaseDto.Game); if (game is null) { result.AppendLine(ErrorMessage); continue; } var purchase = new Purchase { Type = type, ProductKey = purchaseDto.Key, Card = card, Date = date, Game = game }; validPurchases.Add(purchase); result.AppendLine($"Imported {purchase.Game.Name} for {purchase.Card.User.Username}"); } context.Purchases.AddRange(validPurchases); context.SaveChanges(); return(result.ToString()); }
public static string ImportPurchases(VaporStoreDbContext context, string xmlString) { StringBuilder sb = new StringBuilder(); var serializer = new XmlSerializer(typeof(PurchaseDto[]), new XmlRootAttribute("Purchases")); var purchases = (PurchaseDto[])serializer.Deserialize(new StringReader(xmlString)); var pList = new List <Purchase>(); foreach (var purchase in purchases) { if (!IsValid(purchase)) { sb.AppendLine("Invalid Data"); continue; } var cPurchase = new Purchase { Date = DateTime.ParseExact(purchase.Date, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture), ProductKey = purchase.Key, Type = (PurchaseType)Enum.Parse(typeof(PurchaseType), purchase.Type) }; var Card = context.Cards.FirstOrDefault(x => x.Number == purchase.Card); var Game = context.Games.FirstOrDefault(x => x.Name == purchase.Title); cPurchase.Card = Card; cPurchase.Game = Game; sb.AppendLine($"Imported {cPurchase.Game.Name} for {cPurchase.Card.User.Username}"); pList.Add(cPurchase); } context.Purchases.AddRange(pList); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportUsers(VaporStoreDbContext context, string jsonString) { StringBuilder sb = new StringBuilder(); List <User> users = new List <User>(); ImportUsersDto[] importUsers = JsonConvert.DeserializeObject <ImportUsersDto[]>(jsonString); foreach (var userToAdd in importUsers) { if (!IsValid(userToAdd)) { sb.AppendLine(ErrorMessage); continue; } if (userToAdd.Cards.Length == 0) { sb.AppendLine(ErrorMessage); continue; } User user = new User() { FullName = userToAdd.FullName, Username = userToAdd.Username, Email = userToAdd.Email, Age = userToAdd.Age }; foreach (var itemCard in userToAdd.Cards) { if (!IsValid(itemCard)) { sb.AppendLine(ErrorMessage); continue; } CardType typeCard; bool isValid = Enum.TryParse(itemCard.Type, out typeCard); if (!isValid) { sb.AppendLine(ErrorMessage); } Card card = new Card() { Cvc = itemCard.Cvc, Number = itemCard.Number, Type = typeCard, User = user }; user.Cards.Add(card); } users.Add(user); sb.AppendLine(string.Format(SuccessfullyAddedUser, user.Username, user.Cards.Count)); } context.Users.AddRange(users); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportUsers(VaporStoreDbContext context, string jsonString) { StringBuilder sb = new StringBuilder(); ImportUserJsonDto[] jsonDto = JsonConvert.DeserializeObject <ImportUserJsonDto[]>(jsonString); List <User> users = new List <User>(); foreach (var obj in jsonDto) { if (!IsValid(obj)) { sb.AppendLine(ErrorMessage); continue; } User user = new User() { Username = obj.Username, FullName = obj.FullName, Email = obj.FullName, Age = obj.Age }; foreach (var card in obj.Cards) { if (!IsValid(card)) { sb.AppendLine(ErrorMessage); continue; } int cardType = -1; if (card.Type == "Debit") { cardType = 0; } else if (card.Type == "Credit") { cardType = 1; } else { sb.AppendLine(ErrorMessage); continue; } Card addCard = new Card { Number = card.Number, Cvc = card.CVC, Type = (CardType)cardType }; user.Cards.Add(addCard); } users.Add(user); sb.AppendLine($"Imported {user.Username} with {user.Cards.Count} cards"); } context.Users.AddRange(users); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportUsers(VaporStoreDbContext context, string jsonString) { var userDtos = JsonConvert.DeserializeObject <ImportUserDto[]>(jsonString); List <User> users = new List <User>(); var sb = new StringBuilder(); foreach (var userDto in userDtos) { bool validCardType = true; if (!IsValid(userDto) || userDto.Cards.Count == 0) { sb.AppendLine("Invalid Data"); continue; } User user = new User { FullName = userDto.FullName, Username = userDto.Username, Email = userDto.Email, Age = userDto.Age }; foreach (var card in userDto.Cards) { if (!IsValid(card) || userDto.Cards.Any(x => x.Type != "Debit" && x.Type != "Credit")) { sb.AppendLine("Invalid Data"); validCardType = false; break; } } if (validCardType == false) { continue; } foreach (var card in userDto.Cards) { user.Cards.Add(GetCard(context, card)); } sb.AppendLine($"Imported {user.Username} with {user.Cards.Count} cards"); users.Add(user); } context.Users.AddRange(users); context.SaveChanges(); string result = sb.ToString().TrimEnd(); return(result); }
public static string ImportUsers(VaporStoreDbContext context, string jsonString) { var usersDTO = JsonConvert.DeserializeObject <IEnumerable <ImportUserDTO> >(jsonString); var sb = new StringBuilder(); var users = new List <User>(); foreach (var userDTO in usersDTO) { if (!IsValid(userDTO)) { sb.AppendLine(errorMessage); continue; } var user = new User() { Username = userDTO.Username, FullName = userDTO.FullName, Email = userDTO.Email, Age = userDTO.Age }; foreach (var cardDTO in userDTO.Cards) { if (!IsValid(cardDTO)) { sb.AppendLine(errorMessage); continue; } CardType cardType; var isValidEnum = Enum.TryParse(cardDTO.Type, out cardType); if (!isValidEnum) { sb.AppendLine(errorMessage); continue; } var card = new Card() { Number = cardDTO.Number, Cvc = cardDTO.CVC, Type = cardType, }; user.Cards.Add(card); } users.Add(user); sb.AppendLine(string.Format(successsMessageImportUser, user.Username, user.Cards.Count)); } context.Users.AddRange(users); context.SaveChanges(); return(sb.ToString().Trim()); }
public static string ImportGames(VaporStoreDbContext context, string jsonString) { StringBuilder sb = new StringBuilder(); ImportGameDto[] gamesDto = JsonConvert.DeserializeObject <ImportGameDto[]>(jsonString); List <Game> games = new List <Game>(); foreach (var jsonGame in gamesDto) { if (!IsValid(jsonGame) || jsonGame.Tags.Count() == 0) { sb.AppendLine(ERROR_MESSAGE); continue; } Genre genre = context.Genres .FirstOrDefault(x => x.Name == jsonGame.Genre) ?? new Genre { Name = jsonGame.Genre }; Developer developer = context.Developers .FirstOrDefault(x => x.Name == jsonGame.Developer) ?? new Developer { Name = jsonGame.Developer }; Game game = new Game { Name = jsonGame.Name, Genre = genre, Developer = developer, Price = jsonGame.Price, ReleaseDate = jsonGame.ReleaseDate.Value }; foreach (var jsonTag in jsonGame.Tags) { Tag tag = context.Tags .FirstOrDefault(x => x.Name == jsonTag) ?? new Tag { Name = jsonTag }; game.GameTags.Add(new GameTag { Tag = tag }); } context.Games.Add(game); context.SaveChanges(); sb.AppendLine(string.Format(SUCCESSFULL_GAME_IMPORT, game.Name, game.Genre.Name, game.GameTags.Count)); } return(sb.ToString().TrimEnd()); }
public static string ImportGames(VaporStoreDbContext context, string jsonString) { //var gamesDto = JsonConvert.DeserializeObject<List<ImportGameDto>>(jsonString) // .Where(x=> String.IsNullOrEmpty(x.Name)==false // && x.Price>=0 && x.Price<= decimal.MaxValue // && String.IsNullOrEmpty(x.Developer)==false // && String.IsNullOrEmpty(x.Genre)==false // && x.Tags.Count>=1) // .ToList(); var gamesDto = JsonConvert.DeserializeObject <List <ImportGameDto> >(jsonString) .ToList(); ; var sb = new StringBuilder(); var gamesList = new List <Game>(); foreach (var gameDto in gamesDto) { if (!IsValid(gameDto) || gameDto.Tags.Count == 0) { sb.AppendLine("Invalid Data"); continue; } var developer = GetDeveloper(context, gameDto.Developer); var genre = GetGenre(context, gameDto.Genre); var releaseDate = DateTime.ParseExact(gameDto.ReleaseDate, "yyyy-MM-dd", CultureInfo.InvariantCulture); var game = new Game() { Name = gameDto.Name, Price = gameDto.Price, ReleaseDate = releaseDate, Genre = genre, Developer = developer }; foreach (var currentTag in gameDto.Tags) { var tag = GetTag(context, currentTag); game.GameTags.Add(new GameTag { Game = game, Tag = tag }); } gamesList.Add(game); sb.AppendLine($"Added {gameDto.Name} ({game.Genre.Name}) with {game.GameTags.Count} tags"); } context.Games.AddRange(gamesList); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportGames(VaporStoreDbContext context, string jsonString) { var games = JsonConvert.DeserializeObject <IEnumerable <ImportGamesDevsGenresTagsDTO> >(jsonString); var sb = new StringBuilder(); foreach (var jsonGame in games) { if (!IsValid(jsonGame) || jsonGame.Tags.Count() == 0) { sb.AppendLine("Invalid Data"); continue; } var genre = context.Genres.FirstOrDefault(x => x.Name == jsonGame.Genre) ?? new Genre { Name = jsonGame.Genre }; var developer = context.Developers.FirstOrDefault(x => x.Name == jsonGame.Developer) ?? new Developer { Name = jsonGame.Developer }; //var developer = context.Developers.FirstOrDefault(x => x.Name == jsonGame.Developer); //if (developer == null) //{ // developer = new Developer { Name = jsonGame.Developer }; //} var game = new Game { Name = jsonGame.Name, Price = jsonGame.Price, ReleaseDate = jsonGame.ReleaseDate.Value, Developer = developer, Genre = genre }; foreach (var jsonTag in jsonGame.Tags) { var tag = context.Tags.FirstOrDefault(x => x.Name == jsonTag) ?? new Tag { Name = jsonTag }; game.GameTags.Add(new GameTag { Tag = tag }); } context.Games.Add(game); context.SaveChanges(); sb.AppendLine($"Added {jsonGame.Name} ({jsonGame.Genre}) with {jsonGame.Tags.Count()} tags"); } return(sb.ToString().TrimEnd()); }
public static string ImportUsers(VaporStoreDbContext context, string jsonString) { StringBuilder sb = new StringBuilder(); ImportUserDto[] importUserDtos = JsonConvert.DeserializeObject <ImportUserDto[]>(jsonString); foreach (var userDto in importUserDtos) { if (!IsValid(userDto)) { sb.AppendLine(ErrorMessage); continue; } User currentUser = new User() { FullName = userDto.FullName, Username = userDto.Username, Email = userDto.Email, Age = int.Parse(userDto.Age) }; bool AreCardsValid = true; ICollection <Card> userCards = new List <Card>(); foreach (var cardDto in userDto.Cards) { if (!IsValid(cardDto) || (cardDto.Type != "Debit" && cardDto.Type != "Credit")) { AreCardsValid = false; break; } Card currentCard = new Card() { Number = cardDto.Number, Cvc = cardDto.Cvc, Type = (CardType)Enum.Parse(typeof(CardType), cardDto.Type) }; userCards.Add(currentCard); } if (!AreCardsValid) { sb.AppendLine(ErrorMessage); continue; } currentUser.Cards = userCards; sb.AppendLine($"Imported {currentUser.Username} with {currentUser.Cards.Count} cards"); context.Users.Add(currentUser); } context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportPurchases(VaporStoreDbContext context, string xmlString) { var attr = new XmlRootAttribute("Purchases"); var serializer = new XmlSerializer(typeof(List <PurchaseImportDto>), attr); StringBuilder sb = new StringBuilder(); var validPurchases = new List <Purchase>(); using (StringReader reader = new StringReader(xmlString)) { var purchasesDto = (List <PurchaseImportDto>)serializer.Deserialize(reader); foreach (var dto in purchasesDto) { if (!IsValid(dto)) { sb.AppendLine("Invalid Data"); continue; } var resultType = Enum.TryParse(dto.Type, out PurchaseType type); var card = context .Cards .FirstOrDefault(c => c.Number == dto.Card); var game = context .Games .FirstOrDefault(g => g.Name == dto.Title); if (resultType == false || card == null || game == null) { sb.AppendLine("Invalid Data"); continue; } var purchase = new Purchase { Type = type, ProductKey = dto.ProductKey, Date = DateTime.ParseExact(dto.Date, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture), Card = card, Game = game }; validPurchases.Add(purchase); sb.AppendLine($"Imported {purchase.Game.Name} for {purchase.Card.User.Username}"); } } context.Purchases.AddRange(validPurchases); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportUsers(VaporStoreDbContext context, string jsonString) { var cardsDto = JsonConvert.DeserializeObject <ImportUsersDto[]>(jsonString); StringBuilder sb = new StringBuilder(); var users = new List <User>(); foreach (var cardDto in cardsDto) { bool isValidCard = IsValid(cardDto); if (!isValidCard) { sb.AppendLine(ErrorMessage); continue; } User user = new User { FullName = cardDto.FullName, Username = cardDto.Username, Email = cardDto.Username, Age = cardDto.Age, }; user.Cards = new HashSet <Card>(); bool cardsHaveValidType = cardDto.Cards.All(x => Enum.IsDefined(typeof(CardType), x.Type)); bool cardsAreValid = cardDto.Cards.All(x => IsValid(x)); if (cardsHaveValidType && cardsAreValid) { foreach (var card in cardDto.Cards) { user.Cards.Add(new Card { Number = card.Number, Cvc = card.Cvc, Type = card.Type }); } users.Add(user); } else { sb.AppendLine(ErrorMessage); continue; } sb.AppendLine($"Imported {user.Email} with {user.Cards.Count()} cards"); //bool isValidCardType = Enum.IsDefined(typeof(CardType), cardDto.Cards); } context.Users.AddRange(users); context.SaveChanges(); return(sb.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 ImportUsers(VaporStoreDbContext context, string jsonString) { var resultMessages = new List <string>(); var usersDto = JsonConvert.DeserializeObject <ImportUsersDto[]>(jsonString); var users = new List <User>(); foreach (var userDto in usersDto) { if (!Regex.Match(userDto.FullName, @"[A-Z][a-z]+ [A-Z][a-z]+").Success || userDto.FullName == "" || userDto.Username.Length < 3 || userDto.Username.Length > 20 || userDto.Email == null || userDto.Email == "" || userDto.Age < 3 || userDto.Age > 103 || userDto.Cards == null || userDto.Cards.Count == 0) { resultMessages.Add(InvalidMessage); continue; } var user = new User() { FullName = userDto.FullName, Username = userDto.Username, Email = userDto.Email, Age = userDto.Age }; foreach (var userDtoCard in userDto.Cards) { var cardTypeParse = Enum.TryParse <CardType>(userDtoCard.Type, out CardType cardType); if (!Regex.Match(userDtoCard.Number, @"[0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4}").Success || !Regex.Match(userDtoCard.Cvc, @"[0-9]{3}").Success || cardTypeParse == false || cardType == null) { resultMessages.Add(InvalidMessage); continue; } Card card = new Card() { Number = userDtoCard.Number, Cvc = userDtoCard.Cvc, Type = cardType }; user.Cards.Add(card); } users.Add(user); resultMessages.Add($"Imported {user.Username} with {user.Cards.Count} cards"); } context.Users.AddRange(users); context.SaveChanges(); return(string.Join(Environment.NewLine, resultMessages)); }