From aac90cc9ec810fd5af352957fbd127b4c9d283b8 Mon Sep 17 00:00:00 2001 From: Demis Bellot Date: Sat, 4 May 2024 11:26:32 +0800 Subject: [PATCH] switch to declarative validation now that v8.2.3 populates empty Request DTO props from querystring --- MyApp.ServiceInterface/AiServerServices.cs | 17 ----------------- MyApp.ServiceInterface/QuestionServices.cs | 20 +++++++++++--------- MyApp.ServiceModel/OpenAi.cs | 7 ++++++- 3 files changed, 17 insertions(+), 27 deletions(-) diff --git a/MyApp.ServiceInterface/AiServerServices.cs b/MyApp.ServiceInterface/AiServerServices.cs index 425c7e1..47cf338 100644 --- a/MyApp.ServiceInterface/AiServerServices.cs +++ b/MyApp.ServiceInterface/AiServerServices.cs @@ -17,13 +17,6 @@ public class AiServerServices(ILogger log, { public async Task Any(CreateAnswerCallback request) { - if (request.PostId == 0) - request.PostId = int.TryParse(Request!.QueryString[nameof(request.PostId)], out var postId) - ? postId - : throw new ArgumentNullException(nameof(request.PostId)); - if (string.IsNullOrEmpty(request.UserId)) - request.UserId = Request!.QueryString[nameof(request.UserId)] ?? throw new ArgumentNullException(nameof(request.UserId)); - var modelUser = appConfig.GetModelUserById(request.UserId); if (modelUser?.UserName == null) throw HttpError.Forbidden("Invalid Model User Id"); @@ -62,16 +55,6 @@ public async Task Any(CreateAnswerCallback request) public async Task Any(RankAnswerCallback request) { - if (request.PostId == 0) - request.PostId = int.TryParse(Request!.QueryString[nameof(request.PostId)], out var postId) - ? postId - : throw new ArgumentNullException(nameof(request.PostId)); - if (string.IsNullOrEmpty(request.UserId)) - request.UserId = Request!.QueryString[nameof(request.UserId)] ?? throw new ArgumentNullException(nameof(request.UserId)); - - if (string.IsNullOrEmpty(request.Grader)) - request.Grader = Request!.QueryString[nameof(request.Grader)] ?? throw new ArgumentNullException(nameof(request.Grader)); - var answerCreator = appConfig.GetModelUserById(request.UserId)?.UserName ?? await Db.ScalarAsync(Db.From().Where(x => x.Id == request.UserId).Select(x => x.UserName)); if (answerCreator == null) diff --git a/MyApp.ServiceInterface/QuestionServices.cs b/MyApp.ServiceInterface/QuestionServices.cs index 257a7a8..056f459 100644 --- a/MyApp.ServiceInterface/QuestionServices.cs +++ b/MyApp.ServiceInterface/QuestionServices.cs @@ -155,24 +155,26 @@ public async Task Any(AnswerQuestion request) { var userName = GetUserName(); var now = DateTime.UtcNow; + var postId = request.PostId; + var answerId = $"{postId}-{userName}"; var answer = new Post { - ParentId = request.PostId, + ParentId = postId, Summary = request.Body.GenerateSummary(), CreationDate = now, CreatedBy = userName, LastActivityDate = now, Body = request.Body, RefUrn = request.RefUrn, - RefId = $"{request.PostId}-{userName}" + RefId = answerId }; MessageProducer.Publish(new DbWrites { CreateAnswer = answer, - AnswerAddedToPost = new() { Id = request.PostId}, + AnswerAddedToPost = new() { Id = postId }, }); - rendererCache.DeleteCachedQuestionPostHtml(answer.Id); + rendererCache.DeleteCachedQuestionPostHtml(postId); await questions.SaveHumanAnswerAsync(answer); @@ -180,26 +182,26 @@ public async Task Any(AnswerQuestion request) { SaveStartingUpVotes = new() { - Id = answer.RefId!, - PostId = request.PostId, + Id = answerId, + PostId = postId, StartingUpVotes = 0, CreatedBy = userName, } }); - answerNotifier.NotifyNewAnswer(request.PostId, answer.CreatedBy); + answerNotifier.NotifyNewAnswer(postId, answer.CreatedBy); var userId = Request.GetClaimsPrincipal().GetUserId(); MessageProducer.Publish(new AiServerTasks { CreateRankAnswerTask = new CreateRankAnswerTask { - AnswerId = answer.RefId!, + AnswerId = answerId, UserId = userId!, } }); MessageProducer.Publish(new SearchTasks { - AddAnswerToIndex = answer.RefId + AddAnswerToIndex = answerId }); return new AnswerQuestionResponse(); diff --git a/MyApp.ServiceModel/OpenAi.cs b/MyApp.ServiceModel/OpenAi.cs index d079002..574668c 100644 --- a/MyApp.ServiceModel/OpenAi.cs +++ b/MyApp.ServiceModel/OpenAi.cs @@ -6,18 +6,23 @@ namespace MyApp.ServiceModel; [SystemJson(UseSystemJson.Never)] public class CreateAnswerCallback : OpenAiChatResponse, IPost, IReturnVoid { + [ValidateGreaterThan(9)] public int PostId { get; set; } - + + [ValidateNotEmpty] public string UserId { get; set; } } [SystemJson(UseSystemJson.Never)] public class RankAnswerCallback : OpenAiChatResponse, IPost, IReturnVoid { + [ValidateGreaterThan(9)] public int PostId { get; set; } + [ValidateNotEmpty] public string UserId { get; set; } // Use User GUID to prevent tampering + [ValidateNotEmpty] public string Grader { get; set; } }