Example #1
0
        public WorkoutSchedule Create(Models.Requests.Workout.WorkoutScheduleCreate createRequest)
        {
            // da li u isto vrijeme postoji vec trening
            Database.Workout workout = _context.Workouts.Find(createRequest.WorkoutId);

            if (workout == null)
            {
                throw new ResourceNotFoundException($"Workout with id {createRequest.WorkoutId} not found");
            }

            // adds 15 minutes after workout in order to prepare for new workout
            TimeSpan workoutEnd = new TimeSpan(
                createRequest.TimeOfTheDay.Hours,
                createRequest.TimeOfTheDay.Minutes + workout.Duration + 15,
                createRequest.TimeOfTheDay.Seconds);

            Database.WorkoutSchedule workoutSchedule = _context.WorkoutSchedules
                                                       .Where(x => x.DayOfTheWeek == createRequest.DayOfTheWeek)
                                                       .Where(x => new TimeSpan(x.TimeOfTheDay.Hours, x.TimeOfTheDay.Minutes + x.Workout.Duration + 15, 0) > createRequest.TimeOfTheDay
                                                              &&
                                                              x.TimeOfTheDay < workoutEnd
                                                              ).FirstOrDefault();

            if (workoutSchedule != null)
            {
                throw new WorkoutScheduleTakenException("This time slot is taken by another workout");
            }

            Database.WorkoutSchedule newWorkoutSchedule = _mapper.Map <Database.WorkoutSchedule>(createRequest);
            _context.WorkoutSchedules.Add(newWorkoutSchedule);
            _context.SaveChanges();

            return(GetById(newWorkoutSchedule.Id));
        }
Example #2
0
        public void Delete(int id)
        {
            Database.WorkoutSchedule dbSchedule = _context.WorkoutSchedules.Find(id);

            if (dbSchedule == null)
            {
                throw new ResourceNotFoundException($"Workout Schedule with id {id} not found");
            }

            _context.WorkoutSchedules.Remove(dbSchedule);
            _context.SaveChanges();
        }
Example #3
0
        public WorkoutSchedule GetById(int Id)
        {
            Database.WorkoutSchedule workoutSchedule = _context.WorkoutSchedules
                                                       .Include(x => x.Workout)
                                                       .Where(x => x.Id == Id)
                                                       .FirstOrDefault();

            if (workoutSchedule == null)
            {
                throw new ResourceNotFoundException($"WorkoutSchedule with id: {Id} not found");
            }

            return(WorkoutScheduleMapper.fromDb(workoutSchedule));
        }