public async Task <IActionResult> Create([FromBody] CreateAuthorDTO authorDTO) { try { if (authorDTO == null) { return(BadRequest(ModelState)); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var author = _mapper.Map <Author>(authorDTO); var isSuccess = await _authorRepository.Create(author); if (!isSuccess) { return(InternalError($"Author creation failed")); } ; return(Created("Create", new { author })); } catch (Exception e) { return(InternalError($"{e.Message} - {e.InnerException}")); } }
public async Task <IActionResult> Create([FromBody] CreateAuthorDTO createAuthorDTO) { try { if (createAuthorDTO == null) { _logger.LogWarn("Bed request from Create0"); return(BadRequest(ModelState)); } if (!ModelState.IsValid) { _logger.LogWarn("Bed request from Create1"); return(BadRequest(ModelState)); } var record = _mapper.Map <Author>(createAuthorDTO); var isSuccess = await _authorRepository.Create(record); if (!isSuccess) { _logger.LogWarn("Create in db goes wrong"); return(StatusCode(500, "Internal Error")); } return(Created("Created", new { record })); } catch (Exception e) { _logger.LogInfo($"{e.Message} - {e.InnerException}"); return(StatusCode(500, "Internal Error")); } }
public ActionResult Create([Bind(Include = "id,name,dob,qualification,profession")] CreateAuthorDTO author) { if (ModelState.IsValid) { _authorService.NewAuthor(author); return(RedirectToAction("Index")); } return(View(author)); }
public IActionResult CreateAuthor(CreateAuthorDTO authorRequest) { var author = _mapper.Map <Author>(authorRequest); _courseLibrary.AddAuthor(author); _courseLibrary.Save(); var authorToReturn = _mapper.Map <AuthorDTO>(author); return(CreatedAtRoute("GetAuthor", new { authorId = author.Id }, authorToReturn)); }
public void NewAuthor(CreateAuthorDTO authorDTO) { Author author = new Author() { name = authorDTO.name, dob = authorDTO.dob, qualification = authorDTO.qualification, profession = authorDTO.profession }; _Repository.Insert(author); }
public async Task <ActionResult> PostAuthor([FromBody] CreateAuthorDTO createAuthor) { var author = _mapper.Map <Author>(createAuthor); _context.Add(author); await _context.SaveChangesAsync(); var authorDTO = _mapper.Map <AuthorDTO>(author); return(CreatedAtAction("GetAuthor", new { id = authorDTO.Id }, authorDTO)); }
public async Task <ActionResult <CreateAuthorDTO> > PostAuthor([FromBody] CreateAuthorDTO createAuthor) { if (!ModelState.IsValid) { return(BadRequest("Invalid")); } var author = _mapper.Map <Author>(createAuthor); _authorRepository.Create(author); await _authorRepository.SaveAsync(); return(CreatedAtAction("GetAuthor", new { id = author.ID }, createAuthor)); }
public async Task <IActionResult> PutAuthor(int Id, [FromBody] CreateAuthorDTO updateAuthor) { var authorDB = await _context.Authors.FirstOrDefaultAsync(x => x.Id == Id); if (authorDB == null) { return(NotFound()); } authorDB = _mapper.Map(updateAuthor, authorDB); await _context.SaveChangesAsync(); return(NoContent()); }
public IActionResult CreateAuthor([FromBody] CreateAuthorDTO createAuthorDTO) { if (createAuthorDTO == null) { return(BadRequest()); } var newAuthor = Mapper.Map <Author>(createAuthorDTO); _libraryRepository.AddAuthor(newAuthor); if (!_libraryRepository.Save()) { throw new Exception("Creating an author failed on save."); } var authorDTO = Mapper.Map <AuthorDTO>(newAuthor); return(CreatedAtRoute("GetAuthor", new { id = authorDTO.Id }, authorDTO)); }
//we need to create a separate model for this endpoint, as it doesnt need an //id field in its input. So if there are other fields that wont be sent then // create a model that has only the properties that will be sent to this endpoint public async Task <IActionResult> CreateAuthor([FromBody] CreateAuthorDTO model) { //convert to entity var newAuthor = new Entities.Author { DateOfBirth = model.DOB, FirstName = model.FirstName, LastName = model.LastName, MainCategory = model.MainCategory }; // also contains method to add collection of courses for the author await _courseLibraryRepository.AddAuthorAsync(newAuthor); _courseLibraryRepository.Save(); // Map to return type DTO var authorDto = new AuthorDto { Id = newAuthor.Id, Name = $"{newAuthor.FirstName} {newAuthor.LastName}", Age = DateTime.Now.Year - newAuthor.DateOfBirth.Year, MainCategory = newAuthor.MainCategory }; // route name is the name of the route that will return details for the //newly created author () //routeValues: we need to create an object with the same arg names as the // route args for GetAuthor // value: will be the newly created author object // this will return the author.. to get its corresponding courses // append /Courses to the route return(CreatedAtRoute(routeName: "GetAuthor", routeValues: new { authorId = authorDto.Id }, value: authorDto)); }
public CreateAuthor(ILogger <CreateAuthor> logger, IAuthorFacade authorFacade) { AuthorDto = new CreateAuthorDTO(Guid.NewGuid()); this.logger = logger; this.authorFacade = authorFacade; }