コード例 #1
0
        public async Task <IActionResult> Join(int id)
        {
            Journey journey = await journeyService.GetByIdAsync(id);

            if (journey == null)
            {
                TempData.AddErrorMessage(WebConstants.ErrorTryAgain);
                return(NotFound());
            }

            if (journey.Passengers.Count() == journey.Seats) // Check if there are available seats
            {
                TempData.AddErrorMessage(WebConstants.FullCar);
                return(RedirectToHome());
            }

            if (journey.SetOffTime < DateTime.UtcNow)
            {
                TempData.AddErrorMessage(WebConstants.JourneyInThePast);
                return(RedirectToHome());
            }

            int currentUserId = GetCurrentUserAsync().Result.Id;

            if (journey.Passengers.Any(p => p.UserId == currentUserId)) // Check if the user isn't already part of the journey
            {
                TempData.AddErrorMessage(WebConstants.AlreadyJoined);
                return(RedirectToHome());
            }

            if (journey.DriverId == currentUserId) // Check if the user isn't the driver of the journey
            {
                TempData.AddErrorMessage(WebConstants.ErrorTryAgain);
                return(RedirectToHome());
            }

            journeyService.JoinJourney(journey, currentUserId);
            await service.SaveAsync();

            this.SendEmailToDriver(journey, EmailConstants.JoinSubject, EmailConstants.JoinBody);

            TempData.AddSuccessMessage(WebConstants.SuccessfulJoin);
            return(RedirectToHome());
        }