//private static readonly AsyncLock Mutex = new AsyncLock ();
        public async static Task CreateWorkoutTemplateMapping(int workoutTemplateID, ObservableCollection<ExerciseViewModel> simpleExerciseList)
        {
		
			WorkoutTemplateMapping workoutTemplateMapping = new WorkoutTemplateMapping ();
			List<WorkoutTemplateMapping> workoutTemplateMappingInsertList = new List<WorkoutTemplateMapping> ();

			int sequence = 0;
			foreach (ExerciseViewModel simpleExercise in simpleExerciseList) {
				workoutTemplateMapping = new WorkoutTemplateMapping ();
				workoutTemplateMapping.WorkoutTemplateID = workoutTemplateID;
				workoutTemplateMapping.ExerciseID = simpleExercise.Exercise.ExerciseID;
				workoutTemplateMapping.Sets = simpleExercise.Sets;
				workoutTemplateMapping.Reps = simpleExercise.Reps;

				//workoutTemplateMapping.TargetHRMin = simpleExercise.TargetHRMin;
				//workoutTemplateMapping.TargetHRMax = simpleExercise.TargetHRMax;

				workoutTemplateMapping.TargetDurationMin = simpleExercise.Duration;
				//workoutTemplateMapping.CardioExID = simpleExercise.Exercise.CardioExID;

				workoutTemplateMapping.LastUpdatedTime = DateTime.UtcNow;
				workoutTemplateMapping.ExerciseSequence = sequence++;
				workoutTemplateMappingInsertList.Add (workoutTemplateMapping);
			}
			if (workoutTemplateMappingInsertList.Count > 0) {
				
					var db = DependencyService.Get<ISQLite> ().GetAsyncConnection ();
					await db.CreateTableAsync<WorkoutTemplateMapping> ();
					await db.InsertAllAsync (workoutTemplateMappingInsertList);
				
			}
        }
예제 #2
0
		private async static Task<bool> SyncTemplatesWithServer(int gymID, string getServiceType, string dateString)
		{
			bool returnValue = false;
			try
			{
				var workoutTemplateDTOList = await CoachServices.SyncTemplatesWithServer(gymID, dateString);

				if (workoutTemplateDTOList.Count > 0)
				{



					foreach (WorkoutTemplate workoutTemplateDTO in workoutTemplateDTOList)
					{
						DateTime lastTimeUpdateDateTime = DateTime.Parse(workoutTemplateDTO.LastUpdatedTime);

						WorkoutTemplate workoutTemplate = await WorkoutTemplateDAL.GetWorkoutTemplateByServerTemplateID(gymID, workoutTemplateDTO.WorkoutTemplateID);
						if (workoutTemplate == null)
						{
							workoutTemplate = new WorkoutTemplate();
						}
						//add workout template property values
						workoutTemplate.TemplateName = workoutTemplateDTO.TemplateName;
						workoutTemplate.TemplateDescription = workoutTemplateDTO.TemplateDescription;
						workoutTemplate.GymID = workoutTemplateDTO.GymID;
						workoutTemplate.LastUpdatedTimeDT = lastTimeUpdateDateTime;
						workoutTemplate.WorkoutTemplateID = workoutTemplateDTO.WorkoutTemplateID;

						//add or modify in the database
						await WorkoutTemplateDAL.AddModifyTemplate(workoutTemplate);

						//Determine if there are exercises that need to be mapped to the user. If so, map them
						var workoutTemplateExercises = workoutTemplateDTO.WorkoutTemplateExercises;
						if (workoutTemplateExercises != null)
						{
							if (workoutTemplateExercises.Count > 0)
							{
								List<WorkoutTemplateMapping> workoutTemplateMappingList = new List<WorkoutTemplateMapping>();
								foreach (var workoutExercise in workoutTemplateExercises)
								{
									string lastUpdatedTimeString = workoutExercise.LastUpdatedTime;
									DateTime lastUpdatedTime = DateTime.Parse(lastUpdatedTimeString);

									//Create a workouttemplatemapping model object
									WorkoutTemplateMapping workoutTemplateMapping = new WorkoutTemplateMapping();
									workoutTemplateMapping.ExerciseID = workoutExercise.ExerciseID;
									workoutTemplateMapping.Sets = workoutExercise.TargetSets;
									workoutTemplateMapping.Reps = workoutExercise.TargetReps;

									workoutTemplateMapping.TargetDurationMin = workoutExercise.TargetDurationMin;
									workoutTemplateMapping.TargetHRMin = workoutExercise.TargetHRMin;
									workoutTemplateMapping.TargetHRMax = workoutExercise.TargetHRMax;

									workoutTemplateMapping.ExerciseSequence = workoutExercise.ExerciseSequence;
									workoutTemplateMapping.LastUpdatedTime = lastUpdatedTime;
									workoutTemplateMapping.WorkoutTemplateID = workoutTemplate.Id;
									workoutTemplateMappingList.Add(workoutTemplateMapping);

								}
								//Insert created userworkouttemplatemapping model objects into the database
								await WorkoutTemplateMappingDAL.CreateWorkoutTemplateMapping(workoutTemplate.Id, workoutTemplateMappingList);

							}
						}


					}

					returnValue = true;
				}
				else {
					returnValue = false;
				}
			}
			catch (Exception e)
			{
				returnValue = false;
				Insights.Report(e);
				//ErrorLogDAL.AddErrorLogRecord (e, DateTime.UtcNow, gymID, "SyncTemplatesWithServer", "getServiceType : " + getServiceType + ", dateString: " + dateString, e.Message, e.StackTrace);
			}
			if (returnValue)
			{
				//Time stame the request by creating a serversynctracking object and writing to the database
				APISyncTracking serverSyncTrackingRecord = new APISyncTracking();
				serverSyncTrackingRecord.Date = DateTime.UtcNow;
				serverSyncTrackingRecord.DebugInfo = "";
				serverSyncTrackingRecord.GetServiceType = getServiceType;
				ServerSyncTrackingDAL.AddModifyServerSyncTrackingRecord(serverSyncTrackingRecord);
			}
			return returnValue;
		}