Skip to content

Commit

Permalink
Add DeleteQuestion API
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Mar 26, 2024
1 parent c97f4ab commit 8779465
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
19 changes: 17 additions & 2 deletions MyApp.ServiceInterface/BackgroundMqServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ public async Task Any(DbWrites request)
{
await Db.InsertAsync(request.CreatePost);
}

if (request.DeletePost != null)
{
await Db.DeleteAsync<PostJob>(x => x.PostId == request.DeletePost);
await Db.DeleteAsync<Vote>(x => x.PostId == request.DeletePost);
await Db.DeleteByIdAsync<Post>(request.DeletePost);
}

if (request.CreatePostJobs is { Count: > 0 })
{
Expand Down Expand Up @@ -125,16 +132,24 @@ public async Task Any(DbWrites request)

public async Task Any(AnalyticsTasks request)
{
if (request.RecordPostView == null && request.RecordSearchView == null && request.DeletePost == null)
return;

using var analyticsDb = HostContext.AppHost.GetDbConnection(Databases.Analytics);

if (request.RecordPostView != null)// && !Stats.IsAdminOrModerator(request.RecordPostView.UserName))
{
using var analyticsDb = HostContext.AppHost.GetDbConnection(Databases.Analytics);
await analyticsDb.InsertAsync(request.RecordPostView);
}

if (request.RecordSearchView != null)// && !Stats.IsAdminOrModerator(request.RecordSearchView.UserName))
{
using var analyticsDb = HostContext.AppHost.GetDbConnection(Databases.Analytics);
await analyticsDb.InsertAsync(request.RecordSearchView);
}

if (request.DeletePost != null)
{
await analyticsDb.DeleteAsync<PostView>(x => x.PostId == request.DeletePost);
}
}
}
8 changes: 8 additions & 0 deletions MyApp.ServiceInterface/Data/QuestionsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,12 @@ public async Task SaveRemoteFileAsync(string virtualPath, string contents)
{
await r2.WriteFileAsync(virtualPath, contents);
}

public async Task DeleteQuestionFilesAsync(int id)
{
var localQuestionFiles = GetLocalQuestionFiles(id);
fs.DeleteFiles(localQuestionFiles.Files.Select(x => x.VirtualPath));
var remoteQuestionFiles = await GetRemoteQuestionFilesAsync(id);
r2.DeleteFiles(remoteQuestionFiles.Files.Select(x => x.VirtualPath));
}
}
17 changes: 17 additions & 0 deletions MyApp.ServiceInterface/QuestionServices.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using MyApp.Data;
using ServiceStack;
using MyApp.ServiceModel;
using ServiceStack.OrmLite;

namespace MyApp.ServiceInterface;

Expand Down Expand Up @@ -57,6 +58,17 @@ public async Task<object> Any(AskQuestion request)
};
}

public async Task<EmptyResponse> Any(DeleteQuestion request)
{
await questions.DeleteQuestionFilesAsync(request.Id);
rendererCache.DeleteCachedQuestionPostHtml(request.Id);
MessageProducer.Publish(new DbWrites
{
DeletePost = request.Id,
});
return new EmptyResponse();
}

public async Task<object> Any(AnswerQuestion request)
{
var userName = GetUserName();
Expand All @@ -74,6 +86,11 @@ public async Task<object> Any(AnswerQuestion request)

await questions.SaveHumanAnswerAsync(post);
rendererCache.DeleteCachedQuestionPostHtml(post.Id);

// Rewind last Id if it was latest question
var maxPostId = Db.Scalar<int>("SELECT MAX(Id) FROM Post");
AppConfig.Instance.SetInitialPostId(Math.Max(100_000_000, maxPostId));

return new AnswerQuestionResponse();
}

Expand Down
7 changes: 7 additions & 0 deletions MyApp.ServiceModel/Posts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,10 @@ public class PreviewMarkdown : IPost, IReturn<string>
{
public string Markdown { get; set; }

Check warning on line 295 in MyApp.ServiceModel/Posts.cs

View workflow job for this annotation

GitHub Actions / push_to_registry

Non-nullable property 'Markdown' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}

[ValidateHasRole(Roles.Moderator)]
public class DeleteQuestion : IGet, IReturn<EmptyResponse>
{
[ValidateGreaterThan(0)]
public int Id { get; set; }
}
3 changes: 2 additions & 1 deletion MyApp.ServiceModel/Stats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public class AnalyticsTasks
{
public SearchView? RecordSearchView { get; set; }
public PostView? RecordPostView { get; set; }
public int? DeletePost { get; set; }
}

public class StartJob
Expand All @@ -114,7 +115,7 @@ public class DbWrites
{
public Vote? RecordPostVote { get; set; }
public Post? CreatePost { get; set; }
public Post? UpdatePost { get; set; }
public int? DeletePost { get; set; }
public List<PostJob>? CreatePostJobs { get; set; }
public StartJob? StartJob { get; set; }
public int? AnswerAddedToPost { get; set; }
Expand Down

0 comments on commit 8779465

Please sign in to comment.