public async Task <ActionResult <FoodDto> > UpdateFood(int id, [FromBody] FoodCreationDto foodToUpdateDTO) { if (foodToUpdateDTO == null) { return(BadRequest()); } var foodFromRepo = await _food.GetFirstAsync(r => r.Id == id); if (foodFromRepo == null) { return(NotFound()); } _mapper.Map(foodToUpdateDTO, foodFromRepo); try { await _food.UpdateAsync(foodFromRepo); } catch (Exception) { throw new Exception("could not update food"); } return(NoContent()); }
public async Task <ActionResult <FoodDto> > Replace(FoodCreationDto foodDto, string id) { var na = new Food(ObjectId.Parse(id), foodDto.Name, foodDto.Unit, foodDto.Value, foodDto.Calories, foodDto.Protein, foodDto.TotalCarbohydrates, foodDto.Sugar, foodDto.Fiber, foodDto.Fat); await foodCollection.ReplaceById(id, na); return(Ok(200)); }
public async Task <ActionResult <FoodDto> > Add(FoodCreationDto foodDto) { var na = new Food(ObjectId.Empty, foodDto.Name, foodDto.Unit, foodDto.Value, foodDto.Calories, foodDto.Protein, foodDto.TotalCarbohydrates, foodDto.Sugar, foodDto.Fiber, foodDto.Fat); await foodCollection.InsertOneAsync(na); return(CreatedAtRoute(nameof(GetSingleFood), new { na.Id }, new FoodDto(na.Id.ToString(), na.Name, na.Unit, na.Value, na.Calories, na.Protein, na.TotalCarbohydrates, na.Sugar, na.Fiber, na.Fat))); }
public async Task <ActionResult <FoodDto> > CreateFood([FromBody] FoodCreationDto foodCreationDTO) { if (foodCreationDTO == null) { return(BadRequest()); } var foodToAdd = _mapper.Map <FoodModel>(foodCreationDTO); var id = await _food.AddReturnAsync(foodToAdd); if (id == 0) { throw new Exception("An error occured while creating this meal category"); } var foodToReturn = _mapper.Map <FoodDto>(foodCreationDTO); return(CreatedAtRoute("GetFood", new { id = id }, foodToReturn)); }