Exemple #1
0
        public IActionResult AddCardioTraining(TrainingViewModel trainingViewModel)
        {
            try
            {
                if (String.IsNullOrEmpty(trainingViewModel.Exercise.Name))
                {
                    ModelState.AddModelError("Exercise", "Please fill in an exercise");
                    return(View(trainingViewModel));
                }

                IUser             user              = UserFactory.GetUser();
                IUserCollection   userCollection    = UserCollectionFactory.GetUserCollection();
                ExerciseDTO       exerciseDTO       = userCollection.GetExercise(trainingViewModel.Exercise.Name);
                CardioTrainingDTO cardioTrainingDTO = new CardioTrainingDTO(exerciseDTO, trainingViewModel.Distance, new TimeSpan(0, trainingViewModel.Minutes, trainingViewModel.Seconds), Guid.Parse(User.FindFirst("Id").Value), DateTime.Now, TrainingTypeDTO.Cardio);
                user.AddCardioTraining(cardioTrainingDTO);
                TempData["JustAddedTraining"] = true;
                return(LocalRedirect("/Home/Index"));
            }

            catch
            {
                TempData["Error"] = true;
                return(LocalRedirect("/Training/AddCardioTraining"));
            }
        }
        public void AddCardioTraining(CardioTrainingDTO cardioTraining)
        {
            ITrainingDAL      dal = TrainingDALFactory.GetTrainingDAL();
            CardioTrainingDTO cardioTrainingToAdd = new CardioTrainingDTO(cardioTraining.Exercise, cardioTraining.Distance, cardioTraining.Time, Guid.NewGuid(), cardioTraining.UserID, cardioTraining.Date, cardioTraining.TrainingType);

            dal.AddCardioTraining(cardioTrainingToAdd);
        }
        public CardioTrainingDTO GetCardioTraining(string trainingID)
        {
            ITrainingDAL      trainingDAL    = TrainingDALFactory.GetTrainingDAL();
            CardioTrainingDTO cardioTraining = trainingDAL.GetCardioTraining(trainingID);

            return(cardioTraining);
        }
        public void AddCardioTraining()
        {
            //arrange
            IUser              user              = UserFactory.GetUser();
            IUserCollection    userCollection    = UserCollectionFactory.GetUserCollection();
            IUserCollectionDAL userCollectionDAL = UserCollectionDALFactory.GetUserCollectionDAL();
            ITrainingDAL       dal         = TrainingDALFactory.GetTrainingDAL();
            ExerciseDTO        exerciseDTO = userCollection.GetExercise("Running");
            UserDTO            userDTO     = new UserDTO("TempAccountCardioTraining", Guid.NewGuid(), "TempPassword", null, null);

            userCollectionDAL.AddUser(userDTO);
            CardioTrainingDTO cardioTrainingDTO = new CardioTrainingDTO(
                exerciseDTO,
                5.44M,
                new TimeSpan(0, 28, 45),
                userDTO.UserID,
                DateTime.Now,
                TrainingTypeDTO.Cardio);

            //act
            user.AddCardioTraining(cardioTrainingDTO);

            //assert
            Guid trainingID = dal.GetUserTrainings(userDTO.UserID.ToString())[0].TrainingID;
            CardioTrainingDTO trainingFromDB = dal.GetCardioTraining(trainingID.ToString());

            Assert.AreEqual(userDTO.UserID, trainingFromDB.UserID);
            Assert.AreEqual(cardioTrainingDTO.Date.ToLongDateString(), trainingFromDB.Date.ToLongDateString());
            Assert.AreEqual(TrainingTypeDTO.Cardio, trainingFromDB.TrainingType);
        }
        private CardioTrainingDTO ConvertCardioTraining(CardioTraining cardioTraining)
        {
            CardioTrainingDTO cardioTrainingDTO = new CardioTrainingDTO(
                ConvertExercise(cardioTraining.Exercise),
                cardioTraining.Distance,
                cardioTraining.Time,
                cardioTraining.TrainingID,
                cardioTraining.UserID,
                cardioTraining.Date,
                (TrainingTypeDTO)cardioTraining.TrainingType);

            return(cardioTrainingDTO);
        }
Exemple #6
0
        private CardioTraining ConvertCardioTrainingDTO(CardioTrainingDTO cardioTrainingDTO)
        {
            CardioTraining cardioTraining = new CardioTraining(
                ConvertExerciseDTO(cardioTrainingDTO.Exercise),
                cardioTrainingDTO.Distance,
                cardioTrainingDTO.Time,
                cardioTrainingDTO.TrainingID,
                cardioTrainingDTO.UserID,
                cardioTrainingDTO.Date,
                (TrainingType)cardioTrainingDTO.TrainingType
                );

            return(cardioTraining);
        }
        public CardioTrainingDTO GetCardioTraining(string trainingID)
        {
            using (SqlConnection connection = new SqlConnection(GetConnectionString()))
            {
                SqlCommand cmdTraining = new SqlCommand("SELECT * FROM Trainings WHERE ID = @TrainingID", connection);
                cmdTraining.Parameters.AddWithValue("@TrainingID", trainingID);

                DateTime        date         = DateTime.MinValue;
                Guid            userID       = Guid.Empty;
                ExerciseDTO     exercise     = new ExerciseDTO();
                decimal         distance     = 0;
                TrainingTypeDTO trainingType = TrainingTypeDTO.Cardio;

                connection.Open();
                using (SqlDataReader reader = cmdTraining.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        date         = Convert.ToDateTime(reader["Date"]);
                        userID       = Guid.Parse(reader["UserID"].ToString());
                        trainingType = (TrainingTypeDTO)Enum.Parse(typeof(TrainingTypeDTO), reader["TrainingType"].ToString());
                    }
                }

                SqlCommand cmdCardioTraining = new SqlCommand("SELECT * FROM CardioTrainings WHERE TrainingID = @TrainingID", connection);
                cmdCardioTraining.Parameters.AddWithValue("@TrainingID", trainingID);

                using (SqlDataReader cardioReader = cmdCardioTraining.ExecuteReader())
                {
                    while (cardioReader.Read())
                    {
                        distance = Convert.ToDecimal(cardioReader["Distance"].ToString());
                        exercise = GetExerciseDTO(cardioReader["ExerciseID"].ToString());
                        long              ticks             = Convert.ToInt64(cardioReader["Time"].ToString());
                        TimeSpan          time              = new TimeSpan(ticks);
                        CardioTrainingDTO cardioTrainingDTO = new CardioTrainingDTO(exercise, distance, time, Guid.Parse(trainingID), userID, date, trainingType);
                        return(cardioTrainingDTO);
                    }
                }
                return(null);
            }
        }
        public void AddCardioTraining(CardioTrainingDTO trainingDTO)
        {
            using (SqlConnection connection = new SqlConnection(GetConnectionString()))
            {
                SqlCommand cmd = new SqlCommand("" +
                                                "INSERT INTO Trainings " +
                                                "VALUES(@TrainingID, @UserID, @Date, @TrainingType) " +
                                                "INSERT INTO CardioTrainings " +
                                                "VALUES(@TrainingID, @ExerciseID, @Distance, @Time)", connection);
                cmd.Parameters.AddWithValue("@TrainingID", trainingDTO.TrainingID);
                cmd.Parameters.AddWithValue("@ExerciseID", trainingDTO.Exercise.ExerciseID);
                cmd.Parameters.AddWithValue("@Distance", trainingDTO.Distance);
                cmd.Parameters.AddWithValue("@Time", trainingDTO.Time.Ticks);
                cmd.Parameters.AddWithValue("@Date", trainingDTO.Date);
                cmd.Parameters.AddWithValue("@UserID", trainingDTO.UserID);
                cmd.Parameters.AddWithValue("@TrainingType", trainingDTO.TrainingType);

                connection.Open();
                cmd.ExecuteNonQuery();
                connection.Close();
            }
        }
Exemple #9
0
        public IActionResult TrainingDetail(Guid id)
        {
            try
            {
                User user = new User();
                TrainingViewModel trainingVM = ConvertTrainingDTO(user.GetTraining(id.ToString()));

                if (trainingVM.TrainingType == TrainingType.Strength)
                {
                    WeightTrainingDTO     weightTrainingDTO = user.GetWeightTraining(id.ToString());
                    List <RoundViewModel> roundViewModels   = new List <RoundViewModel>();
                    foreach (var roundDTO in weightTrainingDTO.GetRounds())
                    {
                        roundViewModels.Add(ConvertRoundDTO(roundDTO));
                    }
                    trainingVM.Rounds = roundViewModels;
                }

                else
                {
                    CardioTrainingDTO cardioTrainingDTO = user.GetCardioTraining(id.ToString());
                    trainingVM.Exercise   = ConvertExerciseDTOToVM(cardioTrainingDTO.Exercise);
                    trainingVM.Distance   = Math.Round(cardioTrainingDTO.Distance, 2);
                    trainingVM.Minutes    = cardioTrainingDTO.Time.Minutes;
                    trainingVM.Seconds    = cardioTrainingDTO.Time.Seconds;
                    trainingVM.TrainingID = cardioTrainingDTO.TrainingID;
                }
                return(View(trainingVM));
            }

            catch
            {
                TempData["Error"] = true;
                return(LocalRedirect("/Home/Index"));
            }
        }