public async Task <IActionResult> AddEats([FromBody] CreateBulkEatRequest eat) { var loggedUser = User.GetUserIdFromToken(); await _eatService.CreateBulkEatAsync(loggedUser, eat); return(Ok()); }
public async Task CreateBulkEatAsync(int loggedUser, CreateBulkEatRequest eat) { var userEats = await _uow.EatRepository.GetAll().Where(e => e.UserId == loggedUser && e.CreatedAt.Date == eat.DateInUtc.Date) .Include(e => e.EatDishes) .Include(e => e.EatCompoundDishes) .Include(e => e.EatSchedule) .ThenInclude(es => es.Schedule) .ToListAsync(); var isNew = true; bool? oldPlanBalanced = null; DateTime?oldPlanCreatedAt = null; foreach (var d in userEats) { if (isNew) { isNew = false; oldPlanBalanced = d.IsBalancedPlan; oldPlanCreatedAt = d.PlanCreatedAt; } //var jobId = d.EatSchedule?.Schedule?.JobId; //if (!String.IsNullOrEmpty(jobId)) // await _scheduleService.RemoveJobIfExistIfExistAsync(jobId); _uow.EatRepository.Delete(d); } var eatResult = new List <Eat>(); ////this days not eat yet //if (userEats.Count == 0) //{ var kcal = await _accountService.GetKCalAsync(loggedUser); var imc = await _accountService.GetIMCAsync(loggedUser); foreach (var item in eat.Eats) { var e = new Eat(); e.CreatedAt = eat.DateInUtc; e.ModifiedAt = DateTime.UtcNow; e.EatType = (EatTypeEnum)item.EatType; e.UserId = loggedUser; e.IsBalanced = eat.IsBalanced; e.EatUtcAt = item.EatUtcAt; e.KCalAtThatMoment = kcal; e.ImcAtThatMoment = imc; if (IsValidDateForPlan(eat.DateInUtc, eat.DateTimeInUserLocalTime)) { e.PlanCreatedAt = eat.DateInUtc; e.IsBalancedPlan = eat.IsBalanced; } else { e.PlanCreatedAt = oldPlanCreatedAt; e.IsBalancedPlan = oldPlanBalanced; } await _uow.EatRepository.AddAsync(e); eatResult.Add(e); foreach (var d in item.Dishes) { var count = await _uow.DishRepository.GetAll().CountAsync(repo => repo.Id == d.DishId); if (count > 0) { var ed = new EatDish(); ed.DishId = d.DishId; ed.Eat = e; ed.Qty = d.Qty; await _uow.EatDishRepository.AddAsync(ed); } } foreach (var d in item.CompoundDishes) { var count = await _uow.CompoundDishRepository.GetAll().CountAsync(repo => repo.Id == d.DishId); if (count > 0) { var ed = new EatCompoundDish(); ed.CompoundDishId = d.DishId; ed.Eat = e; ed.Qty = d.Qty; await _uow.EatCompoundDishRepository.AddAsync(ed); } } } //} ////needs to update or add //else //{ // foreach (var item in eat.Eats) // { // var ue = userEats.Where(u => u.EatType == (EatTypeEnum)item.EatType).FirstOrDefault(); // if (ue != null) // { // foreach (var ud in ue.EatDishes) // { // _uow.EatDishRepository.Delete(ud); // //await _uow.CommitAsync(); // } // foreach (var uf in item.Dishes) { var ed = new EatDish(); ed.DishId = uf.DishId; // ed.EatId = ue.Id; ed.Qty = uf.Qty; // await _uow.EatDishRepository.AddAsync(ed); // } // } // ue.ModifiedAt = DateTime.UtcNow; // _uow.EatRepository.Update(ue); // //await _uow.CommitAsync(); // } //} await _uow.CommitAsync(); /* * var wantNotification = await _userService.GetUserOptInNotificationAsync(loggedUser, SettingsConstants.PREPARE_EAT_REMINDER); * if (wantNotification) * { * foreach (var e in eatResult) * { * if (e.EatUtcAt.HasValue && e.EatUtcAt.Value > DateTime.UtcNow) * { * var timeReminder = e.EatUtcAt.Value.AddMinutes(-10); * var schedule = await _scheduleService.ScheduleEatReminderNotificationAsync(e, timeReminder); * e.EatSchedule = new EatSchedule * { * Schedule = schedule * }; * * await _uow.EatRepository.UpdateAsync(e, e.Id); * } * } * await _uow.CommitAsync(); * } * API reminder not in use anymore */ }