public int Create(CreateBookDTO newBook) { var x = AutoMapper.Mapper.Map <Book>(newBook); RepoWrap.BookRepo.Create(x); return(RepoWrap.Save()); }
public async Task <ActionResult <BookDTO> > PostBook(CreateBookDTO modelBook) { if (!ModelState.IsValid) { return(BadRequest()); } var book = _mapper.Map <Book>(modelBook); _repository.Create(book); await _repository.SaveAsync(); var bookDTO = _mapper.Map <BookDTO>(book); return(CreatedAtAction("GetBook", new { id = book.ID }, bookDTO)); }
public ActionResult Create(CreateBookDTO data) { if (ModelState.IsValid) { Book book = new Book(); book.Title = data.Title; book.Content = data.Content; book.Genre = data.Genre; book.ImageUrl = data.ImageUrl; _dbContext.Books.Add(book); _dbContext.SaveChanges(); return View(); } return View(data); }
public async Task <IActionResult> Edit([FromBody] CreateBookDTO newBook) { try { if (ModelState.IsValid) { return(await Task.FromResult(Ok(BookService.Update(newBook)))); } else { Logger.LogError("Model Not Valid!"); return(BadRequest("Mode Not Valid")); } } catch (Exception ex) { Logger.LogError(ex.InnerException.ToString()); return(BadRequest("Failed To insert" + ex)); } }
public IActionResult CreateBookForAuthor(Guid authorId, [FromBody] CreateBookDTO createBookDTO) { if (createBookDTO == null) { return(BadRequest()); } if (createBookDTO.Title == createBookDTO.Description) { ModelState.AddModelError(nameof(CreateBookDTO), "The provided description should be different from the title."); } if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } if (!_libraryRepository.AuthorExists(authorId)) { return(NotFound()); } var newBook = Mapper.Map <Book>(createBookDTO); _libraryRepository.AddBookForAuthor(authorId, newBook); if (!_libraryRepository.Save()) { throw new Exception($"Creating a book for author {authorId} failed on save."); } var bookDTO = Mapper.Map <BookDTO>(newBook); return(CreatedAtRoute("GetBookForAuthor", new { authorId, id = bookDTO.Id }, bookDTO)); }
public async Task <IActionResult> Create([FromBody] CreateBookDTO createBookDTO) { try { if (createBookDTO == null) { _logger.LogWarn($"{ GetCallLocation()}:Bed request from Create0"); return(BadRequest(ModelState)); } if (!ModelState.IsValid) { _logger.LogWarn($"{ GetCallLocation()}:Bed request from Create1"); return(BadRequest(ModelState)); } var record = _mapper.Map <Book>(createBookDTO); var isSuccess = await _bookRepository.Create(record); if (!isSuccess) { _logger.LogWarn($"{ GetCallLocation()}:Create in db goes wrong"); return(StatusCode(500, "Internal Error")); } if (!string.IsNullOrEmpty(createBookDTO.File)) { var imgPath = $"{_env.ContentRootPath}\\uploads\\{createBookDTO.Image}"; byte[] imageBytes = Convert.FromBase64String(createBookDTO.File); System.IO.File.WriteAllBytes(imgPath, imageBytes); } return(Created("Created", new { record })); } catch (Exception e) { _logger.LogInfo($"{ GetCallLocation()}:{e.Message} - {e.InnerException}"); return(StatusCode(500, "Internal Error")); } }
public int Update(CreateBookDTO newBook) { RepoWrap.BookRepo.Update(Mapper.Map <CreateBookDTO, Book>(newBook)); return(RepoWrap.Save()); }