Skip to content

Commit

Permalink
switch to declarative validation now that v8.2.3 populates empty Requ…
Browse files Browse the repository at this point in the history
…est DTO props from querystring
  • Loading branch information
mythz committed May 4, 2024
1 parent 383d95e commit aac90cc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 27 deletions.
17 changes: 0 additions & 17 deletions MyApp.ServiceInterface/AiServerServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ public class AiServerServices(ILogger<AiServerServices> 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");
Expand Down Expand Up @@ -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<string>(Db.From<ApplicationUser>().Where(x => x.Id == request.UserId).Select(x => x.UserName));
if (answerCreator == null)
Expand Down
20 changes: 11 additions & 9 deletions MyApp.ServiceInterface/QuestionServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,51 +155,53 @@ public async Task<object> 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);

MessageProducer.Publish(new DbWrites
{
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();
Expand Down
7 changes: 6 additions & 1 deletion MyApp.ServiceModel/OpenAi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}

Expand Down

0 comments on commit aac90cc

Please sign in to comment.