public async Task <int> UpdateBook(string id, Book book) { int DbResult = 0; int oldDataBookId, newDataBookId = 0; if (!String.IsNullOrEmpty(id) && dataReqiered.IsDataNoEmpty(book)) { oldDataBookId = Convert.ToInt32(id); newDataBookId = Convert.ToInt32(book.Id); BookMsSql updatingBook = null; updatingBook = await db.Books.FindAsync(oldDataBookId); if (oldDataBookId == newDataBookId) { updatingBook.Year = book.Year; updatingBook.Name = book.Name; updatingBook.Description = book.Description; 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; BookMsSql 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); }
public async Task <int> CreateBook(Book book) { int DbResult = 0; int authorId = 0; if (dataReqiered.IsDataNoEmpty(book)) { authorId = Convert.ToInt32(book.AuthorId); BookMsSql newBook = new BookMsSql { 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); }