#include "model/Post.hpp"
#include "core/JsonDecode.hpp"
namespace dp {
Post::Data Post::Data::fromJson(const Json& j) {
using D = JsonDecode;
Data d;
d.id = D::req<std::string>(j, "uid");
d.title = D::opt<std::string>(j, "title");
d.topic = D::opt<std::string>(j, "topic");
d.content = D::req<std::string>(j, "content");
d.slug = D::opt<std::string>(j, "slug");
d.userId = D::req<std::string>(j, "user_uid");
d.stars = D::req<std::int64_t>(j, "stars");
d.image = D::opt<std::string>(j, "image");
d.createdAt = D::reqDate(j, "created_at");
d.updatedAt = D::optDate(j, "updated_at");
return d;
}
Post Post::fromJson(const Json& j) {
using D = JsonDecode;
Post p;
p.data = Data::fromJson(D::child(j, "post"));
p.author = User::fromJson(D::child(j, "author"));
p.myVote = Vote::fromInt(D::req<std::int64_t>(j, "my_vote"));
p.commentCount = D::req<std::int64_t>(j, "comment_count");
p.recentComments = D::reqModels<Comment>(j, "recent_comments");
p.bookmarked = D::req<bool>(j, "bookmarked");
p.attachments = D::reqModels<Attachment>(j, "attachments");
if (const Json* poll = JsonRead::child(j, "poll")) p.poll = Poll::fromJson(*poll);
p.reactions = Reactions::fromJson(D::child(j, "reactions"));
return p;
}
Post Post::replacingReactions(const Reactions& newReactions) const {
Post out = *this;
out.reactions = newReactions;
return out;
}
Post Post::replacingRecentComments(const std::vector<Comment>& newComments) const {
Post out = *this;
out.recentComments = newComments;
return out;
}
} // namespace dp