Ejemplo n.º 1
0
            public async Task <Response> Handle(Request request, CancellationToken cancellationToken)
            {
                var account =
                    await
                    (
                        from setting in _context.AccountSettings
                        where setting.AccountId == _currentAccount.Id
                        select new
                {
                    setting.TargetCalories,
                    CurrentCalories = setting.Account.Meals.Where(x => x.Date == request.Date).Sum(x => x.Calories)
                }
                    ).SingleOrDefaultAsync(cancellationToken);

                if (!request.Calories.HasValue)
                {
                    request.Calories = await _caloriesService.GetCaloriesAsync(request.Text);
                }

                var meal = request.ToMeal(_currentAccount.Id);

                // This is a business operation. So, it should not be in the related mapper extension
                meal.CalorieStatus = account?.CurrentCalories.GetValueOrDefault() + request.Calories < account?.TargetCalories;

                _context.Meals.Add(meal);

                await _context.SaveChangesAsync(cancellationToken);

                return(new Response
                {
                    Id = meal.Id
                });
            }
Ejemplo n.º 2
0
            public async Task <Unit> Handle(Request request, CancellationToken cancellationToken)
            {
                var query = _context.Meals.Where(x => x.Id == request.Id);

                // User can only update her own meal
                // Admin can update all meals
                if (_currentAccount.Role != Role.Admin)
                {
                    query = query.Where(x => x.Account.Id == _currentAccount.Id);
                }

                var meal = await query.SingleOrDefaultAsync(cancellationToken);

                if (meal == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, ResourceConstant.NotFound);
                }

                var account =
                    await
                    (
                        from setting in _context.AccountSettings
                        where setting.AccountId == _currentAccount.Id
                        select new
                {
                    setting.TargetCalories,
                    CurrentCalories = setting.Account.Meals.Where(x => x.Date == request.Date).Sum(x => x.Calories)
                }
                    ).SingleOrDefaultAsync(cancellationToken);

                if (!request.Calories.HasValue)
                {
                    request.Calories = await _caloriesService.GetCaloriesAsync(request.Text);
                }

                meal.Date = request.Date;
                meal.Time = DateTime.ParseExact(request.Time, ResourceConstant.TimeFormat, CultureInfo.InvariantCulture,
                                                DateTimeStyles.None);
                meal.Text     = request.Text;
                meal.Calories = request.Calories;
                // CalorieStatus calculation should be optimized
                meal.CalorieStatus = account?.CurrentCalories.GetValueOrDefault() + request.Calories - meal.Calories < account?.TargetCalories;

                await _context.SaveChangesAsync(cancellationToken);

                return(default);