Example #1
0
        public void WriteExercise()
        {
            Task task = new Task(async() =>
            {
                var exerciseToWrite = new Models.Exercise()
                {
                    Name         = "Squat",
                    ExampleVideo = "Http://SomeVideo",
                    FormVideo    = "http://SomeForm",
                };

                IDataWriter dataWriter = new DataWriter(Configuration)
                {
                    ConnectionStringName = ConnectionStringName
                };
                await dataWriter.WriteExercise(exerciseToWrite);

                IDataReader reader = new DataReader(Configuration)
                {
                    ConnectionStringName = ConnectionStringName
                };
                var exercises = await reader.GetExercises();
                if (!exercises.Contains(exerciseToWrite))
                {
                    throw new Exception("Exercise failed to write to database");
                }
            });

            task.RunSynchronously();
        }
 public ExerciseDetailsDTO(Models.Exercise e)
 {
     ID          = e.ID;
     Name        = e.Name;
     Description = e.Description;
     VideoUrl    = e.VideoUrl;
     MuscleGroup = e.MuscleGroup.Name;
 }
        public CreateResponse CreateExercise(Models.Exercise exercise)
        {
            var resp = new CreateResponse();

            //set datetime
            exercise.date = DateTime.Now.ToString("s").Replace('T', ' ') + "UTC";
            DataAccess.DataAccess da = new DataAccess.DataAccess();
            da.CreateExerciseCollection(exercise);

            return(resp);
        }
        public async Task <IActionResult> Put(int id, [FromBody] Models.Exercise exercise)
        {
            if (id != exercise.ID)
            {
                return(BadRequest());
            }

            context.Entry(exercise).State = EntityState.Modified;

            await context.SaveChangesAsync();

            return(Ok(exercise));
        }
        public async Task <IActionResult> Post([FromBody] Models.Exercise exercise)
        {
            var workout = context.Workout.SingleOrDefault(w => w.ID == exercise.WorkoutID);

            if (workout == null)
            {
                return(NotFound());
            }

            context.Exercises.Add(exercise);
            await context.SaveChangesAsync();

            return(Ok(exercise));
        }
Example #6
0
        public ActionResult Save(string title, string intro,
                                 string[] exerciseTitles, string[] exerciseContent, string[] exerciseResponses,
                                 string name, string key, string created,
                                 DateTime?due, bool isPublished, DateTime?publishDate)
        {
            labKey = key;
            DateTime dtCreated = DateTime.Parse(created);

            Models.Lab lab = new Models.Lab();
            lab.Title = title;
            lab.Intro = intro;

            /* Lab DueDate will be set from the Lab Manager once it is created
             *  I left this here as an example of the expected format
             */
            lab.DueDate = due;
            object pubObj = publishDate; // handle evaluating publish date

            if (pubObj == null)
            {
                pubObj = DBNull.Value;
            }
            lab.ExerciseList = new List <Models.Exercise>();
            List <Exercise> list     = new List <Exercise>();
            int             children = 0;

            for (int i = 0; i < exerciseTitles.Length; i++)
            {
                Models.Exercise e = new Models.Exercise()
                {
                    ExerciseTitle = exerciseTitles[i],
                    Content       = exerciseContent[i],
                    ExerciseList  = new List <Exercise>(),
                    Response      = ""
                };
                e.ExerciseList = new List <Exercise>();
                if (!int.TryParse(exerciseResponses[i], out children))
                {
                    e.Response = exerciseResponses[i]; // This feature has yet to be implemented
                    list.Add(e);
                }
                else
                {
                    for (int j = 0; j < children; j++)
                    {
                        i++;
                        Exercise ex = new Exercise()
                        {
                            ExerciseTitle = exerciseTitles[i],
                            Content       = exerciseContent[i],
                            Response      = exerciseResponses[i]
                        };
                        e.ExerciseList.Add(ex);
                        list.Add(ex);
                    }
                }
                lab.ExerciseList.Add(e);
            }

            /*using (var connection = ConnectToServer())
             * {
             *  foreach (Exercise e in list)
             *  {
             *      string insert = "USE GEOL100LABS; INSERT IGNORE INTO Exercises(Lab_ID, Exercise_ID, Exercise_Title) VALUES (@lab, @exercise, @title)";
             *      using (var cmd = new MySqlCommand(insert, connection))
             *      {
             *          cmd.Parameters.AddWithValue("@lab", labKey);
             *          cmd.Parameters.AddWithValue("@exercise", e.ExerciseID);
             *          cmd.Parameters.AddWithValue("@title", e.ExerciseTitle);
             *          cmd.ExecuteNonQuery();
             *      }
             *  }
             * }*/
            /* Lab is deleted due to overwritting problems
             *  Another way to solve this problem should eventually be researched
             *  to lighten the load of server calls
             */
            DeleteLab();

            string sql   = "USE GEOL100LABS; INSERT INTO Labs(Lab_Name, Lab_ID, Content, Is_Overriden, Date_Time_Created, Is_Published, Date_Time_Published) VALUES (@name, @id, @content, @overridden, @created, @ispublished, @publish);";
            string myLab = SerializeLab(lab);

            // These 'using' statments are the best practice for iDisposable objects
            using (MySqlConnection connection = ConnectToServer())
            {
                using (MySqlCommand cmd = new MySqlCommand(sql, connection))
                {
                    // This is the parameterized query style I choose
                    cmd.Parameters.AddWithValue("@name", name);
                    cmd.Parameters.AddWithValue("@id", labKey);
                    cmd.Parameters.AddWithValue("@content", myLab);
                    cmd.Parameters.AddWithValue("@overridden", isOverriden);
                    cmd.Parameters.AddWithValue("@created", dtCreated);
                    cmd.Parameters.AddWithValue("@ispublished", isPublished ? "True" : "False");
                    cmd.Parameters.AddWithValue("@publish", pubObj);
                    cmd.ExecuteNonQuery();
                }

                foreach (Exercise e in list)
                {
                    string insert = "USE GEOL100LABS; INSERT IGNORE INTO Exercises(Lab_ID, Exercise_ID, Exercise_Title) VALUES (@lab, @exercise, @title)";
                    using (var cmd = new MySqlCommand(insert, connection))
                    {
                        cmd.Parameters.AddWithValue("@lab", labKey);
                        cmd.Parameters.AddWithValue("@exercise", e.ExerciseID);
                        cmd.Parameters.AddWithValue("@title", e.ExerciseTitle);
                        cmd.ExecuteNonQuery();
                    }
                }
            }
            return(Json("true")); // the content of this return statement is currently irrelevant and can be changed to any string that you want
        }