Esempio n. 1
0
        private async Task <IActionResult> _InsertMeal(Meal newMeal)
        {
            var authRet = await ControllerUtils.AuthorizeUserAsync(HttpContext, _context, _testUserName, _adminRoleName, _defaultRoleName, _authorizedRolesUsers);

            if (authRet.ErrorMessage != null)
            {
                return(BadRequest(authRet.ErrorMessage));
            }
            if (newMeal.Calories < 0)
            {
                newMeal.Calories = 0;
            }
            var erroMessage = await ValidateMealAsync(authRet.ApiUser, newMeal);

            if (erroMessage != null)
            {
                return(BadRequest(erroMessage));
            }
            var isAdmin = _authorizedRolesAdmins.Contains(authRet.ApiUserRole.Name);

            if (!isAdmin)
            {
                if (newMeal.UserId != authRet.ApiUser.Id)
                {
                    return(BadRequest($"Only User Managers and Administrators can add meals for other users. Please use your own UserId <authRet.ApiUser.Id>"));
                }
            }
            var newUser = newMeal.UserId == authRet.ApiUser.Id ? authRet.ApiUser : await _context.Users.FindAsync(newMeal.UserId);

            var newDailyUserCalories = await _context.DailyUserCalories.FirstOrDefaultAsync(duc => duc.UserId == newUser.Id && duc.Date.Date == newMeal.Date.Date);

            if (newDailyUserCalories == null)
            {
                newDailyUserCalories = new DailyUserCalories {
                    UserId = newUser.Id, Date = newMeal.Date.Date, Calories = newMeal.Calories
                };
                _context.DailyUserCalories.Add(newDailyUserCalories);
            }
            else
            {
                var origDailyUserCalories = newDailyUserCalories.Calories;

                newDailyUserCalories.Calories += newMeal.Calories;
                _context.Entry(newDailyUserCalories).State = EntityState.Modified; // Update database

                if (origDailyUserCalories < newUser.DailyCalories && newDailyUserCalories.Calories >= newUser.DailyCalories)
                {
                    await UpdateSameDayMealsWithinLimit(newMeal, withinLimit : false);
                }
            }
            newMeal.Id          = 0;
            newMeal.WithinLimit = newDailyUserCalories.Calories < newUser.DailyCalories;
            _context.Meals.Add(newMeal);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("Get", new Meal {
                Id = newMeal.Id
            }, newMeal));
        }
Esempio n. 2
0
        private async Task <IActionResult> _UpdateIdMeal(AuthResult authRet, int id, Meal newMeal)
        {
            if (id != newMeal.Id)
            {
                return(BadRequest("Id not equal to meal.Id"));
            }
            var oldMeal = _context.Meals.Find(id);

            if (oldMeal == null)
            {
                return(NotFound($"Not found Id={id}"));
            }
            if (newMeal.UserId < 1)
            {
                newMeal.UserId = oldMeal.UserId;
            }
            if (newMeal.Date.IsDefault() && newMeal.Time.IsDefault())
            {
                newMeal.Date = oldMeal.Date;
                newMeal.Time = oldMeal.Time;
            }
            else if (newMeal.Date.IsDefault())
            {
                newMeal.Date = newMeal.Time.Date;
            }
            else if (newMeal.Time.IsDefault())
            {
                newMeal.Time = newMeal.Date.Date + (oldMeal.Time.NotDefault() ? oldMeal.Time.TimeOfDay : default(TimeSpan));
            }
            if (newMeal.Description == null || newMeal.Description.Trim().Length < 1)
            {
                newMeal.Description = oldMeal.Description;
            }
            if (newMeal.Calories == 0) // If less than zero then resend to Nutritionix service when Validated
            {
                newMeal.Calories = oldMeal.Calories;
            }
            var erroMessage = await ValidateMealAsync(authRet.ApiUser, newMeal);

            if (erroMessage != null)
            {
                return(BadRequest(erroMessage));
            }
            var isAdmin = _authorizedRolesAdmins.Contains(authRet.ApiUserRole.Name);

            if (!isAdmin)
            {
                if (newMeal.UserId != authRet.ApiUser.Id)
                {
                    return(BadRequest($"Only User Managers and Administrators can modify UserId. Please use your own UserId <{authRet.ApiUser.Id}>"));
                }
            }
            // No DailyUserCalories information has changed
            if (newMeal.Calories == oldMeal.Calories && newMeal.UserId == oldMeal.UserId && newMeal.Date == oldMeal.Date)
            {
                newMeal.WithinLimit = oldMeal.WithinLimit;
            }
            // Calories changed but same User and Date
            else if (newMeal.UserId == oldMeal.UserId && newMeal.Date == oldMeal.Date)
            {
                var dealUser          = newMeal.UserId == authRet.ApiUser.Id ? authRet.ApiUser : _context.Users.Find(newMeal.UserId);
                var dailyUserCalories = await _context.DailyUserCalories.FirstAsync(duc => duc.UserId == dealUser.Id && duc.Date.Date == newMeal.Date.Date);

                var origDailyUserCalories = dailyUserCalories.Calories;

                dailyUserCalories.Calories += newMeal.Calories - oldMeal.Calories;
                _context.Entry(dailyUserCalories).State = EntityState.Modified; // Update database (locking DailyUserCalories table first)

                if (origDailyUserCalories < dealUser.DailyCalories && dailyUserCalories.Calories >= dealUser.DailyCalories)
                {
                    await UpdateSameDayMealsWithinLimit(newMeal, withinLimit : false); // Update other Deals on same day
                }
                newMeal.WithinLimit = dailyUserCalories.Calories < dealUser.DailyCalories;
            }
            // User or Date have changed
            else
            {
                var oldUser = oldMeal.UserId == authRet.ApiUser.Id ? authRet.ApiUser : _context.Users.Find(oldMeal.UserId);
                var newUser = newMeal.UserId == authRet.ApiUser.Id ? authRet.ApiUser : _context.Users.Find(newMeal.UserId);
                var oldDailyUserCalories = await _context.DailyUserCalories.FirstAsync(duc => duc.UserId == oldUser.Id && duc.Date.Date == oldMeal.Date.Date);

                var newDailyUserCalories = await _context.DailyUserCalories.FirstOrDefaultAsync(duc => duc.UserId == newUser.Id && duc.Date.Date == newMeal.Date.Date);

                var origOldDailyUserCalories = oldDailyUserCalories.Calories;

                oldDailyUserCalories.Calories -= oldMeal.Calories;
                _context.Entry(oldDailyUserCalories).State = EntityState.Modified; // Update database (locking DailyUserCalories table first)

                if (newDailyUserCalories == null)
                {
                    newMeal.WithinLimit  = newMeal.Calories < newUser.DailyCalories;
                    newDailyUserCalories = new DailyUserCalories {
                        UserId = newUser.Id, Date = newMeal.Date.Date, Calories = newMeal.Calories
                    };
                    _context.DailyUserCalories.Add(newDailyUserCalories);
                }
                else
                {
                    var origNewDailyUserCalories = newDailyUserCalories.Calories;

                    newDailyUserCalories.Calories += newMeal.Calories;
                    _context.Entry(newDailyUserCalories).State = EntityState.Modified; // Update database (locking DailyUserCalories table first)

                    if (origNewDailyUserCalories < newUser.DailyCalories && newDailyUserCalories.Calories >= newUser.DailyCalories)
                    {
                        await UpdateSameDayMealsWithinLimit(newMeal, withinLimit : false); // Update other Deals on same day
                    }
                }
                if (origOldDailyUserCalories >= oldUser.DailyCalories && oldDailyUserCalories.Calories < oldUser.DailyCalories)
                {
                    await UpdateSameDayMealsWithinLimit(oldMeal, withinLimit : true); // Update other Deals on same day
                }
                newMeal.WithinLimit = newDailyUserCalories.Calories < newUser.DailyCalories;
            }
            oldMeal.AssignFrom(newMeal); // Copy attributes from meal to oldMeal so we can update the database
            _context.Entry(oldMeal).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            return(CreatedAtAction("Get", new Meal {
                Id = newMeal.Id
            }, newMeal));
        }