public static string ImportGames(VaporStoreDbContext context, string jsonString) { StringBuilder sb = new StringBuilder(); var gameDtos = JsonConvert.DeserializeObject <ImportGamesDto[]>(jsonString); var validGames = new List <Game>(); foreach (var gameDto in gameDtos) { GenericValidator.TryValidate(gameDto, out validationTestResults); if (validationTestResults.Count != 0) { sb.AppendLine("Invalid Data"); continue; } var game = CreateGame(gameDto, context); context.Games.Add(game); context.SaveChanges(); sb.AppendLine($"Added {game.Name} ({game.Genre.Name}) with {game.GameTags.Count} tags"); } return(sb.ToString().TrimEnd()); }
public static string ImportUsers(VaporStoreDbContext context, string jsonString) { StringBuilder sb = new StringBuilder(); var userDtos = JsonConvert.DeserializeObject <ImportUserDto[]>(jsonString); foreach (var userDto in userDtos) { bool isInvalidCardType = ValidateCardType(userDto.Cards); GenericValidator.TryValidate(userDto, out validationTestResults); var temp = validationTestResults; bool isInvalidCard = ValidateCards(userDto.Cards); if (temp.Count != 0 || userDto.Cards.Count == 0 || isInvalidCard || isInvalidCardType) { sb.AppendLine("Invalid Data"); continue; } var user = CreateUser(userDto, context); context.Users.Add(user); context.SaveChanges(); sb.AppendLine($"Imported {user.Username} with {user.Cards.Count} cards"); } return(sb.ToString().TrimEnd()); }
public static string ImportPurchases(VaporStoreDbContext context, string xmlString) { StringBuilder sb = new StringBuilder(); XmlSerializer serializer = new XmlSerializer(typeof(ImportPurchaseDto[]), new XmlRootAttribute("Purchases")); var purchaseDtos = (ImportPurchaseDto[])serializer.Deserialize(new StringReader(xmlString)); var validPurchases = new List <Game>(); foreach (var purchaseDto in purchaseDtos) { GenericValidator.TryValidate(purchaseDto, out validationTestResults); bool isValid = IsValidEnumPurchaseType(purchaseDto.Type); if (validationTestResults.Count != 0 || isValid == false) { sb.AppendLine("Invalid Data"); continue; } var purchase = CreatePurchase(purchaseDto, context); if (purchase.Game == null || purchase.Card == null || purchase.Type == 0) { sb.AppendLine("Invalid Data"); continue; } context.Purchases.Add(purchase); context.SaveChanges(); sb.AppendLine($"Imported {purchase.Game.Name} for {purchase.Card.User.Username}"); } return(sb.ToString().TrimEnd()); }
/// <inheritdoc/> public async Task CreateOrUpdateProfile(ProfileModel profileModel) { // validate the model var isValidModel = GenericValidator.TryValidate( profileModel, out var validationResults); if (!isValidModel) { throw new ValidationException(validationResults.First().ErrorMessage); } // get profiles in a case insensitive dictionary and add or replace this profile var profiles = new Dictionary <string, ProfileModel>( await GetProfilesAsync(), StringComparer.InvariantCultureIgnoreCase) { [profileModel.Name] = profileModel }; // save to file await SaveProfiles(profiles.Values.ToArray()); // update the cache _profiles.Set("profiles", profiles); }
public Product CreateProduct(string title, decimal price, Category category) { ICollection <ValidationResult> lstvalidationResult; if (category == null) { throw new CategoryNotFoundException($"{title} ürünü için kategori bilgisi bulunamadı."); } var product = new Product(title, price, category); bool valid = GenericValidator.TryValidate(product, out lstvalidationResult); if (!valid) { StringBuilder sb = new StringBuilder(); foreach (ValidationResult res in lstvalidationResult) { sb.AppendLine(res.MemberNames.First() + ":" + res.ErrorMessage); } throw new CustomValidationException(sb.ToString()); } category.AddProduct(product); return(product); }
private static bool ValidateCards(ICollection <CardDto> userDtoCards) { foreach (var cardDto in userDtoCards) { GenericValidator.TryValidate(cardDto, out validationTestResults); if (validationTestResults.Count == 0) { return(false); } } return(true); }
protected void Validate(object obj) { ICollection <ValidationResult> lstvalidationResult; bool valid = GenericValidator.TryValidate(obj, out lstvalidationResult); if (!valid) { foreach (ValidationResult res in lstvalidationResult) { throw new ArgumentException(res.MemberNames + ":" + res.ErrorMessage); } } }
public static void Main(string[] args) { string inputFilePath = Path.Combine(new DirectoryInfo(Directory.GetCurrentDirectory()).Parent.Parent.FullName, "DemoFile", "ExcelDemo.xlsx"); FileInfo intpuFileInfo = new FileInfo(inputFilePath); string outputFilePath = Path.Combine(new DirectoryInfo(Directory.GetCurrentDirectory()).Parent.Parent.FullName, "DemoFile", "output.txt"); using (StreamWriter outputFile = File.CreateText(outputFilePath)) { // Grab demo file using (var excel = new ExcelPackage(intpuFileInfo)) // If missing replace FileInfo constructor parameter with your own file { // Horizontal outputFile.WriteLine("Horizontal mapping"); // Get sheet with horizontal mapping var sheet = excel.Workbook.Worksheets.First(); // Get list of teams based on automatically mapping of the header row //outputFile.WriteLine("List of teams based on automatically mapping of the header row"); //var teams = sheet.GetRecords<Team>(); //foreach (var team in teams) //{ // outputFile.WriteLine($"{team.Name} - {team.FoundationYear} - {team.Titles}"); //} //// Get specific record from sheet based on automatically mapping the header row //outputFile.WriteLine("Team based on automatically mapping of the header row"); //var teamRec = sheet.GetRecord<Team>(2); //outputFile.WriteLine($"{teamRec.Name} - {teamRec.FoundationYear} - {teamRec.Titles}"); //// Remove HeaderRow //sheet.DeleteRow(1); // Get list of teams based on mapping using attributes outputFile.WriteLine("List of teams based on mapping using attributes"); var teamsAttr = sheet.GetRecords <TeamAttributes>(); int failcount = GenericValidator.TryValidate(teamsAttr); foreach (var team in teamsAttr) { bool isValid = team.IsValid; string msg = string.Format($"{team.Name},{team.Designation},{team.DOB},{team.Points}"); if (!isValid) { msg += ",FAILED," + team.ErrorMessage; failcount++; } else { msg += ",PASSED"; } outputFile.WriteLine(msg); } if (failcount > 0) { var resultSheet = excel.Workbook.Worksheets.AddOrReplace("Upload Result"); resultSheet.Cells["A1"].LoadFromCollection <TeamAttributes>(teamsAttr, true); excel.Save(); } // Get specific record from sheet based on mapping using attributes //outputFile.WriteLine("Team based on mapping using attributes"); //var teamAttr = sheet.GetRecord<TeamAttributes>(1); //outputFile.WriteLine($"{teamAttr.Name} - {teamAttr.FoundationYear} - {teamAttr.Titles}"); //// Get list of teams based on user created map //outputFile.WriteLine("List of teams based on user created map"); //var teamsMap = sheet.GetRecords(TeamMap.Create()); //foreach (var team in teamsMap) //{ // outputFile.WriteLine($"{team.Name} - {team.FoundationYear} - {team.Titles}"); //} //// Get specific record from sheet based on user created map //outputFile.WriteLine("Team based on user created map"); //var teamMap = sheet.GetRecord<Team>(1, TeamMap.Create()); //outputFile.WriteLine($"{teamMap.Name} - {teamMap.FoundationYear} - {teamMap.Titles}"); //// Vertical //outputFile.WriteLine("Vertical mapping"); //// Get sheet with vertical mapping //var vsheet = excel.Workbook.Worksheets.Skip(1).Take(1).First(); //// Get list of teams based on automatically mapping of the header row //outputFile.WriteLine("List of teams based on automatically mapping of the header row"); //var vteams = vsheet.GetRecords<VTeam>(); //foreach (var vteam in vteams) //{ // outputFile.WriteLine($"{vteam.Name} - {vteam.FoundationYear} - {vteam.Titles}"); //} //// Get specific record from sheet based on automatically mapping the header row //outputFile.WriteLine("Team based on automatically mapping of the header row"); //var vteamRec = vsheet.GetRecord<VTeam>(2); //outputFile.WriteLine($"{vteamRec.Name} - {vteamRec.FoundationYear} - {vteamRec.Titles}"); //// Remove HeaderRow //vsheet.DeleteColumn(1); //// Get list of teams based on mapping using attributes //outputFile.WriteLine("List of teams based on mapping using attributes"); //var vteamsAttr = vsheet.GetRecords<VTeamAttributes>(); //foreach (var vteam in vteamsAttr) //{ // outputFile.WriteLine($"{vteam.Name} - {vteam.FoundationYear} - {vteam.Titles}"); //} //// Get specific record from sheet based on mapping using attributes //outputFile.WriteLine("Team based on mapping using attributes"); //var vteamAttr = vsheet.GetRecord<VTeamAttributes>(1); //outputFile.WriteLine($"{vteamAttr.Name} - {vteamAttr.FoundationYear} - {vteamAttr.Titles}"); //// Get list of teams based on user created map //outputFile.WriteLine("List of teams based on user created map"); //var vteamsMap = vsheet.GetRecords(VTeamMap.Create()); //foreach (var vteam in vteamsMap) //{ // outputFile.WriteLine($"{vteam.Name} - {vteam.FoundationYear} - {vteam.Titles}"); //} //// Get specific record from sheet based on user created map //outputFile.WriteLine("Team based on user created map"); //var vteamMap = vsheet.GetRecord<VTeam>(1, VTeamMap.Create()); //outputFile.WriteLine($"{vteamMap.Name} - {vteamMap.FoundationYear} - {vteamMap.Titles}"); } } }