forked from sosschs9/2022_SoftwareDesign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.py
327 lines (253 loc) · 9.91 KB
/
post.py
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import pymongo
from abc import *
from basic import *
from user import *
# app.py 작성시 맨 아래쪽의 Method(app.py) 부분 참고
# DB 연결
client = pymongo.MongoClient("mongodb+srv://SD:[email protected]/?retryWrites=true&w=majority")
db = client['2022SoftwareDesign']
col_post = db['Post']
##### PD Layer Class #####
class Writing(metaclass=ABCMeta):
@abstractmethod
def __init__(self, writerID, writeDate, contents):
self.__writerID = writerID
self.__writeDate = writeDate
self.__contents = contents
@abstractmethod
def delete(self):
pass
@abstractmethod
def modify(self):
pass
@abstractmethod
def getWriteDate(self):
pass
@abstractmethod
def getWriterID(self):
pass
# Post Class
class Post(Writing):
# 초기화: 글번호, 작성시간, 작성자ID, 제목, 내용, 요청날짜, 주소, 보수, 연락방법, 댓글리스트
def __init__(self, postIdx: int, writeDate: str, writerID: str, title: str, contents: str, reqDate: str,
addr: Address, pay: str, contact: str, commentList: list):
self.__postIdx = postIdx
self.__writeDate = writeDate
self.__writerID = writerID
self.__title = title
self.__contents = contents
self.__requestDate = reqDate
self.__address = addr
self.__pay = pay
self.__contact = contact
self.commentList = commentList
# 댓글 추가
def addComment(self, commentIdx: int, writeDate: str, writerID: str, contents: str):
self.commentList.append(Comment(commentIdx, writeDate, writerID, contents))
# 글 수정
def modify(self, title: str, contents: str, reqDate: str, addr: Address, pay: str, contact: str):
self.__title = title
self.__contents = contents
self.__requestDate = reqDate
self.__address = addr
self.__pay = pay
self.__contact = contact
# 글 삭제
def delete(self):
deletePost(self.__postIdx)
# Post Getter
def getPostIdx(self):
return self.__postIdx
def getWriteDate(self):
return self.__writeDate
def getWriterID(self):
return self.__writerID
def getTitle(self):
return self.__title
def getContents(self):
return self.__contents
def getRequestDate(self):
return self.__requestDate
def getAddress(self):
return self.__address
def getPay(self):
return self.__pay
def getContact(self):
return self.__contact
def getCommentCnt(self):
return len(self.commentList)
# Comment Class
class Comment(Writing):
# 초기화: 댓글번호, 작성시간, 작성자ID, 내용
def __init__(self, commentIdx: int, writeDate: str, writerID: str, contents: str):
self.__commentIdx = commentIdx
self.__writeDate = writeDate
self.__writerID = writerID
self.__contents = contents
# 댓글 수정
def modify(self, contents: str):
self.__contents = contents
# 댓글 삭제 ** 해당 Post의 commentList에서 삭제해야함 **
def delete(self):
pass
# Comment Getter
def getCommentIdx(self):
return self.__commentIdx
def getWriteDate(self):
return self.__writeDate
def getWriterID(self):
return self.__writerID
def getContents(self):
return self.__contents
##### POST DB #####
# 새로운 게시글 번호 / ret: int
def getPostNumber():
result = col_post.find().sort('_Post__postIdx', -1).limit(1)
for i in result:
return i['_Post__postIdx'] + 1
return 1
# 게시글 추가
def DB_addPost(nPost: Post):
element = nPost.__dict__
element['_Post__address'] = nPost.getAddress().__dict__
commentList = []
for i in nPost.commentList:
commentList.append(i.__dict__)
element['commentList'] = commentList
col_post.insert_one(element)
# 게시글(+댓글) 불러오기 / ret: Post
def getPost(postIdx: int):
result = col_post.find_one({'_Post__postIdx': postIdx})
temp = result['_Post__address']
addr = Address(temp['detailAddress'], temp['placeName'], temp['region'])
post = Post(result['_Post__postIdx'], result['_Post__writeDate'], result['_Post__writerID'],
result['_Post__title'], result['_Post__contents'], result['_Post__requestDate'],
addr, result['_Post__pay'], result['_Post__contact'], [])
for i in result['commentList']:
post.addComment(i['_Comment__commentIdx'], i['_Comment__writeDate'], i['_Comment__writerID'],
i['_Comment__contents'])
return post
# 게시글 삭제
def DB_deletePost(postIdx: int):
col_post.delete_one({'_Post__postIdx': postIdx})
# 게시글 업데이트
def DB_updatePost(post: Post):
DB_deletePost(post.getPostIdx())
DB_addPost(post)
# 총 게시글 페이지 수 / ret: int
def getAllPostPageCount():
cnt = col_post.estimated_document_count()
if cnt % 20 != 0:
return int(cnt / 20) + 1
else:
return int(cnt / 20)
# 게시글 목록 불러오기(페이지 번호별 20개) / ret:list
# list: {'postIdx':글번호, 'region':지역, 'title':제목, 'writerID':작성자ID, 'writeDate':작성일자}
def getAllPostList(pageNumber: int):
maxIdx = pageNumber * 20
result = col_post.find().sort('_Post__postIdx', -1).limit(maxIdx)
ret = []
cnt = 0
for element in result:
cnt += 1
if cnt <= (pageNumber - 1) * 20:
continue
else:
ret.append({'postIdx': element['_Post__postIdx'], 'title': element['_Post__title'],
'region': element['_Post__address']['region'],
'writerID': element['_Post__writerID'], 'writeDate': element['_Post__writeDate']})
return ret
# 총 region 페이지 수 / ret: int
def getRegionPostPageCount(region:str):
cnt = 0
result = col_post.find({})
for element in result:
if element['_Post__address']['region'] == region:
cnt += 1
if cnt % 20 != 0:
return int(cnt / 20) + 1
else:
return int(cnt / 20)
# 지역별 게시글 목록 불러오기(페이지 번호별 20개) / ret:list
# list 형태는 getAllPostList와 같음
def getRegionPostList(pageNumber: int, region: str):
maxIdx = pageNumber * 20
postIdx = getPostNumber() - 1
ret = []
postCnt = 0
while postIdx > 0 and len(ret) < 20:
element = col_post.find_one({'_Post__postIdx': postIdx})
postIdx -= 1
if element == None:
continue
if element['_Post__address']['region'] == region:
postCnt += 1
if postCnt <= (pageNumber - 1) * 20:
continue
else:
ret.append({'postIdx': element['_Post__postIdx'], 'title': element['_Post__title'],
'region': element['_Post__address']['region'],
'writerID': element['_Post__writerID'], 'writeDate': element['_Post__writeDate']})
return ret
# 해당 유저의 게시글 목록 불러오기 / ret: list
# list 형태는 getAllPostList와 같음
def getUserPostList(postNumList: list):
ret = []
for i in postNumList:
element = col_post.find_one({'_Post__postIdx': i})
ret.append({'postIdx': element['_Post__postIdx'], 'title': element['_Post__title'],
'region': element['_Post__address']['region'],
'writerID': element['_Post__writerID'], 'writeDate': element['_Post__writeDate']})
return ret
##### Method(app.py) #####
# 의뢰 게시판 페이지 수 >> getAllPostPageCount()
# 의뢰 게시판 글 목록(지역설정X) 가져오기 >> getAllPostList(pageNumber:int)
# 의뢰 게시판 글 목록(지역설정O) 가져오기 >> getRegionPostList(pageNumber:int, region:str)
# 사용자 게시판 작성 글 목록 가져오기 >> getUserPostList(postNumList:list)
# Post 불러오기(1개) >> getPost(postIdx)
# 새로운 글 생성 :: 작성자ID, 제목, 내용, 요청날짜, Address, 보수, 연락방법
def createPost(writerID: str, title: str, contents: str, reqDate: str, addr: Address, pay: str, contact: str):
writeDate = getNowTime()
postIdx = getPostNumber()
nPost = Post(postIdx, writeDate, writerID, title, contents, reqDate, addr, pay, contact, [])
DB_addPost(nPost)
user = DB_getUser(writerID)
user.addMyPost(postIdx)
DB_updateUser(user)
# 글 삭제 :: 글번호
def deletePost(postIdx: int):
post = getPost(postIdx)
DB_deletePost(postIdx)
user = DB_getUser(post.getWriterID())
user.deleteMyPost(postIdx)
DB_updateUser(user)
# 글 수정 :: 글 번호, 제목, 내용, 요청날짜, Address, 보수, 연락방법
def modifyPost(postIdx: int, title: str, contents: str, reqDate: str, addr: Address, pay: str, contact: str):
post = getPost(postIdx)
post.modify(title, contents, reqDate, addr, pay, contact)
DB_updatePost(post)
# 새로운 댓글 생성:: 글번호, 작성자ID, 내용
def createComment(postIdx: int, writerID: str, contents: str):
post = getPost(postIdx)
commentIdx = post.getCommentCnt() + 1
writeDate = getNowTime()
post.addComment(commentIdx, writeDate, writerID, contents)
DB_updatePost(post)
# 댓글 삭제:: 글번호, 댓글번호
def deleteComment(postIdx: int, commentIdx: int):
post = getPost(postIdx)
for comment in post.commentList:
if comment.getCommentIdx() == commentIdx:
index = post.commentList.index(comment)
post.commentList.pop(index)
DB_updatePost(post)
return
# 댓글 수정:: 글번호, 댓글번호, 내용
def modifyComment(postIdx: int, commentIdx: int, contents: str):
post = getPost(postIdx)
for comment in post.commentList:
if comment.getCommentIdx() == commentIdx:
index = post.commentList.index(comment)
post.commentList[index].modify(contents)
DB_updatePost(post)
return