public async Task <ActionResult <MealCategoriesDto> > CreateMealCategory([FromBody] MealCategoriesCreationDto mealCategoryDTO) { if (mealCategoryDTO == null) { return(BadRequest()); } var mealCategoryToAdd = _mapper.Map <MealCategoriesModel>(mealCategoryDTO); var id = await _mealCategory.AddReturnAsync(mealCategoryToAdd); if (id == 0) { throw new Exception("An error occured while creating this meal category"); } var mealCatgeoryToReturn = _mapper.Map <MealCategoriesDto>(mealCategoryDTO); return(CreatedAtRoute("GetMealCategory", new { id = id }, mealCategoryDTO)); }
public async Task <ActionResult <MealCategoriesDto> > UpdateMealCategory(int id, [FromBody] MealCategoriesCreationDto mealCategoryDTO) { if (mealCategoryDTO == null) { return(BadRequest()); } var mealCategoryToFromRepo = await _mealCategory.GetFirstAsync(r => r.Id == id); if (mealCategoryToFromRepo == null) { return(NotFound()); } var mealCategoryToUpdate = _mapper.Map(mealCategoryDTO, mealCategoryToFromRepo); try { await _mealCategory.UpdateAsync(mealCategoryToUpdate); } catch (Exception) { throw new Exception("could not update meal category"); } return(NoContent()); }