public async Task <Guid> CreateBookAsync(Book book, Stream image = null, string imageContentType = null) { if (book == null) { throw new ArgumentNullException(nameof(book)); } _logger.LogInformation($"Create book {book.Id} ({book.Title})"); if (book.Id == default(Guid)) { book.Id = Guid.NewGuid(); } var containsImage = _imagesService.SaveImage(book, image, imageContentType); try { return(await _repository.CreateBookAsync(book)); } catch (Exception ex) { _logger.LogError(ex, ex.Message); if (containsImage) { _imagesService.DeleteImage(book); } throw; } }
public static void LoadDataFromJson(IBooksRepository repository) { var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SampleData", "books.json"); var json = File.ReadAllText(path); var books = JsonConvert.DeserializeObject <Book[]>(json); foreach (var item in books) { repository.CreateBookAsync(item); } }