From the course: .NET Microservices for Azure Developers

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Creating a new pet

Creating a new pet

- [Instructor] The PetsController class is looking good, but it's missing the pet creation logic, so let's fix that. Let's add a new method that returns an IActionResult, and let's name it Create. And of course, we need to use the HttpPost attribute. Here, I can use a Pet object. However, I think it's better to have another model for pet creation, so let's create a new record named NewPet that has the Name, the Age, and the BreedId. So I can use newPet as a payload, and here, dbContext.Pets.AddAsync, and we need a way to transform the newPet object into a Pet object. So I'm going to implement that here, public Pet ToPet, so this is going to return a new Pet with the Age, the BreedId, and, of course, the Name, just like this. Let's close the braces, and now I'm ready to create a Pet variable and newPet ToPet, and I can use pet here. Then we need to save the changes, so await dbContext.SaveChangesAsync. And finally, we need to return the result. I can return Ok or I can use other…

Contents