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 )); }
public IActionResult Create([FromBody] Category categoryToCreate) { if (categoryToCreate == null) { return(BadRequest(ModelState)); } Category book = repository.Get() .Where(c => (c.Name).Trim().ToUpper() == (categoryToCreate.Name).Trim().ToUpper()) .FirstOrDefault(); if (book != null) { ModelState .AddModelError("", $"Category {book.Name} already exists."); return(StatusCode(422, ModelState)); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!repository.Create(categoryToCreate)) { ModelState .AddModelError("", $"Something went wrong while saving {book.Name}."); return(StatusCode(500, ModelState)); } Response.ContentType = "application/json"; return(CreatedAtRoute("GetCategory", new { categoryId = categoryToCreate.Id }, categoryToCreate )); }
public IActionResult Create([FromBody] Book bookToCreate) { if (bookToCreate == null) { return(BadRequest(ModelState)); } Book book = repository.Get() .Where(c => (c.Title).Trim().ToUpper() == (bookToCreate.Title).Trim().ToUpper()) .FirstOrDefault(); if (book != null) { ModelState .AddModelError("", $"Book {book.Title} already exists."); return(StatusCode(422, ModelState)); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!repository.Create(bookToCreate)) { ModelState .AddModelError("", $"Something went wrong while saving {book.Title}."); return(StatusCode(500, ModelState)); } return(CreatedAtRoute("GetBook", new { authorId = bookToCreate.Id }, bookToCreate )); }