Пример #1
0
        public async Task <WTServiceResponse <List <WorkoutSessionVM> > > GetSessionsForMonth(UserVM user, int year, int month)
        {
            var startDate = new DateTime(year - 2, month, 1);
            var endDate   = new DateTime(year + 2, month, 1);

            var sessionRequest = new WorkoutSessionRequest {
                User = user, StartDate = startDate, EndDate = endDate
            };

            var serializedRequest = JsonConvert.SerializeObject(sessionRequest);

            var httpClient = await _workoutTrackerHttpClient.GetClient();

            var response = await httpClient.PostAsync($"/api/Sessions/user/{sessionRequest.User.ID}/Sessions",
                                                      new StringContent(serializedRequest, System.Text.Encoding.Unicode, "application/json"));


            return(await HandleApiResponse(response, async() =>
            {
                var content = await response.Content.ReadAsStringAsync();
                var sessions = JsonConvert.DeserializeObject <List <WorkoutSessionVM> >(content);

                return new WTServiceResponse <List <WorkoutSessionVM> >
                {
                    StatusCode = response.StatusCode,
                    ViewModel = sessions
                };
            }));
        }
Пример #2
0
        public async Task <WTServiceResponse <string> > UpdateConcreteExerciseAttributes(ConcreteExerciseVM exercise)
        {
            var request = new WorkoutSessionRequest {
                ConcreteExercises = new List <ConcreteExerciseVM>()
            };

            request.ConcreteExercises.Add(exercise);

            // serialize it
            var serializedRequest = JsonConvert.SerializeObject(request);

            var httpClient = await _workoutTrackerHttpClient.GetClient();

            var response = await httpClient.PutAsync($"/api/Sessions/user/{UserId()}/session/{exercise.WorkoutSessionID}/UpdateConcreteExercisesAttributes",
                                                     new StringContent(serializedRequest, System.Text.Encoding.Unicode, "application/json"));

            return(HandleApiResponse(response, () =>
            {
                return new WTServiceResponse <string>
                {
                    StatusCode = response.StatusCode,
                    ViewModel = "",
                };
            }));
        }
Пример #3
0
        public async Task <IActionResult> SaveRoutinesForSession([FromForm] WorkoutSessionRequest workoutSessionRequest)
        {
            workoutSessionRequest.Routines = new List <WorkoutRoutineVM>();
            var routineIDs = JsonConvert.DeserializeObject <List <int> >(workoutSessionRequest.SerializedRoutineIds);

            routineIDs.ForEach(item => workoutSessionRequest.Routines.Add(new WorkoutRoutineVM {
                ID = item
            }));

            var response = await _workoutTrackerService.SaveRoutinesForSession(workoutSessionRequest);

            return(Json(response));
        }
Пример #4
0
        public async Task <IActionResult> Index(string queryDate)
        {
            await WriteOutIdentityInformation();

            var currentDate = DateTime.Now;

            if (queryDate != null)
            {
                DateTime parsedDate;
                if (DateTime.TryParse(queryDate, out parsedDate))
                {
                    currentDate = parsedDate;
                }
            }
            var UserVM = new UserVM {
                ID = WorkotTrackerHelper.GetUserId(User)
            };
            var sessionRequest = new WorkoutSessionRequest {
                User = UserVM, CurrentDate = currentDate
            };

            var response = await _workoutTrackerService.GetSessionForDay(sessionRequest);

            //set image path relative to api's URL ...
            response.ViewModel.ConcreteExercises.ToList().ForEach(item => item.ImagePath = WorkotTrackerHelper.ApiUrl + item.ImagePath);

            //Get exercises for the user
            var exercisesResposne = await _workoutTrackerService.GetExercisesForUser(UserVM);

            //set image path relative to api's URL ...
            exercisesResposne.ViewModel.ForEach(item => item.ImagePath = WorkotTrackerHelper.ApiUrl + item.ImagePath);
            ViewData["Exercises"] = exercisesResposne.ViewModel;

            //Get routines for the user
            var routineResponse = await _workoutTrackerService.GetRoutinesForUser(UserVM);

            //set image path relative to api's URL ...
            routineResponse.ViewModel.ForEach(item => item.ImagePath = WorkotTrackerHelper.ApiUrl + item.ImagePath);
            routineResponse.ViewModel.ForEach(item => item.Exercises.ToList()
                                              .ForEach(img => img.ImagePath = WorkotTrackerHelper.ApiUrl + img.ImagePath));
            ViewData["Routines"] = routineResponse.ViewModel;

            return(View(response.ViewModel));
        }
Пример #5
0
        public async Task <WTServiceResponse <string> > SaveRoutinesForSession(WorkoutSessionRequest workoutSessionRequest)
        {
            // serialize it
            var serializedRequest = JsonConvert.SerializeObject(workoutSessionRequest);

            var httpClient = await _workoutTrackerHttpClient.GetClient();

            var response = await httpClient.PostAsync($"/api/Sessions/user/{UserId()}/CreateOrUpdate",
                                                      new StringContent(serializedRequest, System.Text.Encoding.Unicode, "application/json"));

            return(HandleApiResponse(response, () =>
            {
                return new WTServiceResponse <string>
                {
                    StatusCode = response.StatusCode,
                    ViewModel = "",
                };
            }));
        }
Пример #6
0
        public async Task <IActionResult> GetSessionForDay([FromRoute] int?userId, [FromBody] WorkoutSessionRequest sessionRequest)
        {
            if (userId == null)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid || sessionRequest.CurrentDate.HasValue == false)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            var session = await _repository.GetSessionForDay(userId, sessionRequest.CurrentDate.Value);

            if (session == null)
            {
                return(NotFound("Session not found"));
            }

            var mappedSession = Mapper.Map <WorkoutSessionDTO>(session);

            return(Ok(mappedSession));
        }
Пример #7
0
        public async Task <IActionResult> GetSessions([FromRoute] int?userId, [FromBody] WorkoutSessionRequest sessionRequest)
        {
            if (userId == null)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            var sessions = await _repository.GetSessionsForUser(userId, sessionRequest.StartDate, sessionRequest.EndDate);

            if (sessions == null)
            {
                return(NotFound("Sessions not found"));
            }

            var mappedSessions = Mapper.Map <IEnumerable <WorkoutSessionDTO> >(sessions);

            return(Ok(mappedSessions));
        }
Пример #8
0
        public async Task <WTServiceResponse <WorkoutSessionVM> > GetSessionForDay(WorkoutSessionRequest workoutSessionRequest)
        {
            var serializedRequest = JsonConvert.SerializeObject(workoutSessionRequest);

            var httpClient = await _workoutTrackerHttpClient.GetClient();

            var response = await httpClient.PostAsync($"/api/Sessions/user/{workoutSessionRequest.User.ID}/SessionForDay",
                                                      new StringContent(serializedRequest, System.Text.Encoding.Unicode, "application/json"));


            return(await HandleApiResponse(response, async() =>
            {
                var content = await response.Content.ReadAsStringAsync();
                var session = JsonConvert.DeserializeObject <WorkoutSessionVM>(content);

                return new WTServiceResponse <WorkoutSessionVM>
                {
                    StatusCode = response.StatusCode,
                    ViewModel = session
                };
            }));
        }
Пример #9
0
        public async Task <IActionResult> CreateOrUpdateSession([FromRoute] int?userId, [FromBody] WorkoutSessionRequest sessionRequest)
        {
            if (userId == null)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid || sessionRequest.CurrentDate.HasValue == false)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            var routines          = Mapper.Map <List <WorkoutRoutine> >(sessionRequest.Routines);
            var exercises         = Mapper.Map <List <Exercise> >(sessionRequest.Exercises);
            var concreteExercises = Mapper.Map <List <ConcreteExercise> >(sessionRequest.ConcreteExercises);

            var session = await _repository.AddOrUpdateSession(userId,
                                                               sessionRequest.CurrentDate.Value,
                                                               routines,
                                                               exercises,
                                                               concreteExercises);

            if (session == null)
            {
                return(BadRequest("Add or udpate Failed for Session..."));
            }

            var sessionToReturn = Mapper.Map <WorkoutSessionDTO>(session);

            return(CreatedAtRoute(
                       routeName: "GetSession",
                       routeValues: new
            {
                userId = userId,
                sessionId = sessionToReturn.ID
            },
                       value: sessionToReturn));
        }
Пример #10
0
        public async Task <IActionResult> UpdateConcreteExercisesAttributes([FromRoute] int?userId, [FromRoute] int?sessionId, [FromBody] WorkoutSessionRequest sessionRequest)
        {
            if (userId == null || sessionId == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            var concreteExercises = Mapper.Map <List <ConcreteExercise> >(sessionRequest.ConcreteExercises);

            var result = await _repository.UpdateConcreteExercisesAttributes(userId, sessionId, concreteExercises);

            if (result == false)
            {
                return(BadRequest("Update failed for concrete exercises attributes ..."));
            }

            return(NoContent());
        }