public async Task AddAsync(RecurringRides recurringRides, CancellationToken cancellationToken = default)
 => await _dbContext.Set <RecurringRides>().AddAsync(recurringRides, cancellationToken);
 public void Delete(RecurringRides recurringRides)
 => _dbContext.Set <RecurringRides>().Remove(recurringRides);
        public async Task <RecurringRideId> Handle(AddRecurringRideCommand request,
                                                   CancellationToken cancellationToken)
        {
            var weekDays  = WeekDay.GetDays(request.WeekDays);
            var startDate = request.StartDate;

            var dates = Enumerable
                        .Range(0, int.MaxValue)
                        .Where(index => weekDays.Contains(startDate.AddDays(index).DayOfWeek))
                        .Select(index => startDate.AddDays(index))
                        .TakeWhile(date => date <= request.EndDate)
                        .ToList();

            IdGenerator rideIdGenerator = new(IdGeneratorType.Ride);

            IdGenerator recurringRideIdGenerator = new(IdGeneratorType.RecurringRide);
            var         recurringRideId          = new RecurringRideId(recurringRideIdGenerator.CreateId());

            var recurringRides = new RecurringRides(recurringRideId);

            var ids = rideIdGenerator.Take(dates.Count + 1);

            using var rideIdEnumerator = ids.GetEnumerator();

            foreach (var date in dates)
            {
                try
                {
                    rideIdEnumerator.MoveNext();

                    DateTimeOffset dateTime = new DateTimeOffset(date.Year,
                                                                 date.Month,
                                                                 date.Day,
                                                                 request.RideTime.Hours,
                                                                 request.RideTime.Minutes,
                                                                 0,
                                                                 date.Offset);

                    // var dateTime = new DateTimeOffset(date.Year, date.Month, date.Day, request.RideTime.Hours,
                    //  request.RideTime.Minutes, 0);

                    if (request.Location == null)
                    {
                        throw new ApiException("Ride location cannot be empty", StatusCodes.Status400BadRequest);
                    }

                    var ride = new Ride(new RideId(rideIdEnumerator.Current),
                                        request.OwnerId,
                                        request.GroupId,
                                        dateTime,
                                        request.Price,
                                        new Location(request.Location.Longitude, request.Location.Latitude),
                                        request.RideDirection,
                                        request.Stops?.Select(x => new Stop(x.ParticipantId,
                                                                            new Location(x.Location.Longitude, x.Location.Latitude),
                                                                            new RideId(rideIdEnumerator.Current)))
                                        .ToList() ?? new List <Stop>(),
                                        request.SeatsLimit,
                                        new List <RideRequest>(),
                                        recurringRideId);

                    recurringRides.AddRide(ride);
                }
                catch (Exception ex)
                {
                    throw new ApiException(ex.InnerException);
                }
            }

            try
            {
                await _recurringRidesRepository.AddAsync(recurringRides, cancellationToken);

                await _unitOfWork.SaveAsync(cancellationToken);
            }
            catch (DbUpdateException ex)
            {
                throw new ApiException(ex.InnerException);
            }

            return(recurringRides.Id);
        }