public async Task <IActionResult> PutMeal(int id, MealDto meal) { if (id != meal.Id) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var mealDbModel = _mealMapper.ToDbModel(meal); _context.Entry(mealDbModel).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!MealExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <MealDto> UpdateAsync(int id, MealDto entity) { _logger.LogInformation("Entered in Update with id {}, mealDto {}", id, entity); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); Meal mealFromDb = await _ctx.Meals.FindAsync(id); if (mealFromDb == null) { stopWatch.Stop(); _logger.LogInformation("Elapsed time in {0} ms", stopWatch.ElapsedMilliseconds); _logger.LogError("Meal with id {} not found", id); return(null); } Meal mealToUpdate = _mapper.Map <MealDto, Meal>(entity); _ctx.Entry(mealFromDb).CurrentValues.SetValues(mealToUpdate); await _ctx.SaveChangesAsync(); stopWatch.Stop(); _logger.LogInformation("Elapsed time in {0} ms", stopWatch.ElapsedMilliseconds); return(_mapper.Map <Meal, MealDto>(mealToUpdate)); }
public async Task <ActionResult> Update(int id, [FromBody] MealDto mealDto) { mealDto.Id = id; await _mealsService.UpdateAsync(mealDto); return(NoContent()); }
public async Task Create(MealDto entity) { Meal mealToAdd = _mapper.Map <MealDto, Meal>(entity); await _ctx.Meals.AddAsync(mealToAdd); await _ctx.SaveChangesAsync(); }
/// <inheritdoc /> public async Task CreateAsync(MealDto mealDto) { mealDto.Id = 0; var mealEntity = _mapper.Map <MealEntity>(mealDto); await _mealsRepository.CreateAsync(mealEntity); }
private MealDto GenerateMealWithRandomAliments(int alimentCount, List <AlimentDto> alimentList) { var meal = new MealDto(); var category = EnumAlimentCategory.Protides; for (int i = 0; i < alimentCount; i++) { var filteredAliments = alimentList.Where(a => a.Category == category).ToList(); meal.MealParts.Add(new MealPartDto { Aliment = filteredAliments[_random.Next(0, filteredAliments.Count() - 1)] }); switch (category) { case EnumAlimentCategory.Protides: category = EnumAlimentCategory.Glucides; break; case EnumAlimentCategory.Glucides: category = EnumAlimentCategory.Lipides; break; case EnumAlimentCategory.Lipides: category = EnumAlimentCategory.Vegetables; break; case EnumAlimentCategory.Vegetables: category = EnumAlimentCategory.Protides; break; } } return(meal); }
public async Task <Response> AddMeal(Guid id, MealDto dto) { var st = await GetStudentFullDetails(id); if (st != null) { if (st.Parents.Count > 0) { var p = st.Parents.First(); st.AddMeal(dto.MealType, dto.POSId); _context.Students.Update(st); //SMS var s = SMSLog.Create(p.Phone, $"Dear {p.Name.First} student has ordered a meal on {DateTime.UtcNow} ..."); _context.SMSLogs.Add(s); await _context.SaveChangesAsync(); //Email _emailService.SendMail(p.Email, "Meal Notice", $"Dear {p.Name.First} student has ordered a meal on {DateTime.UtcNow} ..."); return(new Response()); } return(new Response(new List <string> { "Parent not found" })); } return(new Response(new List <string> { "Student not found" })); }
// GET: api/Meals?userId=xyz public IEnumerable <MealDto> GetMeals(String userId) { if (userId == "current") { userId = User.Identity.GetUserId(); } return(msvc.GetMeals(userId).Select(b => MealDto.FromMeal(b))); }
public static bool EqualValues(this MealDto mealDto, Meal meal) { bool equal = mealDto.ID == meal.ID && mealDto.Calories == meal.Calories && mealDto.UserID == meal.UserID && mealDto.DateCreated == meal.DateCreated; return(equal); }
public async Task <MealDto> UpdateMeal(MealDto mealDto) { var meal = _mapper.Map <Meal>(mealDto); var result = await Task.Run(() => _context.Update(meal)); var dto = _mapper.Map <MealDto>(result.Entity); return(dto); }
public async Task <IHttpActionResult> Create([FromBody] MealDto meal) { await _bus.Publish(new AddMealMessage() { Meal = meal }); return(Ok()); }
private bool MatchGoals(MealDto meal) { Debug.WriteLine(meal.Accuracy); if (meal.MealParts.Any(m => m.Quantity == 0) || meal.Accuracy < 50) { return(false); } return(true); }
public bool CheckIfUnique(string parameter, MealDto entity) { if (parameter.Equals(NAME_PARAMETER)) { return(_ctx.Meals.Any(m => m.Name == entity.Name)); } return(false); }
public async Task Update(int id, MealDto entity) { Meal mealFromDb = await _ctx.Meals.FindAsync(id); Meal mealToUpdate = _mapper.Map <MealDto, Meal>(entity); _ctx.Entry(mealFromDb).CurrentValues.SetValues(mealToUpdate); await _ctx.SaveChangesAsync(); }
public IHttpActionResult GetMeal(long id) { var b = msvc.GetMeal(id); if (b == null) { return(NotFound()); } return(Ok(MealDto.FromMeal(b))); }
public async Task <IActionResult> Update(int id, [FromBody] MealDto model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var result = await _service.UpdateMeal(id, model); return(Ok(result)); }
public IHttpActionResult PostMeal(Meal m) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } msvc.CreateMeal(m); uow.Save(); return(CreatedAtRoute("DefaultApi", new { id = m.ID }, MealDto.FromMeal(m))); }
public async Task <MealDto> CreateMeal(MealDto mealDto) { var meal = _mapper.Map <Meal>(mealDto); var result = await AddAsync(meal); await _context.SaveChangesAsync(); var dto = _mapper.Map <MealDto>(result); return(dto); }
//Return all meals associated with a specified dietitian //Return all Meal plans associated with a specidied dietitian //Return all Meal sets associated with a particular dietitian //Return all Meals Plans within a specified cost range //Add meals public void addMeal(MealDto mealDto) { Meal meal = mapper.Map <Meal>(mealDto); int meal_id, ingredient_id, step_id; database.Meals.Add(meal); database.SaveChanges(); meal_id = meal.id; Console.WriteLine("This is the id: " + meal.id); }
public async Task <MealDto> UpdateMeal(int mealId, MealDto meal) { var myMeal = await _mealRepository.GetMeal(mealId); if (myMeal == null) { _logger.LogInformation("Error while changing a meal"); throw new MealNotFoundException(); } var result = await _mealRepository.UpdateMeal(meal); return(result); }
private MealDto ComputeBestQuantities(MealDto meal, MealGoalsDto goals) { var calories = meal.MealParts.Select(p => p.Aliment.Calories).ToList(); var protides = meal.MealParts.Select(p => p.Aliment.Protides).ToList(); var lipides = meal.MealParts.Select(p => p.Aliment.Lipides).ToList(); var glucides = meal.MealParts.Select(p => p.Aliment.Glucides).ToList(); var combos = GetAllPossibleCombos(meal.MealParts.Count); double minRes = double.MaxValue; List <double> bestValue = null; foreach (var combo in combos) { double ct = 0; double pt = 0; double lt = 0; double gt = 0; for (int i = 0; i < combo.Count; i++) { ct += combo[i] * calories[i]; pt += combo[i] * protides[i]; lt += combo[i] * lipides[i]; gt += combo[i] * glucides[i]; } var result = 100 * Math.Abs(ct - goals.Calories) / goals.Calories + 100 * Math.Abs(pt - goals.Protides) / goals.Protides + 100 * Math.Abs(lt - goals.Lipides) / goals.Lipides + 100 * Math.Abs(gt - goals.Glucides) / goals.Glucides; if (result < minRes) { minRes = result; bestValue = combo; } } for (int i = 0; i < meal.MealParts.Count; i++) { meal.MealParts[i].Quantity = bestValue[i]; } meal.Accuracy = Math.Max(0, 100 - minRes); return(meal); }
public IHttpActionResult createMeal(MealDto mealDto) { if (!ModelState.IsValid) { return(BadRequest("Not Valid")); } var meal = Mapper.Map <MealDto, Meal>(mealDto); _context.Meals.Add(meal); _context.SaveChanges(); mealDto.Id = meal.Id; return(Created(new Uri(Request.RequestUri + "/" + mealDto.Id), mealDto)); }
public MealDbModel ToDbModel(MealDto item) { if (item.Id == 0) { return(new MealDbModel { Name = item.Name, PricePerGr = item.PricePerGr }); } else { return(new MealDbModel { Id = item.Id, Name = item.Name, PricePerGr = item.PricePerGr }); } }
public async Task <IActionResult> PostMeal(MealDto meal) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var mealDbModel = _mealMapper.ToDbModel(meal); await _context.MealsSet.AddAsync(mealDbModel); await _context.SaveChangesAsync(); var createdMeal = _context.MealsSet.OrderByDescending(e => e.Id).First(); return(CreatedAtAction(nameof(GetMeal), new { id = createdMeal.Id }, _mealMapper.ToDto(createdMeal))); }
/// <inheritdoc /> public async Task UpdateAsync(MealDto mealDto) { MealEntity mealEntity = null; if (mealDto.Id != 0) { mealEntity = await _mealsRepository.GetByIdAsync(mealDto.Id); } if (mealEntity == null) { throw new NotFoundException(); } _mapper.Map(mealDto, mealEntity); await _mealsRepository.UpdateAsync(mealEntity); }
public async Task <MealDto> CreateAsync(MealDto entity) { _logger.LogInformation("Entered in Create with mealDto {}", entity); Stopwatch stopWatch = new Stopwatch(); Meal mealToAdd = _mapper.Map <MealDto, Meal>(entity); stopWatch.Start(); Meal newMeal = (await _ctx.Meals.AddAsync(mealToAdd)).Entity; await _ctx.SaveChangesAsync(); stopWatch.Stop(); _logger.LogInformation("Elapsed time in {0} ms", stopWatch.ElapsedMilliseconds); return(_mapper.Map <Meal, MealDto>(newMeal)); }
public IHttpActionResult updateMeal(MealDto mealDto) { if (!ModelState.IsValid) { return(BadRequest("ModelState is not Valid")); } var meal = _context.Meals.SingleOrDefault(m => m.Id == mealDto.Id); if (meal == null) { return(BadRequest("Meal not found")); } Mapper.Map(mealDto, meal); _context.SaveChanges(); return(Ok()); }
public async Task <IActionResult> Update(int id, [FromBody] MealDto mealDto) { _logger.LogInformation("Entered in [PUT] /meals endpoint with id {}, mealDto {}", id, mealDto); IActionResult result; var updatedMeal = await _mealService.UpdateAsync(id, mealDto); _logger.LogDebug("Updated meal: {}", updatedMeal); if (updatedMeal != null) { result = Ok(updatedMeal); } else { result = BadRequest(); } return(result); }
public async Task <IActionResult> Create([FromBody] MealDto mealDto) { _logger.LogInformation("Entered in [POST] /meals endpoint with mealDto {}", mealDto); IActionResult result; var newMeal = await _mealService.CreateAsync(mealDto); _logger.LogDebug("New meal: {}", newMeal); if (newMeal != null) { result = Created("", newMeal); } else { result = BadRequest(); } return(result); }
public async Task Update_WhenCalled_ReturnsMeal() { // Arrange var mealDto = _fixture.Items.FirstOrDefault(); var expectedMealDto = new MealDto { Id = mealDto.Id, Name = "MealDto 11" }; _mockRepository.Setup(r => r.UpdateAsync(mealDto.Id, mealDto)).ReturnsAsync(expectedMealDto); // Act var mealResult = await _sut.UpdateAsync(mealDto.Id, mealDto); // Assert _mockRepository.Verify(r => r.UpdateAsync(It.IsAny <int>(), It.IsAny <MealDto>()), Times.Once); mealResult.Should().NotBeNull(); mealResult.Should().BeEquivalentTo(expectedMealDto); }