Skip to content
This repository was archived by the owner on Dec 8, 2022. It is now read-only.

Commit d86f5fc

Browse files
committed
fix incorrect answers showing up
fix searching endpoint
1 parent 2eaf24a commit d86f5fc

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

CodeChallenge/api/vote.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def get_contestants():
5454
.join(Answer.question) \
5555
.join(Answer.user) \
5656
.outerjoin(Answer.votes) \
57-
.filter(Question.rank == core.max_rank()) \
57+
.filter(Question.rank == core.max_rank(), Vote.confirmed.is_(True), Answer.correct) \
5858
.group_by(Answer.id)
5959

6060
if desc is not None:
@@ -214,29 +214,26 @@ def search():
214214

215215
keyword = f"%{keyword}%"
216216

217-
p = Answer.query \
217+
p = Answer.query.with_entities(
218+
Answer.id,
219+
Answer.text,
220+
func.count(Answer.votes),
221+
Users.studentfirstname,
222+
Users.studentlastname,
223+
Users.username,
224+
func.concat(Users.studentfirstname, func.right(Users.studentlastname, 1))
225+
) \
218226
.join(Answer.question) \
219227
.join(Answer.user) \
220-
.filter(Question.rank == core.max_rank(),
221-
Answer.correct, or_(Users.username.ilike(keyword), Users.studentlastname.ilike(keyword),
222-
Users.studentlastname.ilike(keyword))) \
228+
.outerjoin(Answer.votes) \
229+
.filter(Question.rank == core.max_rank(), Vote.confirmed.is_(True), Answer.correct,
230+
or_(Users.username.ilike(keyword), Users.studentfirstname.ilike(keyword),
231+
Users.studentlastname.ilike(keyword))) \
232+
.group_by(Answer.id)\
223233
.paginate(page=page, per_page=per)
224234

225-
results = []
226-
227-
for ans in p.items: # type: Answer
228-
results.append(dict(
229-
id=ans.id,
230-
text=ans.text,
231-
numVotes=ans.confirmed_votes(),
232-
firstName=ans.user.studentfirstname,
233-
lastName=ans.user.studentlastname,
234-
username=ans.user.username,
235-
display=ans.user.display()
236-
))
237-
238235
return jsonify(
239-
items=results,
236+
items=p.items,
240237
totalItems=p.total,
241238
page=p.page,
242239
totalPages=p.pages,

CodeChallenge/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ class ProductionConfig(DefaultConfig):
8181

8282
class DevelopmentConfig(ProductionConfig):
8383
EXTERNAL_URL = "http://localhost:8080"
84-
#SQLALCHEMY_DATABASE_URI = "mysql://cc-user:password@localhost" \
85-
# "/code_challenge_local"
84+
SQLALCHEMY_DATABASE_URI = os.getenv("SQLALCHEMY_DATABASE_URI",
85+
"mysql://cc-user:password@localhost/code_challenge_local")
8686
JWT_COOKIE_SECURE = False
8787
CODE_CHALLENGE_START = os.getenv("CODE_CHALLENGE_START", "1581415200")
8888
JWT_SECRET_KEY = "SuperSecret"

0 commit comments

Comments
 (0)