Esempio n. 1
0
        public IActionResult PurchaseWorkoutRoutine(long id, [FromBody] long workoutRoutineId)
        {
            try
            {
                WorkoutRoutine workoutRoutine = workoutRoutineRepository.GetById(workoutRoutineId);
                if (workoutRoutine == null)
                {
                    return(BadRequest("Invalid workout routine Id: " + workoutRoutineId));
                }

                Athlete athlete = athleteRepository.GetById(id);
                if (athlete == null)
                {
                    return(BadRequest("Invalid athlete Id: " + id));
                }

                if (athlete.PurchasedWorkoutRoutine.Any(x => x.WorkoutRoutineId == workoutRoutine.Id && (x.ExpirationDate == null || x.ExpirationDate.Value >= DateTime.UtcNow)))
                {
                    return(BadRequest("The workout routine is already purchased: " + workoutRoutine.Name));
                }

                athleteService.PurchaseWorkoutRoutine(athlete, workoutRoutine);

                athleteRepository.SaveChanges();

                return(Ok());
            }
            catch (Exception e)
            {
                return(StatusCode(500, new { error = e.Message }));
            }
        }