public async Task <IActionResult> AddMenuCategory([FromBody] Models.MenuCategory menuCategory) { if (menuCategory == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } // Check if another menu category exists with specified name. if (await _menuService.MenuCategoryExistsAsync(menuCategory.MenuId, menuCategory.Name)) { return(Conflict(new { Error = $"A category with the name '{menuCategory.Name}' already exists." })); } // Map category model to entity. var menuCatEnt = _menuMapper.MenuCategoryModelToEntity(menuCategory); _menuService.AddMenuCategory(menuCatEnt); if (!await _menuService.SaveChangesAsync()) { throw new Exception($"Could not save category '{menuCategory.Name}'."); } // Map newly saved restaurant back to model. menuCategory = _menuMapper.MenuCategoryEntityToModel(menuCatEnt); return(CreatedAtRoute("GetMenuCategoryById", new { categoryId = menuCatEnt.Id }, menuCategory)); }