public async Task<int> NewGameHost2(NewGameHostWebModel2 model) { var me = new UserProfileLogic(db).GetAthleteForUserName(this.User.Identity.Name); var venue = db.Venues.SingleOrDefault(i => i.VenueID == model.VenueID); List<Athlete> playersToInvite = new List<Athlete>(); if (model.Invitees != null) { var ids = model.Invitees.Distinct().ToList(); if (ids.Count > 100) throw new ApplicationException("Too many athletes are being invited."); foreach (var id in ids) playersToInvite.Add(db.Athletes.Single(i => i.AthleteID == id)); } if ((model.When - DateTime.UtcNow).TotalHours < -1) throw new Exception("when is in the past"); // create the gamehost GameHost gameHost = new GameHost() { AthleteID = me.AthleteID, TimeCreated = DateTime.UtcNow, VenueID = model.VenueID, Visibility = 0,//model.Visibility, When = model.When, When_InLocalTimeZone = model.When_InLocalTimeZone, LimitOnNumberOfPlayers = model.LimitOnNumberOfPlayers, EventType = (int)model.EventType, }; db.GameHosts.Add(gameHost); // create the invites foreach (var athlete in playersToInvite) { GameHostInvite invite = new GameHostInvite() { AthleteID = athlete.AthleteID, GameHost = gameHost, IsApprovedByHost = true, TimeCreated = DateTime.UtcNow }; db.GameHostInvites.Add(invite); } db.SaveChanges(); // add the comment if (string.IsNullOrEmpty(model.Comments) == false) { await new FeedLogic(db).MakeComment(me.AthleteID, NewsfeedItemTypeEnum.GameHost, gameHost.GameHostID, model.Comments, false); } // send the invites foreach (var athlete in playersToInvite) { await sendAnInvite(me, athlete, gameHost, venue, model.Comments); } return gameHost.GameHostID; }
public async Task<int> NewGameHostInvite(NewGameHostInviteWebModel model) { int gameHostID = model.GameHostID; int athleteID = model.AthleteID; var me = new UserProfileLogic(db).GetAthleteForUserName(this.User.Identity.Name); var athlete = db.Athletes.Single(i => i.AthleteID == athleteID); if (athlete == null || me.AthleteID == athleteID) throw new Exception("athleteID is invalid"); GameHost gameHost = db.GameHosts.Include("Venue").Single(i => i.GameHostID == gameHostID); if (gameHost.GameHostInvites.Where(i => i.AthleteID == athleteID).Count() > 0) throw new Exception("an invite for this athlete already exists"); GameHostInvite invite = new GameHostInvite() { AthleteID = athleteID, GameHost = gameHost, IsApprovedByHost = true, TimeCreated = DateTime.UtcNow }; db.GameHostInvites.Add(invite); db.SaveChanges(); await sendAnInvite(me, athlete, gameHost, gameHost.Venue, ""); return invite.GameHostInviteID; }
public async Task<bool> DeclineInvitation(int gameHostID) { var me = new UserProfileLogic(db).GetAthleteForUserName(this.User.Identity.Name); GameHost gameHost = db.GameHosts.Include("Athlete").Include("Venue").Where(i => i.GameHostID == gameHostID).Single(); GameHostInvite invite = gameHost.GameHostInvites.Where(i => i.AthleteID == me.AthleteID).First(); invite.IsApprovedByInvitee = false; invite.IsDeniedByInvitee = true; db.SaveChanges(); await this.sendInviteDeclined(me, gameHost.Athlete, gameHost, gameHost.Venue); return true; }
public async Task<int> AskToJoinGameHost(int gameHostID) { var me = new UserProfileLogic(db).GetAthleteForUserName(this.User.Identity.Name); GameHost gameHost = db.GameHosts.Single(i => i.GameHostID == gameHostID); if (gameHost.AthleteID == me.AthleteID) throw new Exception("Cannot ask yourself"); GameHostInvite invite = gameHost.GameHostInvites.Where(i => i.AthleteID == me.AthleteID).FirstOrDefault(); if (invite != null && invite.IsApprovedByHost == true) { // already invited by the host - mark as approved by invitee invite.IsApprovedByInvitee = true; invite.IsDeniedByInvitee = false; db.SaveChanges(); await this.sendInviteAccepted(me, gameHost.Athlete, gameHost, gameHost.Venue); return invite.GameHostInviteID; } else if (invite != null && invite.IsApprovedByHost == false) { // the invitee already invited himself, the host hasn't accepted yet return invite.GameHostInviteID; } else { // create an invitation object invite = new GameHostInvite() { AthleteID = me.AthleteID, GameHost = gameHost, IsApprovedByHost = false, IsApprovedByInvitee = true, TimeCreated = DateTime.UtcNow }; db.GameHostInvites.Add(invite); db.SaveChanges(); // email await this.emailWantsToPlay(me, gameHost.Athlete, gameHost, gameHost.Venue); return invite.GameHostInviteID; } }
public async Task<bool> ApproveInvitee(int gameHostID, int inviteeID) { var me = new UserProfileLogic(db).GetAthleteForUserName(this.User.Identity.Name); GameHost gameHost = db.GameHosts.Include("Athlete").Include("Venue").Single(i => i.GameHostID == gameHostID); if (gameHost.AthleteID != me.AthleteID) throw new Exception("Not your game host!"); GameHostInvite invite = gameHost.GameHostInvites.Where(i => i.AthleteID == inviteeID).FirstOrDefault(); if (invite == null) throw new Exception("Not found"); if (invite.IsApprovedByInvitee == false || invite.IsDeniedByInvitee == true) throw new Exception("IsApprovedByInvitee == false"); Athlete athleteInvitee = db.Athletes.Where(i => i.AthleteID == invite.AthleteID).Single(); invite.IsApprovedByHost = true; db.SaveChanges(); await this.sendHostApproves(me, athleteInvitee, gameHost, gameHost.Venue); return true; }