public void Can_CalculateProperValueIfFemaleHighActivity()
        {
            var userDto = new UserForUpdateDto
            {
                ActivityLevel = ActivityLevel.High
            };
            var user = new UserDetails
            {
                User = new User
                {
                    Weight = 60,
                    Height = 160,
                    Age    = 40,
                    Gender = "Female"
                }
            };

            var mockUser   = new Mock <IUserRepository>();
            var mockDMRepo = new Mock <IDailyMealRepository>();
            var _sut       = new DCICalculator(mockUser.Object, mockDMRepo.Object);

            var result = _sut.CalculateDailyDemand(userDto, user);

            Assert.Equal(2830, result);
        }
        public void Cannot_CalculateIdArgumentsAreNull()
        {
            UserForUpdateDto userDto = null;
            UserDetails      user    = null;
            var mockUser             = new Mock <IUserRepository>();
            var mockDMRepo           = new Mock <IDailyMealRepository>();
            var _sut = new DCICalculator(mockUser.Object, mockDMRepo.Object);

            Assert.Throws <ArgumentNullException>(() => _sut.CalculateDailyDemand(userDto, user));
        }
Beispiel #3
0
        public async Task <IActionResult> UpdateUserDetails(int id, [FromBody] UserForUpdateDto userforUpdateDto)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            var userFromRepo = await _repo.GetUserDetail(id);

            if (userFromRepo != null)
            {
                userFromRepo.User = await _repo.GetUser(id);

                userforUpdateDto.DailyDemand = _calc.CalculateDailyDemand(userforUpdateDto, userFromRepo);
                _mapper.Map(userforUpdateDto, userFromRepo);
            }
            else
            {
                userFromRepo = new UserDetails
                {
                    DailyDemand   = 0,
                    Carbohydrates = 0,
                    Fats          = 0,
                    Protein       = 0,
                    Description   = "",
                    User          = await _repo.GetUser(id),
                    ActivityLevel = ActivityLevel.Low,
                    UserId        = id
                };
                userforUpdateDto.DailyDemand = _calc.CalculateDailyDemand(userforUpdateDto, userFromRepo);
                _mapper.Map(userforUpdateDto, userFromRepo);
                _repo.Add(userFromRepo);
            }
            return(!await _repo.SaveAll()
                ? throw new Exception($"Updating user with {id} failed on save")
                : NoContent());
        }