public async Task <ImportResult <Category> > Import(string csvContent) { var result = new ImportResult <Category> { ImportedEntities = new List <Category>(), ErrorMessages = new List <string>() }; int counter = 0; var records = _recordExtractor.GetRecords <CategoryRecord, CategoryRecordMap>(csvContent); foreach (var record in records) { counter++; if (string.IsNullOrWhiteSpace(record.AreaName)) { AddError(result, $"[{counter}]: Area name is null or empty."); continue; } if (string.IsNullOrWhiteSpace(record.Name)) { AddError(result, $"[{counter}]: Category name is null or empty."); continue; } var categoryArea = await _categoryAreaService.ReadByNameAsync(record.AreaName); if (categoryArea == null) { AddError(result, $"[{counter}]: Area with name: {record.AreaName} does not exist."); continue; } var category = await _categoryService.ReadByNameAsync(record.AreaName, record.Name); if (category != null) { AddError(result, $"[{counter}]: Category with name: {record.Name} already exists in Area: {record.AreaName}."); continue; } category = new Category { Name = record.Name, Description = record.Description, CategoryArea = categoryArea }; await _categoryService.CreateAsync(category); result.ImportedEntities.Add(category); } return(result); }
public async Task <ImportResult <Book> > Import(string csvContent) { var result = new ImportResult <Book> { ImportedEntities = new List <Book>(), ErrorMessages = new List <string>() }; var counter = 0; var records = _recordExtractor.GetRecords <BookRecord, BookRecordMap>(csvContent); foreach (var record in records) { counter++; if (RecordInvalid(counter, result, record)) { continue; } var categoryArea = await _categoryAreaService.ReadByNameAsync(record.AreaName); if (categoryArea == null) { AddError(result, $"[{counter}]: Area with name: {record.AreaName} does not exist."); continue; } var category = await _categoryService.ReadByNameAsync(record.AreaName, record.CategoryName); if (category == null) { AddError(result, $"[{counter}]: Category with name: {record.CategoryName} does not exist in Area: {record.AreaName}."); continue; } var book = await _bookService.ReadByNameAsync(record.Title, record.PublishYear, record.Authors); if (book != null) { AddError(result, $"[{counter}]: Book with title:'{record.Title}', publishYear:'{record.PublishYear}', authors:'{record.Authors}' already exists."); continue; } book = new Book { Name = record.Title, PublishYear = record.PublishYear, Authors = record.Authors, Rating = record.Rating, ImageUri = record.ImageUri, AmazonUri = record.AmazonUri, ContentUri = record.ContentUri, ContentType = record.ContentType, Reflection = record.Reflection, Category = category }; await _bookService.CreateAsync(book); result.ImportedEntities.Add(book); } return(result); }