public void MapMenuCategoryModelToEntity(Models.MenuCategory menuCatMod, Entities.MenuCategory menuCatEnt) { var mapper = new MapperConfiguration(configure => configure.CreateMap <Models.MenuCategory, Entities.MenuCategory>() .ForMember(dest => dest.Menu, opt => opt.Ignore())) .CreateMapper(); mapper.Map(menuCatMod, menuCatEnt); }
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)); }
public async Task <IActionResult> UpdateMenuCategory(int menuCategoryId, [FromBody] Models.MenuCategory menuCatMod) { if (menuCatMod == null) { return(BadRequest()); } var menuCatEnt = await _menuService.GetMenuCategoryAsync(menuCategoryId); if (menuCatEnt == null) { return(NotFound(new { Error = $"A menu category with the ID '{menuCategoryId}'" + $" could not be found." })); } TryValidateModel(menuCatMod); // Add validation if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } _menuMapper.MapMenuCategoryModelToEntity(menuCatMod, menuCatEnt); _menuService.UpdateCategory(menuCatEnt); if (!await _menuService.SaveChangesAsync()) { throw new Exception($"Error updating menu category '{menuCatEnt.Name}'."); } return(NoContent()); }
public Entities.MenuCategory MenuCategoryModelToEntity(Models.MenuCategory menuCategory) => new MapperConfiguration(cfg => cfg.CreateMap <Models.MenuCategory, Entities.MenuCategory>() .ForMember(dest => dest.Menu, opt => opt.Ignore())) .CreateMapper() .Map <Entities.MenuCategory>(menuCategory);