public async Task <IActionResult> Join(IFormCollection request) { int pilotId = request._str("pilot_id") == "" ? Request.Cookies.PreferredPilotId() : request._int("pilot_id"); List <int> roleIds = request._str("role_ids") != "" ? request._str("role_ids").Split(',').Select(item => int.Parse(item)).ToList() : new List <int>(); List <int> fitIds = request._str("fit_ids").Split(',').Select(item => int.Parse(item)).ToList(); Pilot pilot = await _Db.Pilots.FindAsync(pilotId); if (pilot == null) { return(NotFound("Pilot not found")); } if (fitIds.Count == 0) { return(BadRequest("You must select a fit before you can join the waitlist")); } try { WaitingPilot waitlist = new WaitingPilot { PilotId = pilot.CharacterID, SelectedFits = null, SelectedRoles = null, RemovedByAccountId = null, NewPilot = false, CreatedAt = DateTime.UtcNow, UpdatedAt = DateTime.UtcNow }; await _Db.AddAsync(waitlist); foreach (int id in fitIds) { await _Db.AddAsync(new SelectedFit { FitId = id, WaitingPilotId = waitlist.Id }); } // Add Roles foreach (int id in roleIds) { await _Db.AddAsync(new SelectedRole { FleetRoleId = id, WaitingPilotId = waitlist.Id }); } await _Db.SaveChangesAsync(); return(Ok()); } catch (Exception ex) { _Logger.LogWarning("{0} could not be added to the waitlist: {1}", pilot.CharacterName, ex.Message); return(BadRequest($"Could not add {pilot.CharacterName} to the waitlist {ex.Message}")); } }
private async Task <int?> CheckWaitlistForPilot(int characterId, Fleet fleet) { WaitingPilot x = await _Db.WaitingPilots.Where(c => c.PilotId == characterId && c.RemovedByAccountId == null).FirstOrDefaultAsync(); if (x == null) { Pilot pilot = await _Db.Pilots.FindAsync(characterId); if (pilot == null) { // We don't know who this pilot is, let's look them up with ESI and create some records for them var pilotInfo = await EsiWrapper.PilotLookupAsync(characterId); Corporation.EnsureInDatabase(pilotInfo.CorporationId, _Db); await _Db.AddAsync(new Pilot { Account = null, CharacterID = characterId, CharacterName = pilotInfo.Name, CorporationID = pilotInfo.CorporationId, RegisteredAt = DateTime.UtcNow, UpdatedAt = DateTime.UtcNow }); } // Create a waiting pilot! WaitingPilot waitlistPilot = new WaitingPilot { RemovedByAccountId = fleet.BossPilot.AccountId, PilotId = characterId, FleetAssignment = null, SystemId = null, IsOffline = false, CreatedAt = DateTime.UtcNow, UpdatedAt = DateTime.UtcNow }; await _Db.AddAsync(waitlistPilot); await _Db.SaveChangesAsync(); //Return that waiting pilot return(waitlistPilot.Id); } x.RemovedByAccountId = fleet.BossPilot.AccountId; x.UpdatedAt = DateTime.UtcNow; return(x.Id); }
public async Task <IActionResult> Remove(int id) { WaitingPilot pilot = await _Db.WaitingPilots.FindAsync(id); if (pilot == null) { return(NotFound("The pilot has already either been removed, or invited.")); } pilot.RemovedByAccountId = User.AccountId(); pilot.UpdatedAt = DateTime.UtcNow; await _Db.SaveChangesAsync(); return(Ok()); }