public async Task <ActionResult> Create(CreateOrUpdateAuthorRequest request) { try { await _manager.AddAuthor(request); return(RedirectToAction(nameof(ShowAuthors))); } catch (ArgumentNullException) { return(RedirectToAction("ErrorPage", nameof(Main), new { message = "Error: can not add new author", call = nameof(Author) })); } catch (DbUpdateException) { return(RedirectToAction("ErrorPage", nameof(Main), new { message = "Error: invalid input", call = nameof(Author) })); } }
[Authorize(Roles = "Admin")] //<--- Authorization for only Admins public IActionResult AddAuthor([FromBody] Author author) { if (ModelState.IsValid) { authorManager.AddAuthor(author); //return Created($"/api/authors/{author.Id}",author); return(CreatedAtAction(nameof(GetAuthorById), new { Id = author.Id }, author)); } else { return(BadRequest(ModelState)); } }
public ActionResult Create(Author author) //model binding { if (ModelState.IsValid) { authorManager.AddAuthor(author); //return View("Details", author); return(RedirectToAction("List")); } else { //send user back to the same page Response.StatusCode = 400; return(View()); } }
public async Task <IActionResult> AddAuthor([FromBody] AuthorRequest authorRequest) { if (!await IsUserAdmin()) { return(Forbid()); } var author = _mapper.Map <Author>(authorRequest); _authorManager.AddAuthor(author); await _authorManager.SaveChangesAsync(); var response = _mapper.Map <AuthorResponse>(author); return(CreatedAtAction(nameof(GetAuthorById), new { id = author.Id }, response)); }