예제 #1
0
        public async Task <IEnumerable <T> > ShowSearchResultsAsync <T>(TripSearchInputModel inputModel, string userId, int take, int skip = 0)
        {
            var townsDistanceId = await this.GetOriginAndDestination(inputModel.Origin, inputModel.Destination);

            if (townsDistanceId == null)
            {
                return(null);
            }

            var origin      = (Town)Enum.Parse(typeof(Town), inputModel.Origin.Replace(" ", string.Empty));
            var destination = (Town)Enum.Parse(typeof(Town), inputModel.Destination.Replace(" ", string.Empty));

            var trips = await this.tripsRepository
                        .All()
                        .Include(t => t.Driver)
                        .ThenInclude(d => d.AvatarImage)
                        .Include(t => t.Car)
                        .Include(t => t.TownsDistance)
                        .Where(t => t.UserTrips.All(ut => ut.UserId != userId) &&
                               t.Origin == origin &&
                               t.Destination == destination &&
                               t.DateOfDeparture.Date.CompareTo(inputModel.DateOfDeparture) == 0 &&
                               t.FreeSeats >= inputModel.SeatsNeeded)
                        .OrderBy(t => t.DateOfDeparture)
                        .Skip(skip)
                        .Take(take)
                        .To <T>()
                        .ToListAsync();

            return(trips);
        }
예제 #2
0
        public IActionResult Get([FromQuery] TripSearchInputModel search)
        {
            var filters = this.GetFilters(search.CityId, search.CragId, search.Date, search.Seats);
            var orderBy = this.GetOrderBy(search.OrderBy);

            var response = new Response()
            {
                Data = this.tripsService.GetManyExtended <TripViewModel>(filters, orderBy, search.Ascending, search.Skip, search.Take),
            };

            return(this.Ok(response));
        }
예제 #3
0
        public async Task <int> GetSearchResultsCountAsync(TripSearchInputModel inputModel, string userId)
        {
            var origin      = (Town)Enum.Parse(typeof(Town), inputModel.Origin.Replace(" ", string.Empty));
            var destination = (Town)Enum.Parse(typeof(Town), inputModel.Destination.Replace(" ", string.Empty));

            var tripsCount = await this.tripsRepository
                             .All()
                             .Include(t => t.Driver)
                             .ThenInclude(d => d.AvatarImage)
                             .Include(t => t.Car)
                             .Include(t => t.TownsDistance)
                             .Where(t => t.UserTrips.All(ut => ut.UserId != userId) &&
                                    t.Origin == origin &&
                                    t.Destination == destination &&
                                    t.DateOfDeparture.Date.CompareTo(inputModel.DateOfDeparture) == 0 &&
                                    t.FreeSeats >= inputModel.SeatsNeeded)
                             .CountAsync();

            return(tripsCount);
        }
예제 #4
0
        public async Task <IActionResult> Search(TripSearchInputModel inputModel, int page = 1)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View());
            }

            var userId = this.userManager.GetUserId(this.User);

            var tripsViewModel = await this.tripsService
                                 .ShowSearchResultsAsync <TripViewModel>(inputModel, userId, TripsPerPage, (page - 1) *TripsPerPage);

            int searchResultsCount = 0;

            if (tripsViewModel != null)
            {
                foreach (var trip in tripsViewModel)
                {
                    trip.DriverAvatarImageUrl = trip.DriverAvatarImageUrl == null
                    ? "/img/avatar.png"
                    : this.imagePathPrefix + this.driverImageSizing + trip.DriverAvatarImageUrl;
                }

                searchResultsCount = await this.tripsService.GetSearchResultsCountAsync(inputModel, userId);
            }

            var tripsSearchViewModel = new TripsViewModel
            {
                Title       = "Search results:",
                CurrentPage = page,
                PagesCount  = (int)Math.Ceiling((double)(searchResultsCount / TripsPerPage)),
                Trips       = tripsViewModel,
            };

            if (tripsSearchViewModel.PagesCount == 0)
            {
                tripsSearchViewModel.PagesCount = 1;
            }

            return(this.View("All", tripsSearchViewModel));
        }
예제 #5
0
 public async Task <IActionResult> Index(TripSearchInputModel model)
 {
     model.DateOfDeparture         = DateTime.Now;
     this.ViewData["Destinations"] = SelectListGenerator.GetAllDestinations(this.destinationsService);
     return(this.View());
 }