From 6b7d9ca0f87f007e9da6c697669390e4b9a17361 Mon Sep 17 00:00:00 2001 From: evan-scales Date: Sat, 13 Apr 2024 17:40:28 -0400 Subject: [PATCH 1/4] 442: Assert valid date & time on post update --- FU.API/FU.API/Services/PostService.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/FU.API/FU.API/Services/PostService.cs b/FU.API/FU.API/Services/PostService.cs index 7da21c1b..42939afa 100644 --- a/FU.API/FU.API/Services/PostService.cs +++ b/FU.API/FU.API/Services/PostService.cs @@ -7,6 +7,7 @@ namespace FU.API.Services; using FU.API.Models; using Microsoft.EntityFrameworkCore; using System.Collections.Generic; +using Microsoft.Extensions.Hosting; public class PostService : CommonService, IPostService { @@ -76,6 +77,8 @@ public async Task UpdatePost(Post postChanges) throw new PostException("The updated post's creator does not match the old post's creator", HttpStatusCode.UnprocessableEntity); } + AssertValidDateAndTime(postChanges, updatingPost: true); + ogPost.Game = await _dbContext.Games.FindAsync(postChanges.GameId) ?? throw new NonexistentGameException(); ogPost.Description = postChanges.Description; ogPost.MaxPlayers = postChanges.MaxPlayers; @@ -211,7 +214,7 @@ public async Task DeletePost(int postId) await _dbContext.SaveChangesAsync(); } - private static void AssertValidDateAndTime(Post post) + private static void AssertValidDateAndTime(Post post, bool updatingPost = false) { // Check if either start time or end time is present when the other is present bool isInvalidTimeRange = (post.StartTime is null) != (post.EndTime is null); @@ -221,8 +224,9 @@ private static void AssertValidDateAndTime(Post post) } // Make sure the post is not in the past + // Only check if we are not updating the post so that we can allow for posts to be updated bool isPostInPast = post.StartTime < DateTime.UtcNow; - if (isPostInPast) + if (!updatingPost && isPostInPast) { throw new PostException("Post cannot be in the past", HttpStatusCode.UnprocessableEntity); } From cfca6efe1edd04db563a93b932ca068990791ab4 Mon Sep 17 00:00:00 2001 From: evan-scales Date: Sat, 13 Apr 2024 17:42:31 -0400 Subject: [PATCH 2/4] remove using statement --- FU.API/FU.API/Services/PostService.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/FU.API/FU.API/Services/PostService.cs b/FU.API/FU.API/Services/PostService.cs index 42939afa..b28b95b9 100644 --- a/FU.API/FU.API/Services/PostService.cs +++ b/FU.API/FU.API/Services/PostService.cs @@ -7,7 +7,6 @@ namespace FU.API.Services; using FU.API.Models; using Microsoft.EntityFrameworkCore; using System.Collections.Generic; -using Microsoft.Extensions.Hosting; public class PostService : CommonService, IPostService { From 412a5ebd85812c53fcd86f4ea76c3ba884231a9a Mon Sep 17 00:00:00 2001 From: evan-scales Date: Thu, 18 Apr 2024 16:05:32 -0400 Subject: [PATCH 3/4] 442: Fix review points --- FU.API/FU.API/Services/PostService.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/FU.API/FU.API/Services/PostService.cs b/FU.API/FU.API/Services/PostService.cs index b28b95b9..8e2b153d 100644 --- a/FU.API/FU.API/Services/PostService.cs +++ b/FU.API/FU.API/Services/PostService.cs @@ -76,7 +76,7 @@ public async Task UpdatePost(Post postChanges) throw new PostException("The updated post's creator does not match the old post's creator", HttpStatusCode.UnprocessableEntity); } - AssertValidDateAndTime(postChanges, updatingPost: true); + AssertValidDateAndTime(postChanges); ogPost.Game = await _dbContext.Games.FindAsync(postChanges.GameId) ?? throw new NonexistentGameException(); ogPost.Description = postChanges.Description; @@ -213,7 +213,7 @@ public async Task DeletePost(int postId) await _dbContext.SaveChangesAsync(); } - private static void AssertValidDateAndTime(Post post, bool updatingPost = false) + private static void AssertValidDateAndTime(Post post) { // Check if either start time or end time is present when the other is present bool isInvalidTimeRange = (post.StartTime is null) != (post.EndTime is null); @@ -225,7 +225,7 @@ private static void AssertValidDateAndTime(Post post, bool updatingPost = false) // Make sure the post is not in the past // Only check if we are not updating the post so that we can allow for posts to be updated bool isPostInPast = post.StartTime < DateTime.UtcNow; - if (!updatingPost && isPostInPast) + if (isPostInPast) { throw new PostException("Post cannot be in the past", HttpStatusCode.UnprocessableEntity); } From 6ba67e16995d333102333b613a13a94ad2e1ad2c Mon Sep 17 00:00:00 2001 From: evan-scales Date: Thu, 18 Apr 2024 16:06:03 -0400 Subject: [PATCH 4/4] remove bad comment --- FU.API/FU.API/Services/PostService.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/FU.API/FU.API/Services/PostService.cs b/FU.API/FU.API/Services/PostService.cs index 8e2b153d..0aaa871d 100644 --- a/FU.API/FU.API/Services/PostService.cs +++ b/FU.API/FU.API/Services/PostService.cs @@ -223,7 +223,6 @@ private static void AssertValidDateAndTime(Post post) } // Make sure the post is not in the past - // Only check if we are not updating the post so that we can allow for posts to be updated bool isPostInPast = post.StartTime < DateTime.UtcNow; if (isPostInPast) {