public IActionResult AddAuthor([FromBody] AuthorCreationDTO authorDto) { if (authorDto == null) { return(BadRequest("The author cannot be null")); } Author authorModel = this.mapper.Map <Author>(authorDto); authorModel.Id = Guid.NewGuid(); // Check if the authors already exists IEnumerable <Author> lstAuthors = this.repository .GetWhere((item) => item.FirstName.ToLower().Trim().Equals(authorModel.FirstName.ToLower().Trim()) && item.LastName.ToLower().Trim().Equals(authorModel.LastName.ToLower().Trim())); if (lstAuthors.Any()) { ErrorMessage error = new ErrorMessage((int)HttpStatusCode.NotFound, $"The author, {authorModel.FirstName} {authorModel.LastName}, already exists."); return(BadRequest(error)); } this.repository.Add(authorModel); this.repository.Save(); return(CreatedAtRoute("GetAuthorsById", new { id = authorModel.Id }, null)); }
public async Task <ActionResult> Put(int id, [FromBody] AuthorCreationDTO authorUpdating) { var author = mapper.Map <Author>(authorUpdating); author.Id = id; context.Entry(author).State = EntityState.Modified; await context.SaveChangesAsync(); return(NoContent()); }
public ActionResult <AuthorReadDTO> PostAuthor([FromBody] AuthorCreationDTO author) { var newAuthor = mapper.Map <AuthorReadDTO>(repository.Create(mapper.Map <Author>(author))); return(CreatedAtRoute( routeName: "GetAuthor", routeValues: new { id = newAuthor.AuthorId }, value: newAuthor )); }
public async Task <ActionResult> Post([FromBody] AuthorCreationDTO authorcreationDTO) { var author = mapper.Map <Author>(authorcreationDTO); context.Add(author); await context.SaveChangesAsync(); var authorDTO = mapper.Map <AuthorDTO>(author); return(new CreatedAtRouteResult("GetAuthor", new { id = author.Id }, authorDTO)); }
public async Task <ActionResult> CreateAuthor([FromBody] AuthorCreationDTO authorCreation) { if (authorCreation == null) { return(BadRequest()); } var author = _mapper.Map <Author>(authorCreation); _context.Add(author); await _context.SaveChangesAsync(); var authorDTO = _mapper.Map <AuthorDTO>(author); return(new CreatedAtRouteResult("GetAuthor", new { id = author.Id }, authorDTO)); }