Skip to content
Repos

DevPlaceApp

C++ 1.5 KB36 lines Opens in site

Directory Raw Download

#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<bool>(j, "is_owner");
  d.starCount = D::req<std::int64_t>(j, "star_count");
  d.myVote = Vote::fromInt(D::req<std::int64_t>(j, "my_vote"));
  d.comments = D::reqModels<Comment>(j, "comments");
  d.attachments = D::reqModels<Attachment>(j, "attachments");
  d.bookmarked = D::req<bool>(j, "bookmarked");
  if (const Json* poll = JsonRead::child(j, "poll")) d.poll = Poll::fromJson(*poll);
  d.commentCount = D::req<std::int64_t>(j, "comment_count");
  d.relatedPosts = D::reqModels<Post>(j, "related_posts");
  d.topics = D::reqList<std::string>(j, "topics", [](const Json& v) { return D::convert<std::string>(v, "topics"); });
  d.reactions = Reactions::fromJson(D::child(j, "reactions"));
  return d;
}

PostDetail PostDetail::with(std::optional<Vote> newVote, std::optional<std::int64_t> newStarCount,
                            std::optional<std::vector<Comment>> newComments, std::optional<Reactions> 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
21.2ms