Creating the API endpoint

We can now add an additional API endpoint to the Features/Products/Controller.cs file. The controller action definition looks like this:

[HttpPost, Authorize(Roles = "Admin")]public async Task<IActionResult> Create([FromBody] CreateProductViewModel model){  ...}

This endpoint will only accept HTTP POST requests from authenticated users who belong to the Admin role. Remembering that our custom typeahead control allows users to enter any arbitrary string value, we first need to check whether what they entered for the brand and operating system already exist in the database or not. We do this as follows:

var brand = await _db.Brands.FirstOrDefaultAsync(x => x.Name == model.Brand);if (brand == null)  brand = new Brand ...

Get ASP.NET Core 2 and Vue.js now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.