Skip to content

Commit

Permalink
Allow publish from existing draft.
Browse files Browse the repository at this point in the history
  • Loading branch information
MetroWind committed Sep 29, 2024
1 parent f6db490 commit 65bdf38
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
29 changes: 23 additions & 6 deletions src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ std::string App::urlFor(const std::string& name, const std::string& arg) const
{
return URL(base_url).appendPath("save-post").str();
}
if(name == "publish-from-new-draft")
if(name == "publish-from-draft")
{
return URL(base_url).appendPath("publish-from-new-draft").str();
}
Expand Down Expand Up @@ -597,14 +597,31 @@ void App::handleSavePost(const httplib::Request& req, httplib::Response& res)
res.set_redirect(urlFor("post", std::to_string(*p.id)));
}

void App::handlePublishFromNewDraft(const httplib::Request& req, httplib::Response& res)
void App::handlePublishFromDraft(const httplib::Request& req, httplib::Response& res)
const
{
auto session = prepareSession(req, res);
if(!session.has_value()) return;

ASSIGN_OR_RESPOND_ERROR(Post draft, formToPost(req, session->user.name), res);
ASSIGN_OR_RESPOND_ERROR(int64_t id, data->saveDraft(std::move(draft)), res);
ASSIGN_OR_RESPOND_ERROR(Post draft, formToPost(req, session->user.name),
res);
int64_t id;
if(draft.id.has_value())
{
id = *draft.id;
auto ok_maybe = data->editDraft(draft);
if(!ok_maybe)
{
res.status = 500;
res.set_content(errorMsg(ok_maybe.error()), "text/plain");
return;
}
}
else
{
ASSIGN_OR_RESPOND_ERROR(id, data->saveDraft(std::move(draft)),
res);
}
if(!data->publishPost(id))
{
res.status = 500;
Expand Down Expand Up @@ -924,10 +941,10 @@ void App::setup()
{
handleSaveDraft(req, res);
});
server.Post(getPath("publish-from-new-draft"),
server.Post(getPath("publish-from-draft"),
[&](const httplib::Request& req, httplib::Response& res)
{
handlePublishFromNewDraft(req, res);
handlePublishFromDraft(req, res);
});
server.Get(getPath("attachments"),
[&](const httplib::Request& req, httplib::Response& res)
Expand Down
4 changes: 2 additions & 2 deletions src/app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class App
httplib::Response& res);
void handleSavePost(const httplib::Request& req, httplib::Response& res)
const;
void handlePublishFromNewDraft(const httplib::Request& req,
httplib::Response& res) const;
void handlePublishFromDraft(const httplib::Request& req,
httplib::Response& res) const;
void handleAttachments(const httplib::Request& req, httplib::Response& res);
void handleAttachment(const httplib::Request& req, httplib::Response& res);
void handleAttachmentUpload(const httplib::Request& req,
Expand Down
2 changes: 1 addition & 1 deletion templates/create_post.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ <h1>{{ blog_title }}</h1>
<div><textarea id="TextContent" name="content" required></textarea></div>
<div id="ButtonBar">
<input type="submit" value="Save draft" formaction="{{ url_for("save-draft") }}" formmethod="post" formtarget="_self" />
<input type="submit" value="Publish" formaction="{{ url_for("publish-from-new-draft") }}" formmethod="post" formtarget="_self" />
<input type="submit" value="Publish" formaction="{{ url_for("publish-from-draft") }}" formmethod="post" formtarget="_self" />
</div>
</form>
</main>
Expand Down
2 changes: 1 addition & 1 deletion templates/edit_draft.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ <h1>{{ blog_title }}</h1>
</div>
<div id="ButtonBar">
<input type="submit" value="Save draft" formaction="{{ url_for("save-draft") }}" formmethod="post" formtarget="_self" />
<input type="submit" value="Publish" formaction="{{ url_for("publish-from-new-draft") }}" formmethod="post" formtarget="_self" />
<input type="submit" value="Publish" formaction="{{ url_for("publish-from-draft") }}" formmethod="post" formtarget="_self" />
</div>
</form>
</main>
Expand Down

0 comments on commit 65bdf38

Please sign in to comment.