예제 #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.WorkoutRoutine.Id == workoutRoutine.Id && !x.ExpirationDate.IsExpired))
                {
                    return(BadRequest("The workout routine is already purchased: " + workoutRoutine.Name));
                }

                athlete.PurchaseWorkoutRoutine(workoutRoutine);

                athleteRepository.SaveChanges();

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