public async Task <int> UpdateBook(string bookId, Book book) { int DbResult = 0; if (!String.IsNullOrEmpty(bookId) && dataReqiered.IsDataNoEmpty(book)) { List <BookMongoDb> oldBookData = await db.Books.Find(new BsonDocument("_id", new ObjectId(bookId))).ToListAsync(); if (oldBookData != null) { BookMongoDb newBookData = new BookMongoDb { Id = book.Id, Year = book.Year, Name = book.Name, Description = book.Description, AuthorId = book.AuthorId }; try { await db.Books.ReplaceOneAsync(new BsonDocument("_id", new ObjectId(bookId)), newBookData); DbResult = 1; } catch { return(DbResult); } } } return(DbResult); }
public async Task <int> CreateBook(Book book) { int DbResult = 0; if (dataReqiered.IsDataNoEmpty(book)) { bool isNewBook = await CheckingBookOnDuplicate(book); if (isNewBook) { BookMongoDb newBook = new BookMongoDb { Id = book.Id, Year = book.Year, Name = book.Name, Description = book.Description, AuthorId = book.AuthorId }; try { await db.Books.InsertOneAsync(newBook); DbResult = 1; } catch { return(DbResult); } } } return(DbResult); }
private async Task LoadDataFromColumnAsync(ISheet sheet) { for (int row = 1; row <= sheet.LastRowNum; row++) { BookMongoDb bookMongoDb = new BookMongoDb(); if (sheet.GetRow(row).GetCell(0).CellType == CellType.Numeric) { bookMongoDb.Year = Convert.ToInt32(sheet.GetRow(row).GetCell(0).NumericCellValue); } if (sheet.GetRow(row).GetCell(1).CellType == CellType.String) { bookMongoDb.Name = sheet.GetRow(row).GetCell(1).StringCellValue; } if (sheet.GetRow(row).GetCell(2).CellType == CellType.String) { bookMongoDb.Description = sheet.GetRow(row).GetCell(2).StringCellValue; } if (sheet.GetRow(row).GetCell(3).CellType == CellType.Numeric) { bookMongoDb.AuthorId = Convert.ToString(sheet.GetRow(row).GetCell(3).NumericCellValue); } else if (sheet.GetRow(row).GetCell(3).CellType == CellType.String) { string authorName = sheet.GetRow(row).GetCell(3).StringCellValue; string[] words = authorName.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string firstName = words[0]; string surName = words[1]; string authorId = await authorRepo.GetAuthorIdByName(firstName, surName); if (!String.IsNullOrEmpty(authorId)) { bookMongoDb.AuthorId = authorId; } else { Author author = new Author { Name = firstName, Surname = surName }; int dbResult = await authorRepo.CreateAuthor(author); if (dbResult > 0) { bookMongoDb.AuthorId = await authorRepo.GetAuthorIdByName(author.Name, author.Surname); } } } Book book = new Book(bookMongoDb); bookList.Add(book); book = null; } }
public Book(BookMongoDb book) { Id = book.Id; Year = book.Year; Name = book.Name; Description = book.Description; AuthorId = book.AuthorId; BookMongoDb = book; }
public BooksController(BookMongoDb category) { _book = category; }