Esempio n. 1
0
        public async Task <IActionResult> Leave(int id)
        {
            Journey journey = await journeyService.GetByIdAsync(id);

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

            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 is part of the journey
            {
                TempData.AddErrorMessage(WebConstants.NotPartOfTheJourney);
                return(RedirectToHome());
            }

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

            this.SendEmailToDriver(journey, EmailConstants.LeaveSubject, EmailConstants.LeaveBody);

            TempData.AddWarningMessage(WebConstants.WarningLeaveJourney);
            return(RedirectToHome());
        }