private Book MapToModelBook(CustomerCreateOrUpdateBookRequest request) { var book = new Book(); book.Id = request.BookData.Id; book.Name = request.BookData.Name; book.Price = request.BookData.Price; book.Category = request.BookData.Category; book.Author = request.BookData.Author; return(book); }
CustomerCreateOrUpdateBookRequest MapToCustomerRequest(Book newBook) { var request = new CustomerCreateOrUpdateBookRequest(); var bookData = new CustomerBookDataReponse(); bookData.Id = newBook.Id; bookData.Name = newBook.Name; bookData.Price = newBook.Price; bookData.Category = newBook.Category; bookData.Author = newBook.Author; request.BookData = bookData; return(request); }
public override async Task <CustomerBoolBookResponse> UpdateBook(CustomerCreateOrUpdateBookRequest request, ServerCallContext context) { logger.LogInformation("Begin grpc call from method {Method} for book id {Id}", context.Method, request.BookData.Id); //using (var dbcontext = new dbBooksContext(dbContextOptions)) //{ var currentBook = await bookContext.Books.FindAsync(request.BookData.Id); if (currentBook == null) { context.Status = new Status(StatusCode.NotFound, $"Book with id {request.BookData.Id} do not exist"); return(new CustomerBoolBookResponse { Error = false }); } currentBook.Name = request.BookData.Name; currentBook.Category = request.BookData.Category; currentBook.Author = request.BookData.Author; currentBook.Price = request.BookData.Price; bookContext.Books.Update(currentBook); try { await bookContext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) when(!BookExists(bookContext, currentBook.Id)) { context.Status = new Status(StatusCode.NotFound, $"Book with id {request.BookData.Id} do not update"); return(new CustomerBoolBookResponse { Error = false }); } context.Status = new Status(StatusCode.OK, $"Book with id {request.BookData.Id} do update"); return(new CustomerBoolBookResponse { Error = true }); //} }
public override async Task <CustomerBoolBookResponse> CreateBook(CustomerCreateOrUpdateBookRequest request, ServerCallContext context) { logger.LogInformation("Begin grpc call from method {Method} for book Id {Id}", context.Method, request.BookData.Id); //using (var dbcontext = new dbBooksContext(dbContextOptions)) //{ var existeBook = await bookContext.Books.FindAsync(request.BookData.Id); if (existeBook != null) { context.Status = new Status(StatusCode.NotFound, $"Book with id {request.BookData.Id} do not exist"); return(new CustomerBoolBookResponse { Error = false }); } var book = MapToModelBook(request); await bookContext.AddAsync(book); try { await bookContext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) when(!BookExists(bookContext, book.Id)) { context.Status = new Status(StatusCode.NotFound, $"Book with Name {request.BookData.Id} do not create"); return(new CustomerBoolBookResponse { Error = false }); } context.Status = new Status(StatusCode.OK, $"Book with Name {request.BookData.Id} do create"); return(new CustomerBoolBookResponse { Error = true }); //} }