public ActionResult CreateGame(CreateGameViewModel model) { // Check model validation before doing anything if (!ModelState.IsValid) { PopulateDropdownValues(); return(View(model)); } // Get start and end dates var dates = model.DateRange.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries); var startDateTime = DateTime.Parse(dates[0], CultureInfo.InvariantCulture); var endDateTime = DateTime.Parse(dates[1], CultureInfo.CurrentCulture); // Return error to View if user picks different dates for start and end if (startDateTime.DayOfWeek != endDateTime.DayOfWeek) { ViewData.ModelState.AddModelError("DateRange", "Start date and end date must be same date."); PopulateDropdownValues(); return(View(model)); } // Check if similar game already exists Game existingGame = _gameService.CheckForExistingGame(model.VenueId, model.SportId, startDateTime); if (existingGame != null) { ViewData.ModelState.AddModelError("GameExists", "A game already exists with this venue, sport, and time."); PopulateDropdownValues(); return(View(model)); } // Get venue by ID and business hours for that venue Venue venue = _venueService.GetVenueById(model.VenueId); List <BusinessHours> venueHours = _venueService.GetVenueBusinessHours(model.VenueId); // Return error to View if the venue is not available bool isVenueAvailable = _venueService.IsVenueAvailable(venueHours, startDateTime, endDateTime); if (!isVenueAvailable) { ViewData.ModelState.AddModelError("DateRange", $"Unfortunately, {venue.Name} is not available during the hours you chose."); PopulateDropdownValues(); return(View(model)); } string email = User.Identity.GetUserName(); Contact contact = _contactService.GetContactByEmail(email); // All validation passed so add game to database Game newGame = new Game { ContactId = contact.ContactId, GameStatusId = (int)GameStatusEnum.Open, VenueId = model.VenueId, SportId = model.SportId, StartTime = startDateTime, EndTime = endDateTime }; _gameService.CreateGame(newGame); ViewBag.GameCreated = true; PopulateDropdownValues(); //email content var fileContents = System.IO.File.ReadAllText(Server.MapPath("~/Content/EmailFormat.html")); //add game link to the email var directUrl = Url.Action("GameDetails", "Game", new { id = newGame.GameId }, protocol: Request.Url.Scheme); fileContents = fileContents.Replace("{URL}", directUrl); //replace the html contents to the game details fileContents = fileContents.Replace("{VENUE}", venue.Name); fileContents = fileContents.Replace("{SPORT}", _gameService.GetSportNameById(model.SportId)); fileContents = fileContents.Replace("{STARTTIME}", startDateTime.ToString()); fileContents = fileContents.Replace("{ENDTIME}", endDateTime.ToString()); //send notification to users once a new game created and it includes the user's preference List <SportPreference> checkSportPreference = _contactService.GetAllSportPreferences(); foreach (var item in checkSportPreference) { if (item.SportID == model.SportId && item.ContactID != newGame.ContactId) { fileContents = fileContents.Replace("{URLNAME}", "JOIN"); fileContents = fileContents.Replace("{TITLE}", "New Games Coming!!!"); fileContents = fileContents.Replace("{INFO}", "We have a new game you may interested!"); var subject = "New Game At Rec Nexus"; SendMessage(newGame, item.ContactID, fileContents, subject); } } // time preference List <TimePreference> checkTimePreferences = _contactService.GetAllTimePreferences(); List <Contact> nonDuplicateUser = new List <Contact>(); bool duplicate = false; foreach (var item in checkTimePreferences) { if ((int)newGame.StartTime.DayOfWeek == item.DayOfWeek && newGame.StartTime.TimeOfDay > item.BeginTime && newGame.EndTime.TimeOfDay < item.EndTime && item.ContactID != newGame.ContactId) { foreach (var user in nonDuplicateUser) { if (user.ContactId == item.ContactID) { duplicate = true; } else { duplicate = false; } } if (!duplicate) { nonDuplicateUser.Add(_contactService.GetContactById(item.ContactID)); } foreach (var checkeduser in nonDuplicateUser) { fileContents = fileContents.Replace("{URLNAME}", "JOIN"); fileContents = fileContents.Replace("{TITLE}", "New Games Coming!!!"); fileContents = fileContents.Replace("{INFO}", "We have a new game you may interested!"); var subject = "New Game At Rec Nexus"; SendMessage(newGame, item.ContactID, fileContents, subject); } } } //send notification to the venue owner if (_venueService.VenueHasOwner(venue)) { int ownerId = _venueOwnerService.GetVenueOwnerByVenueId(venue.VenueId).VenueOwnerId; fileContents = fileContents.Replace("{URLNAME}", "CHECK"); fileContents = fileContents.Replace("{TITLE}", "New Game Created on Your Venue!"); fileContents = fileContents.Replace("{INFO}", "There is a new game created on your venue, please check and make sure there is no conflict with the venue schedule."); var subject = "New Game on Your Venue"; SendMessage(newGame, ownerId, fileContents, subject); } return(View()); }
/* * Get venue, hours, and review data for single Venue and return to view */ public ActionResult Details(int id) { // Initializing Owner authentication variable ViewBag.IsOwner = false; // Model to be sent to view VenueViewModel model = new VenueViewModel(); // Map venue details Venue venue = _venueService.GetVenueById(id); model.Address1 = venue.Address1; model.Address2 = venue.Address2; model.City = venue.City; model.Name = venue.Name; model.Phone = venue.Phone; model.State = venue.State; model.VenueId = venue.VenueId; model.ZipCode = venue.ZipCode; //Checking if the user is logged in & and is the owner bool loggedInUser = User.Identity.IsAuthenticated; if (loggedInUser) { string email = User.Identity.GetUserName(); bool isOwner = _venueService.LoggedInUserIsVenueOwner(email, venue); // If logged-in user is the owner, then show link to edit venue if (isOwner) { ViewBag.IsOwner = true; } } // Check if venue owner exists model.HasVenueOwner = _venueService.VenueHasOwner(venue); if (model.HasVenueOwner) { model.VenueOwner = new VenueOwnerViewModel(); var venueOwner = _venueOwnerService.GetVenueOwnerByVenueId(id); model.VenueOwner.FirstName = venueOwner.FirstName; model.VenueOwner.LastName = venueOwner.LastName; model.VenueOwner.VenueOwnerId = venueOwner.VenueOwnerId; } // Map business hours List <BusinessHours> businessHours = _venueService.GetVenueBusinessHours(id); model.BusinessHours = new List <BusinessHoursViewModel>(); foreach (var businessHour in businessHours) { var closeDateTime = new DateTime() + businessHour.CloseTime; var openDateTime = new DateTime() + businessHour.OpenTime; model.BusinessHours.Add(new BusinessHoursViewModel { DayOfWeek = Enum.GetName(typeof(DayOfWeek), businessHour.DayOfWeek), CloseTime = closeDateTime.ToShortTimeString(), OpenTime = openDateTime.ToShortTimeString() }); } // Map reviews List <Review> reviews = _venueService.GetVenueReviews(id); decimal?avgRating; if (reviews.Count > 0) { avgRating = (decimal)reviews.Average(r => r.Rating); model.AverageRating = Math.Round((decimal)avgRating, 1); } else { model.AverageRating = null; } return(View(model)); }