Skip to content

Commit

Permalink
Add API to GetLastUpdated
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed May 5, 2024
1 parent 5380700 commit 2bc50ff
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion MyApp.ServiceInterface/AiServerServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public async Task Any(CreateAnswerCallback request)
PostId = request.PostId,
StartingUpVotes = 0,
CreatedBy = modelUser.UserName,
LastUpdated = DateTime.UtcNow,
}
});

Expand Down Expand Up @@ -166,7 +167,7 @@ public async Task Any(AnswerCommentCallback request)
await questions.SaveMetaAsync(postId, meta);
}

private async Task<string?> AssertUserNameById(string userId)
private async Task<string> AssertUserNameById(string userId)
{
var userName = appConfig.GetModelUserById(userId)?.UserName
?? await Db.ScalarAsync<string?>(Db.From<ApplicationUser>().Where(x => x.Id == userId).Select(x => x.UserName));
Expand Down
5 changes: 5 additions & 0 deletions MyApp.ServiceInterface/App/NewCommentCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ await db.InsertAsync(new Notification
appConfig.IncrUnreadNotificationsFor(createdBy);
}

await db.UpdateOnlyAsync(() => new StatTotals
{
LastUpdated = DateTime.UtcNow,
}, where: x => x.Id == request.RefId);

var userNameMentions = cleanBody.FindUserNameMentions()
.Where(x => x != createdBy && x != comment.CreatedBy && appConfig.IsHuman(x))
.ToList();
Expand Down
2 changes: 2 additions & 0 deletions MyApp.ServiceInterface/App/SaveGradeResultCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ public class SaveGradeResultCommand(IDbConnection db, IMessageProducer mq, Worke
{
public async Task ExecuteAsync(StatTotals request)
{
var lastUpdated = request.LastUpdated ?? DateTime.UtcNow;
var updatedRow = await db.UpdateOnlyAsync(() => new StatTotals
{
StartingUpVotes = request.StartingUpVotes,
CreatedBy = request.CreatedBy,
LastUpdated = lastUpdated,
}, x => x.Id == request.Id);

if (updatedRow == 0)
Expand Down
17 changes: 17 additions & 0 deletions MyApp.ServiceInterface/QuestionServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ public async Task<object> Any(AnswerQuestion request)
PostId = postId,
StartingUpVotes = 0,
CreatedBy = userName,
LastUpdated = DateTime.UtcNow,
}
});

Expand Down Expand Up @@ -520,6 +521,22 @@ public async Task<object> Any(ImportQuestion request)
?? throw new Exception("Import failed")
};
}

public async Task<object> Any(GetLastUpdated request)
{
var lastUpdated = request.Id != null
? await Db.ScalarAsync<DateTime?>(Db.From<StatTotals>().Where(x => x.Id == request.Id)
.Select(x => x.LastUpdated))
: request.PostId != null
? await Db.ScalarAsync<DateTime?>(Db.From<StatTotals>().Where(x => x.PostId == request.PostId)
.Select(x => Sql.Max(x.LastUpdated)))
: null;

return new GetLastUpdatedResponse
{
Result = lastUpdated
};
}
}

[ValidateHasRole(Roles.Moderator)]
Expand Down
10 changes: 10 additions & 0 deletions MyApp.ServiceModel/Posts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -554,3 +554,13 @@ public class ImportQuestionResponse
public required AskQuestion Result { get; set; }
public ResponseStatus? ResponseStatus { get; set; }
}

public class GetLastUpdated : IGet, IReturn<GetLastUpdated>
{
public string? Id { get; set; }
public int? PostId { get; set; }
}
public class GetLastUpdatedResponse
{
public DateTime? Result { get; set; }
}

0 comments on commit 2bc50ff

Please sign in to comment.