public async Task <Eat> UpdateEatAsync(int loggedUser, UpdateEatRequest eat) { var e = await _uow.EatRepository.GetAll().Where(e => e.Id == eat.Id) .Include(e => e.User) .Include(e => e.EatDishes) .ThenInclude(ed => ed.Dish) .ThenInclude(d => d.DishTags) .ThenInclude(dt => dt.Tag) .Include(e => e.EatCompoundDishes) .ThenInclude(ed => ed.CompoundDish) .ThenInclude(d => d.DishCompoundDishes) .ThenInclude(dt => dt.Dish) .ThenInclude(dt => dt.DishTags) .ThenInclude(dt => dt.Tag) .FirstOrDefaultAsync(); if (e == null) { throw new NotFoundException(ExceptionConstants.NOT_FOUND, "Eat"); } e.EatType = (EatTypeEnum)eat.EatType; e.ModifiedAt = DateTime.UtcNow; foreach (var ed in e.EatDishes) { _uow.EatDishRepository.Delete(ed); } var eatDishes = new List <EatDish>(); foreach (var ed in eat.Dishes) { var eatD = new EatDish(); eatD.DishId = ed.DishId; eatD.Qty = ed.Qty; eatDishes.Add(eatD); } e.EatDishes = eatDishes; _uow.EatRepository.Update(e); await _uow.CommitAsync(); return(e); }
public async Task <Eat> CreateEatAsync(int loggedUser, CreateEatRequest eat) { // this assume the user is creating an eat every day var eatTypeAlradyExists = await _uow.EatRepository.GetAll() .Where(e => e.CreatedAt.Date == DateTime.UtcNow.Date && e.EatType == (EatTypeEnum)eat.EatType) .FirstOrDefaultAsync(); if (eatTypeAlradyExists != null) { throw new AlreadyExistsException("An eat is already configured this day."); } var e = new Eat(); e.EatType = (EatTypeEnum)eat.EatType; e.CreatedAt = DateTime.UtcNow; e.ModifiedAt = DateTime.UtcNow; e.UserId = loggedUser; var eatDishes = new List <EatDish>(); foreach (var ed in eat.Dishes) { var eatD = new EatDish(); eatD.DishId = ed.DishId; eatD.Qty = ed.Qty; eatDishes.Add(eatD); } e.EatDishes = eatDishes; await _uow.EatRepository.AddAsync(e); await _uow.CommitAsync(); return(e); }
public async Task AddOrUpdateEatAsync(int loggedUser, CreateEatRequest eat) { if (!eat.EatUtcAt.HasValue) { throw new InvalidDataException("is required", "EatUtcAt"); } var dateInUtc = eat.EatUtcAt.Value; // this assume the user is creating an eat of each type every day var eatDb = await _uow.EatRepository.GetAll() .Where(e => e.CreatedAt.Date == dateInUtc.Date && e.EatType == (EatTypeEnum)eat.EatType) .FirstOrDefaultAsync(); //this is an update if (eatDb != null) { eatDb.ModifiedAt = DateTime.UtcNow; // delete previous related dishes and compund dishes var eatCompoundDishes = eatDb.EatCompoundDishes.ToList(); var eatDishes = eatDb.EatDishes.ToList(); foreach (var item in eatCompoundDishes) { _uow.EatCompoundDishRepository.Delete(item); } foreach (var item in eatDishes) { _uow.EatDishRepository.Delete(item); } // recreate the related dishes objects var eatDishesNew = new List <EatDish>(); foreach (var ed in eat.Dishes) { var eatD = new EatDish(); eatD.DishId = ed.DishId; eatD.Qty = ed.Qty; eatDishesNew.Add(eatD); } eatDb.EatDishes = eatDishesNew; var eatCompoundDishesNew = new List <EatCompoundDish>(); foreach (var ed in eat.CompoundDishes) { var eatD = new EatCompoundDish(); eatD.CompoundDishId = ed.DishId; eatD.Qty = ed.Qty; eatCompoundDishesNew.Add(eatD); } eatDb.EatCompoundDishes = eatCompoundDishesNew; } // this is a create else { var imcKcals = await GetKCalImcAsync(loggedUser, dateInUtc); var kcal = imcKcals.kcal; var imc = imcKcals.imc; var e = new Eat(); e.EatType = (EatTypeEnum)eat.EatType; e.CreatedAt = dateInUtc; e.ModifiedAt = DateTime.UtcNow; e.UserId = loggedUser; e.EatUtcAt = eat.EatUtcAt; e.KCalAtThatMoment = kcal; e.ImcAtThatMoment = imc; var eatDishes = new List <EatDish>(); foreach (var ed in eat.Dishes) { var eatD = new EatDish(); eatD.DishId = ed.DishId; eatD.Qty = ed.Qty; eatDishes.Add(eatD); } e.EatDishes = eatDishes; var eatCompoundDishes = new List <EatCompoundDish>(); foreach (var ed in eat.CompoundDishes) { var eatD = new EatCompoundDish(); eatD.CompoundDishId = ed.DishId; eatD.Qty = ed.Qty; eatCompoundDishes.Add(eatD); } e.EatCompoundDishes = eatCompoundDishes; await _uow.EatRepository.AddAsync(e); } await _uow.CommitAsync(); await SetIsBalancedPlanAync(loggedUser, dateInUtc); }
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 */ }
public void EatSomeDish(Dish dish) { EatDish.Add(dish); Count(); }
public void RejectEatSomeDish(int i) { EatDish.RemoveAt(i); Count(); }