public async Task <IActionResult> CreatAsync(CreateNewBookRequest request) { try { var result = await _bookService.CreateNewAsync(request); return(Ok(result)); } catch (Exception ex) { return(BadRequest(ex)); } }
public async Task <CreateNewBookResponse> CreateNewAsync(CreateNewBookRequest request) { try { if (request == null) { throw new Exception("Invalid Argument"); } var existCategory = await _unitOfWork.CategoryRepository.FirstOfDefaultAsync(s => s.Id == request.CategoryId); if (existCategory == null) { throw new Exception("BookCategory does not exist"); } var newRequest = new Book { Name = request.BookName, BookCategory = existCategory }; await _unitOfWork.BookRepository.Add(newRequest); if (await _unitOfWork.CommitAsync()) { return(new CreateNewBookResponse { BookId = newRequest.Id, BookName = newRequest.Name, CategoryId = newRequest.CategoryId, CategoryName = newRequest.BookCategory.Name }); } throw new Exception("Data invalid"); } catch (Exception ex) { throw; } }
public async Task <ActionResult <UploadState> > CreateUploadAsync(CreateNewBookRequest request) { try { // creating an entirely new book if (request.Book != null) { return(_uploads.CreateTask(new BookUpload { Book = request.Book, Content = request.Content })); } // adding contents to an existing book if (request.BookId != null) { var book = await _books.GetAsync(request.BookId); if (book == null) { return(ResultUtilities.NotFound <Book>(request.BookId)); } return(_uploads.CreateTask(new BookUpload { BookId = book.Id, Content = request.Content })); } } catch (InvalidOperationException e) { return(BadRequest(e.Message)); } throw new ArgumentException("Invalid upload state."); }