-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPost.cpp
135 lines (132 loc) · 3.36 KB
/
Post.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "Post.h"
#include "Id.h"
#include<sstream>
size_t Post::postIdentificator=readIdFromFile("postIdentificator.txt");
size_t Post::getId()const {
return id;
}
const MyString& Post::getTitle()const {
return title;
}
size_t Post::getCommentsSize()const {
return comments.getSize();
}
size_t Post::getIdentificator()const {
return postIdentificator;
}
const Vector<Comment>& Post::getComments()const {
return comments;
}
Vector<Comment>& Post::getComments() {
return comments;
}
void Post::showComments()const {
for (size_t i = 0; i < comments.getSize(); i++) {
std::cout << comments[i].getComment()<<" {id: "<<comments[i].getId()<<"}{upvote: "<<comments[i].getUpvoteCount()
<<"}{downvote: "<< comments[i].getDownvoteCount()<<"}" << std::endl;
comments[i].showReplies(" ");
}
std::cout << std::endl;
}
bool Post::checkId(size_t id)const {
for (size_t i = 0; i < comments.getSize(); i++) {
if (comments[i].getId() == id)
return true;
if (comments[i].getReplies().getSize() > 0) {
for(size_t i=0;i< comments[i].getReplies().getSize();i++){
}
}
}
}
Post::Post(const MyString& title) {
this->title = title;
id = postIdentificator++;
}
void Post::addComments(Comment&& newComment) {
comments.pushBack(std::move(newComment));
}
void Post::addUpvoteToComment(size_t id) {
for (size_t i = 0; i < comments.getSize(); i++) {
if (comments[i].getId() == id) {
comments[i].addUpvote();
break;
}
else {
comments[i].addUpvoteToReply(id);
}
}
}
void Post::addDownvoteToComment(size_t id) {
for (size_t i = 0; i < comments.getSize(); i++) {
if (comments[i].getId() == id) {
comments[i].addDownvote();
break;
}
else {
comments[i].addDownvoteToReply(id);
}
}
}
void Post::removeUpvoteFromComment(size_t id) {
for (size_t i = 0; i < comments.getSize(); i++) {
if (comments[i].getId() == id) {
comments[i].removeUpvote();
break;
}
else {
comments[i].removeUpvoteFromReply(id);
}
}
}
void Post::removeDownvoteFromComment(size_t id) {
for (size_t i = 0; i < comments.getSize(); i++) {
if (comments[i].getId() == id) {
comments[i].removeDownvote();
}
else {
comments[i].removeDownvoteFromReply(id);
}
}
}
void Post::addReply(size_t id,SharedPointer<Comment>&& newReply) {
for (size_t i = 0; i < comments.getSize(); i++) {
if (comments[i].getId() == id) {
comments[i].addReply(std::move(newReply));
break;
}
else {
comments[i].replyToReply(id,std::move(newReply));
}
}
}
int Post::searchCommentById(size_t id) const{
for (size_t i = 0; i < comments.getSize(); i++) {
if (comments[i].getId() == id) {
return i;
}
else if(comments[i].getReplies().getSize()>0){
int flag = comments[i].searchInReplies(id);
if (flag == -1) {
continue;
}
else
return flag;
}
}
return -1;
}
void Post::printPost()const {
std::cout<< title << "{id " << id << "}" << std::endl;
}
std::ofstream& operator<<(std::ofstream& out, const Post& post) {
out.write((const char*)&post.id, sizeof(post.id));
writeMyStringToFile(post.title, out);
writeVectorToFile(post.comments,out);
return out;
}
std::ifstream& operator>>(std::ifstream& in, Post& post) {
in.read((char*)&post.id, sizeof(post.id));
post.title=readMyStringFromFile(in);
readVectorFromFile(post.comments, in);
return in;
}