示例#1
0
        public async Task <ActionResult <UserTraining> > PostUserTraining(UserTraining userTraining)
        {
            var currentId = User.Claims.FirstOrDefault(el => el.Type == ClaimTypes.NameIdentifier)?.Value;

            if (currentId != userTraining.UserId && !User.IsInRole("admin"))
            {
                return(BadRequest());
            }

            var user = await _context.Users
                       .Include(el => el.UserDiseases)
                       .FirstAsync(el => el.Id == userTraining.UserId);

            var training = await _context.Training.FindAsync(userTraining.TrainingId);

            var trainingIndex = TrainingCalculator.CalculateExercise(user, training);

            userTraining.ExerciseIndex = trainingIndex;

            _context.UserTraining.Add(userTraining);
            await _context.SaveChangesAsync();

            userTraining.Training = null;
            return(Ok(userTraining));
        }
示例#2
0
        public async Task <ActionResult <UserTraining> > Calculate(UserTraining userTraining)
        {
            var user = await _context.Users
                       .Include(el => el.UserDiseases)
                       .FirstAsync(el => el.Id == userTraining.UserId);

            var training = await _context.Training.FindAsync(userTraining.TrainingId);

            var trainingIndex = TrainingCalculator.CalculateExercise(user, training);

            userTraining.ExerciseIndex = trainingIndex;

            userTraining.Training = null;
            return(Ok(userTraining));
        }
示例#3
0
        public async Task <ActionResult <IEnumerable <Training> > > Recommend(string id)
        {
            if (!_context.UserTraining.Any(el => el.UserId == id))
            {
                return(BadRequest("Неможливо побудувати рекомендації для данного користувача через відсутність його оцінок у моделі"));
            }

            var trainingsToCheck = await _context.Training
                                   .Include(el => el.UserTrainings)
                                   .Where(el => el.UserTrainings.All(s => s.UserId != id) &&
                                          el.UserTrainings.Any())
                                   .ToListAsync();

            var user = await _context.Users
                       .Include(el => el.UserDiseases)
                       .FirstAsync(el => el.Id == id);

            var result = new List <Training>();

            foreach (var training in trainingsToCheck)
            {
                var input = new ModelInput
                {
                    UserId        = user.Id,
                    TrainingId    = float.Parse(training.Id.ToString()),
                    Activity      = float.Parse(((int)user.Activity).ToString()),
                    Age           = float.Parse(user.Age.ToString()),
                    Duration      = float.Parse(training.Duration.ToString()),
                    ExerciseIndex = float.Parse(TrainingCalculator.CalculateExercise(user, training).ToString()),
                    FigureType    = float.Parse(((int)user.FigureType).ToString()),
                    Gender        = float.Parse(((int)user.Gender).ToString()),
                    Goal          = float.Parse(((int)user.Goal).ToString()),
                    Height        = float.Parse(user.Height.ToString()),
                    Weight        = float.Parse(user.Weight.ToString()),
                    Level         = float.Parse(((int)training.Level).ToString())
                };
                ModelOutput output;
                try
                {
                    output = ConsumeModel.Predict(input);
                }
                catch (Exception ex)
                {
                    output = new ModelOutput()
                    {
                        Score = 0
                    };
                }

                if (output.Score > 3.5)
                {
                    training.UserTrainings = new[] { new UserTraining()
                                                     {
                                                         Score = (int)Math.Round(output.Score, 0)
                                                     } };
                    result.Add(training);
                }
            }

            return(result);
        }