#include "model/PostDetail.hpp" #include "core/JsonDecode.hpp" namespace dp { PostDetail PostDetail::fromJson(const Json& j) { using D = JsonDecode; PostDetail d; d.post = Post::Data::fromJson(D::child(j, "post")); d.author = User::fromJson(D::child(j, "author")); d.isOwner = D::req(j, "is_owner"); d.starCount = D::req(j, "star_count"); d.myVote = Vote::fromInt(D::req(j, "my_vote")); d.comments = D::reqModels(j, "comments"); d.attachments = D::reqModels(j, "attachments"); d.bookmarked = D::req(j, "bookmarked"); if (const Json* poll = JsonRead::child(j, "poll")) d.poll = Poll::fromJson(*poll); d.commentCount = D::req(j, "comment_count"); d.relatedPosts = D::reqModels(j, "related_posts"); d.topics = D::reqList(j, "topics", [](const Json& v) { return D::convert(v, "topics"); }); d.reactions = Reactions::fromJson(D::child(j, "reactions")); return d; } PostDetail PostDetail::with(std::optional newVote, std::optional newStarCount, std::optional> newComments, std::optional newReactions) const { PostDetail out = *this; if (newVote) out.myVote = *newVote; if (newStarCount) out.starCount = *newStarCount; if (newComments) out.comments = std::move(*newComments); if (newReactions) out.reactions = std::move(*newReactions); return out; } } // namespace dp