public async Task <int> CreateBook(Book book) { int DbResult = 0; int authorId = 0; if (dataReqiered.IsDataNoEmpty(book)) { bool isNewBook = await CheckingBookOnDuplicate(book); if (isNewBook) { authorId = Convert.ToInt32(book.AuthorId); BookPostgreSql newBook = new BookPostgreSql { Name = book.Name, Description = book.Description, Year = book.Year, AuthorId = authorId }; db.Books.Add(newBook); try { DbResult = await db.SaveChangesAsync(); } catch { return(DbResult); } } } return(DbResult); }
public Book(BookPostgreSql book) { Id = Convert.ToString(book.Id); Name = book.Name; Year = book.Year; Description = book.Description; AuthorId = Convert.ToString(book.AuthorId); BookPostgreSql = book; }
public async Task <int> UpdateBook(string id, Book book) { int DbResult = 0; int oldDataBookId, newDataBookId, authorId = 0; if (!String.IsNullOrEmpty(id) && dataReqiered.IsDataNoEmpty(book)) { try { oldDataBookId = Convert.ToInt32(id); newDataBookId = Convert.ToInt32(book.Id); authorId = Convert.ToInt32(book.AuthorId); } catch { return(DbResult); } BookPostgreSql updatingBook = null; updatingBook = await db.Books.FindAsync(oldDataBookId); if (oldDataBookId == newDataBookId) { updatingBook.Year = book.Year; updatingBook.Name = book.Name; updatingBook.Description = book.Description; updatingBook.AuthorId = authorId; db.Entry(updatingBook).State = EntityState.Modified; try { DbResult = await db.SaveChangesAsync(); } catch { return(DbResult); } } } return(DbResult); }
public async Task <int> DeleteBook(string id) { int DbResult = 0; BookPostgreSql book = null; if (!String.IsNullOrEmpty(id)) { int delBookId = Convert.ToInt32(id); book = db.Books.Find(delBookId); if (book != null) { db.Books.Remove(book); DbResult = await db.SaveChangesAsync(); } } return(DbResult); }
private async Task LoadDataFromColumnAsync(ISheet sheet) { for (int row = 1; row <= sheet.LastRowNum; row++) { BookPostgreSql bookPostgreSql = new BookPostgreSql(); if (sheet.GetRow(row).GetCell(0).CellType == CellType.Numeric) { bookPostgreSql.Year = Convert.ToInt32(sheet.GetRow(row).GetCell(0).NumericCellValue); } if (sheet.GetRow(row).GetCell(1).CellType == CellType.String) { bookPostgreSql.Name = sheet.GetRow(row).GetCell(1).StringCellValue; } if (sheet.GetRow(row).GetCell(2).CellType == CellType.String) { bookPostgreSql.Description = sheet.GetRow(row).GetCell(2).StringCellValue; } if (sheet.GetRow(row).GetCell(3).CellType == CellType.Numeric) { bookPostgreSql.AuthorId = Convert.ToInt32(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)) { bookPostgreSql.AuthorId = Convert.ToInt32(authorId); } else { Author author = new Author { Name = firstName, Surname = surName }; int dbResult = await authorRepo.CreateAuthor(author); if (dbResult > 0) { bookPostgreSql.AuthorId = Convert.ToInt32(await authorRepo.GetAuthorIdByName(author.Name, author.Surname)); } author = null; } } Book book = new Book(bookPostgreSql); bookList.Add(book); book = null; } }