public IActionResult GetBooks() { List <Book> books = repository.Get().ToList(); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } return(Ok(books)); }
public IActionResult Create([FromForm] Author authorToCreate) { var nvc = Request.Form; string firstName = nvc["FirstName"]; string lastName = nvc["LastName"]; authorToCreate.FirstName = firstName; authorToCreate.LastName = lastName; Console.WriteLine(authorToCreate); //authorToCreate.FirstName = FirstName; //authorToCreate.LastName = LastName; if (authorToCreate == null) { Console.WriteLine(authorToCreate); return(BadRequest(ModelState)); } Author author = repository.Get() .Where(c => (c.FirstName + c.LastName).Trim().ToUpper() == (authorToCreate.FirstName + authorToCreate.LastName).Trim().ToUpper()) .FirstOrDefault(); if (author != null) { ModelState .AddModelError("", $"Author {author.FirstName + " " + author.LastName} already exists."); return(StatusCode(422, ModelState)); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!repository.Create(authorToCreate)) { ModelState .AddModelError("", $"Something went wrong while saving {authorToCreate.FirstName + " " + authorToCreate.LastName}."); return(StatusCode(500, ModelState)); } Response.ContentType = "application/json"; return(CreatedAtRoute("GetAuthor", new { authorId = authorToCreate.Id }, authorToCreate )); }