public async Task <IActionResult> Post([FromRoute] int userId, [FromBody] NutritionTarget newNutritionTarget)
        {
            _claimsManager.Init(HttpContext.User);
            _logger.LogInformation(
                $"User with id: {_claimsManager.GetUserIdClaim()} attempting to create Nutrition target for userId: {userId}");

            if (!_claimsManager.VerifyUserId(userId))
            {
                _logger.LogInformation($"user was denied access to this resource");
                return(Forbid());
            }

            if (await _context
                .NutritionTargets
                .Where(_ => _.UserId == userId)
                .AnyAsync())
            {
                _logger.LogInformation($"there is already a nutrition target for user: {userId}");
                return(BadRequest("nutrition target for this user already exists"));
            }

            _logger.LogInformation("adding new nutrition target to database");
            await _context
            .NutritionTargets
            .AddAsync(newNutritionTarget);

            _logger.LogInformation("saving changes");
            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(Get), new { userId = userId }, newNutritionTarget));
        }
Пример #2
0
        public async Task <IActionResult> Summary()
        {
            FitnessUser currentUser = await userManager.GetUserAsync(HttpContext.User);

            FoodRecord[] userRecords = await dbContext.FoodRecords
                                       .Where(record => record.User == currentUser && record.ConsumptionDate >= DateTime.Today.AddDays(-28))
                                       .Include(record => record.Food)
                                       .ToArrayAsync();

            NutritionTarget userTarget = await dbContext.NutritionTargets.FirstOrDefaultAsync(record => record.User == currentUser);

            if (userTarget == null)
            {
                userTarget = new NutritionTarget();
            }

            NutritionSummaryModel summaryModel = new NutritionSummaryModel()
            {
                Records = userRecords,
                Target  = userTarget
            };

            return(View(summaryModel));
        }
 public async Task <IActionResult> Put([FromRoute] int userId, [FromBody] NutritionTarget modifiedNutritionTarget)
 {
     return(Ok());
 }