public async Task <IActionResult> PutBook([FromForm] UpdateBookPostModel bookVM) { Book book = await _context.Book.FindAsync(bookVM.Id); if (book is null) { return(Ok(new { error_message = "Khong tim thay sach" })); } if (await _bookServices.UpdateBookAsync(book, bookVM)) { return(Ok(new { success = true, message = "Cap nhat thanh cong" })); } else { return(Ok(new { error_message = "Cap nhat that bai, co loi xay ra" })); } }
internal async Task <bool> UpdateBookAsync(Book book, UpdateBookPostModel bookVM) { if (bookVM.AuthorId is not null) { book.AuthorId = bookVM.AuthorId ?? 0; } if (bookVM.BookName is not null) { book.BookName = bookVM.BookName; } if (bookVM.CategoryId is not null) { book.CategoryId = bookVM.CategoryId ?? 0; } if (bookVM.Description is not null) { book.Description = bookVM.Description; } if (bookVM.Price is not null) { book.Price = bookVM.Price ?? 0; } if (bookVM.Private is not null) { book.Private = bookVM.Private; } //Reup image var existingBookImage = await _bookstoreContext.BookImage .FirstOrDefaultAsync(images => images.BookId == book.Id); if (existingBookImage is null) { BookImage bookImage = new BookImage(); bookImage.BookId = book.Id; if (bookVM.MainImage is not null) { book.MainImage = DateTimeOffset.Now.ToUnixTimeSeconds().ToString() + '_' + bookVM.MainImage.FileName; _imageServices.UploadImage(bookVM.MainImage, book.MainImage); } if (bookVM.Image1 is not null) { bookImage.Image1 = DateTimeOffset.Now.ToUnixTimeSeconds().ToString() + '_' + bookVM.Image1.FileName; _imageServices.UploadImage(bookVM.Image1, bookImage.Image1); } if (bookVM.Image2 is not null) { bookImage.Image2 = DateTimeOffset.Now.ToUnixTimeSeconds().ToString() + '_' + bookVM.Image2.FileName; _imageServices.UploadImage(bookVM.Image2, bookImage.Image2); } if (bookVM.Image3 is not null) { bookImage.Image3 = DateTimeOffset.Now.ToUnixTimeSeconds().ToString() + '_' + bookVM.Image3.FileName; _imageServices.UploadImage(bookVM.Image3, bookImage.Image3); } if (bookVM.Image4 is not null) { bookImage.Image4 = DateTimeOffset.Now.ToUnixTimeSeconds().ToString() + '_' + bookVM.Image4.FileName; _imageServices.UploadImage(bookVM.Image4, bookImage.Image4); } await _bookstoreContext.BookImage .AddAsync(bookImage); } else { if (bookVM.MainImage is not null) { book.MainImage = DateTimeOffset.Now.ToUnixTimeSeconds().ToString() + '_' + bookVM.MainImage.FileName; _imageServices.UploadImage(bookVM.MainImage, book.MainImage); } if (bookVM.Image1 is not null) { existingBookImage.Image1 = DateTimeOffset.Now.ToUnixTimeSeconds().ToString() + '_' + bookVM.Image1.FileName; _imageServices.UploadImage(bookVM.Image1, existingBookImage.Image1); } if (bookVM.Image2 is not null) { existingBookImage.Image2 = DateTimeOffset.Now.ToUnixTimeSeconds().ToString() + '_' + bookVM.Image2.FileName; _imageServices.UploadImage(bookVM.Image2, existingBookImage.Image2); } if (bookVM.Image3 is not null) { existingBookImage.Image3 = DateTimeOffset.Now.ToUnixTimeSeconds().ToString() + '_' + bookVM.Image3.FileName; _imageServices.UploadImage(bookVM.Image3, existingBookImage.Image3); } if (bookVM.Image4 is not null) { existingBookImage.Image4 = DateTimeOffset.Now.ToUnixTimeSeconds().ToString() + '_' + bookVM.Image4.FileName; _imageServices.UploadImage(bookVM.Image4, existingBookImage.Image4); } _bookstoreContext.Entry(existingBookImage).State = EntityState.Modified; } _bookstoreContext.Entry(book).State = EntityState.Modified; try { return(await _bookstoreContext.SaveChangesAsync() != 0); } catch (DbUpdateConcurrencyException) { return(false); }; }