From d46432919340e4cac8f4d2d3d0061372ca5f39b1 Mon Sep 17 00:00:00 2001 From: LumaLivy <7350336+LumaLivy@users.noreply.github.com> Date: Wed, 17 Nov 2021 20:50:44 -0500 Subject: [PATCH] Fix a bunch of warnings --- .gitignore | 2 ++ .../Controllers/EnterLevelController.cs | 6 ++++-- .../Controllers/ListController.cs | 12 ++++++------ .../Controllers/PhotosController.cs | 4 ++-- .../Controllers/ScoreController.cs | 6 +++--- .../Controllers/SlotsController.cs | 3 ++- .../Controllers/UserController.cs | 19 ++++++++++--------- 7 files changed, 29 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 1c626784..1f63e982 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,5 @@ riderModule.iml /ProjectLighthouse/logs/* /ProjectLighthouse/ProjectLighthouse.csproj.user .vs/ +.vscode/ +.editorconfig \ No newline at end of file diff --git a/ProjectLighthouse/Controllers/EnterLevelController.cs b/ProjectLighthouse/Controllers/EnterLevelController.cs index 1e409bd3..cebca719 100644 --- a/ProjectLighthouse/Controllers/EnterLevelController.cs +++ b/ProjectLighthouse/Controllers/EnterLevelController.cs @@ -35,7 +35,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers GameVersion gameVersion = token.GameVersion; IQueryable visited = this.database.VisitedLevels.Where(s => s.SlotId == slotId && s.UserId == user.UserId); - VisitedLevel v; + VisitedLevel? v; if (!visited.Any()) { switch (gameVersion) @@ -61,6 +61,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers { v = await visited.FirstOrDefaultAsync(); } + if (v == null) return this.StatusCode(403, ""); switch (gameVersion) { @@ -95,7 +96,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers if (slot == null) return this.NotFound(); IQueryable visited = this.database.VisitedLevels.Where(s => s.SlotId == id && s.UserId == user.UserId); - VisitedLevel v; + VisitedLevel? v; if (!visited.Any()) { slot.PlaysLBP1Unique++; @@ -109,6 +110,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers { v = await visited.FirstOrDefaultAsync(); } + if (v == null) return StatusCode(403, ""); slot.PlaysLBP1++; v.PlaysLBP1++; diff --git a/ProjectLighthouse/Controllers/ListController.cs b/ProjectLighthouse/Controllers/ListController.cs index fffda451..71ab40e1 100644 --- a/ProjectLighthouse/Controllers/ListController.cs +++ b/ProjectLighthouse/Controllers/ListController.cs @@ -55,7 +55,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers User? user = await this.database.UserFromRequest(this.Request); if (user == null) return this.StatusCode(403, ""); - QueuedLevel queuedLevel = await this.database.QueuedLevels.FirstOrDefaultAsync(q => q.UserId == user.UserId && q.SlotId == id); + QueuedLevel? queuedLevel = await this.database.QueuedLevels.FirstOrDefaultAsync(q => q.UserId == user.UserId && q.SlotId == id); if (queuedLevel != null) return this.Ok(); this.database.QueuedLevels.Add @@ -78,7 +78,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers User? user = await this.database.UserFromRequest(this.Request); if (user == null) return this.StatusCode(403, ""); - QueuedLevel queuedLevel = await this.database.QueuedLevels.FirstOrDefaultAsync(q => q.UserId == user.UserId && q.SlotId == id); + QueuedLevel? queuedLevel = await this.database.QueuedLevels.FirstOrDefaultAsync(q => q.UserId == user.UserId && q.SlotId == id); if (queuedLevel != null) this.database.QueuedLevels.Remove(queuedLevel); await this.database.SaveChangesAsync(); @@ -119,7 +119,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers User? user = await this.database.UserFromRequest(this.Request); if (user == null) return this.StatusCode(403, ""); - HeartedLevel heartedLevel = await this.database.HeartedLevels.FirstOrDefaultAsync(q => q.UserId == user.UserId && q.SlotId == id); + HeartedLevel? heartedLevel = await this.database.HeartedLevels.FirstOrDefaultAsync(q => q.UserId == user.UserId && q.SlotId == id); if (heartedLevel != null) return this.Ok(); this.database.HeartedLevels.Add @@ -142,7 +142,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers User? user = await this.database.UserFromRequest(this.Request); if (user == null) return this.StatusCode(403, ""); - HeartedLevel heartedLevel = await this.database.HeartedLevels.FirstOrDefaultAsync(q => q.UserId == user.UserId && q.SlotId == id); + HeartedLevel? heartedLevel = await this.database.HeartedLevels.FirstOrDefaultAsync(q => q.UserId == user.UserId && q.SlotId == id); if (heartedLevel != null) this.database.HeartedLevels.Remove(heartedLevel); await this.database.SaveChangesAsync(); @@ -185,7 +185,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers User? heartedUser = await this.database.Users.FirstOrDefaultAsync(u => u.Username == username); if (heartedUser == null) return this.NotFound(); - HeartedProfile heartedProfile = await this.database.HeartedProfiles.FirstOrDefaultAsync + HeartedProfile? heartedProfile = await this.database.HeartedProfiles.FirstOrDefaultAsync (q => q.UserId == user.UserId && q.HeartedUserId == heartedUser.UserId); if (heartedProfile != null) return this.Ok(); @@ -212,7 +212,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers User? heartedUser = await this.database.Users.FirstOrDefaultAsync(u => u.Username == username); if (heartedUser == null) return this.NotFound(); - HeartedProfile heartedProfile = await this.database.HeartedProfiles.FirstOrDefaultAsync + HeartedProfile? heartedProfile = await this.database.HeartedProfiles.FirstOrDefaultAsync (q => q.UserId == user.UserId && q.HeartedUserId == heartedUser.UserId); if (heartedProfile != null) this.database.HeartedProfiles.Remove(heartedProfile); diff --git a/ProjectLighthouse/Controllers/PhotosController.cs b/ProjectLighthouse/Controllers/PhotosController.cs index c853aedf..3f224432 100644 --- a/ProjectLighthouse/Controllers/PhotosController.cs +++ b/ProjectLighthouse/Controllers/PhotosController.cs @@ -82,7 +82,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers { User? userFromQuery = await this.database.Users.FirstOrDefaultAsync(u => u.Username == user); // ReSharper disable once ConditionIsAlwaysTrueOrFalse - if (user == null) return this.NotFound(); + if (user == null || userFromQuery == null) return this.NotFound(); List photos = await this.database.Photos.Include (p => p.Creator) @@ -100,7 +100,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers { User? userFromQuery = await this.database.Users.FirstOrDefaultAsync(u => u.Username == user); // ReSharper disable once ConditionIsAlwaysTrueOrFalse - if (user == null) return this.NotFound(); + if (user == null || userFromQuery == null) return this.NotFound(); List photos = new(); foreach (Photo photo in this.database.Photos.Include(p => p.Creator)) diff --git a/ProjectLighthouse/Controllers/ScoreController.cs b/ProjectLighthouse/Controllers/ScoreController.cs index 05019a1f..283dc0b9 100644 --- a/ProjectLighthouse/Controllers/ScoreController.cs +++ b/ProjectLighthouse/Controllers/ScoreController.cs @@ -80,7 +80,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers await this.database.SaveChangesAsync(); - string myRanking = await GetScores(score.SlotId, score.Type, user); + string myRanking = GetScores(score.SlotId, score.Type, user); return this.Ok(myRanking); } @@ -99,10 +99,10 @@ namespace LBPUnion.ProjectLighthouse.Controllers if (user == null) return this.StatusCode(403, ""); - return this.Ok(await GetScores(slotId, type, user, pageStart, pageSize)); + return this.Ok(GetScores(slotId, type, user, pageStart, pageSize)); } - public async Task GetScores(int slotId, int type, User user, int pageStart = -1, int pageSize = 5) + public string GetScores(int slotId, int type, User user, int pageStart = -1, int pageSize = 5) { // This is hella ugly but it technically assigns the proper rank to a score // var needed for Anonymous type returned from SELECT diff --git a/ProjectLighthouse/Controllers/SlotsController.cs b/ProjectLighthouse/Controllers/SlotsController.cs index 5b536306..802bb0c5 100644 --- a/ProjectLighthouse/Controllers/SlotsController.cs +++ b/ProjectLighthouse/Controllers/SlotsController.cs @@ -31,7 +31,8 @@ namespace LBPUnion.ProjectLighthouse.Controllers GameVersion gameVersion = token.GameVersion; - User user = await this.database.Users.FirstOrDefaultAsync(dbUser => dbUser.Username == u); + User? user = await this.database.Users.FirstOrDefaultAsync(dbUser => dbUser.Username == u); + if (user == null) return StatusCode(403, ""); string response = Enumerable.Aggregate ( diff --git a/ProjectLighthouse/Controllers/UserController.cs b/ProjectLighthouse/Controllers/UserController.cs index d3f07198..2fd3efe2 100644 --- a/ProjectLighthouse/Controllers/UserController.cs +++ b/ProjectLighthouse/Controllers/UserController.cs @@ -30,7 +30,8 @@ namespace LBPUnion.ProjectLighthouse.Controllers { User? user = await this.database.Users.Include(u => u.Location).FirstOrDefaultAsync(u => u.Username == username); - return user?.Serialize(gameVersion); + if (user == null) return ""; + return user.Serialize(gameVersion); } [HttpGet("user/{username}")] @@ -40,7 +41,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers if (token == null) return this.StatusCode(403, ""); string? user = await this.GetSerializedUser(username, token.GameVersion); - if (user == null) return this.NotFound(); + if (user == "") return this.NotFound(); return this.Ok(user); } @@ -54,7 +55,8 @@ namespace LBPUnion.ProjectLighthouse.Controllers List serializedUsers = new(); foreach (string userId in u) { - serializedUsers.Add(await this.GetSerializedUser(userId, token.GameVersion)); + string? serializedUser = await this.GetSerializedUser(userId, token.GameVersion); + if (serializedUser != "") serializedUsers.Add(serializedUser); } string serialized = serializedUsers.Aggregate(string.Empty, (current, u) => u == null ? current : current + u); @@ -68,8 +70,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers [HttpPost("updateUser")] public async Task UpdateUser() { - User user = await this.database.UserFromRequest(this.Request); - + User? user = await this.database.UserFromRequest(this.Request); if (user == null) return this.StatusCode(403, ""); XmlReaderSettings settings = new() @@ -144,8 +145,8 @@ namespace LBPUnion.ProjectLighthouse.Controllers // the way location on a user card works is stupid and will not save with the way below as-is, so we do the following: if (locationChanged) // only modify the database if we modify here { - Location l = await this.database.Locations.Where(l => l.Id == user.LocationId).FirstOrDefaultAsync(); // find the location in the database again - + Location? l = await this.database.Locations.Where(l => l.Id == user.LocationId).FirstOrDefaultAsync(); // find the location in the database again + if (l == null) return this.StatusCode(403, ""); // set the location in the database to the one we modified above l.X = user.Location.X; l.Y = user.Location.Y; @@ -160,11 +161,11 @@ namespace LBPUnion.ProjectLighthouse.Controllers [HttpPost("update_my_pins")] public async Task UpdateMyPins() { - User user = await this.database.UserFromRequest(this.Request); + User? user = await this.database.UserFromRequest(this.Request); if (user == null) return this.StatusCode(403, ""); string pinsString = await new StreamReader(this.Request.Body).ReadToEndAsync(); - Pins pinJson = JsonSerializer.Deserialize(pinsString); + Pins? pinJson = JsonSerializer.Deserialize(pinsString); if (pinJson == null) return this.BadRequest();