Skip to content

Commit

Permalink
delete trash
Browse files Browse the repository at this point in the history
  • Loading branch information
kish1n committed Aug 22, 2024
1 parent ba2d8e1 commit b0fe406
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ CREATE TABLE IF NOT EXISTS daily_questions (
correct_answer INTEGER NOT NULL,
num_correct_answers INTEGER DEFAULT 0,
num_incorrect_answers INTEGER DEFAULT 0,
num_no_answer INTEGER DEFAULT 0,
num_all_participants INTEGER DEFAULT 0
);

Expand Down
11 changes: 2 additions & 9 deletions internal/config/daily_questions.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,15 @@ func (q *DailyQuestions) GetDeadline(key string) *int64 {
return &value
}

func (q *DailyQuestions) SetDeadlineTimer(log *logan.Entry, question data.DailyQuestionsQ, eve *data.Event, nullifier string, deadline int64) {
func (q *DailyQuestions) SetDeadlineTimer(log *logan.Entry, question data.DailyQuestionsQ, nullifier string, deadline int64) {
now := time.Now().UTC()

q.muDeadlines.Lock()
q.Deadlines[nullifier] = deadline
q.muDeadlines.Unlock()

time.AfterFunc(time.Duration(deadline-now.Unix())*time.Second, func() {
q.muDeadlines.Lock()
defer q.muDeadlines.Unlock()

if deadline <= time.Now().UTC().Unix() {
if eve != nil {
delete(q.Deadlines, nullifier)
}
}
delete(q.Deadlines, nullifier)
})

err := question.IncrementAllParticipants()
Expand Down
1 change: 0 additions & 1 deletion internal/data/daily_questions.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ type DailyQuestion struct {
CorrectAnswer int64 `db:"correct_answer"`
NumCorrectAnswers int64 `db:"num_correct_answers"`
NumIncorrectAnswers int64 `db:"num_incorrect_answers"`
NumNoAnswer int64 `db:"num_no_answer"`
NumAllParticipants int64 `db:"num_all_participants"`
}

Expand Down
6 changes: 1 addition & 5 deletions internal/data/pg/daily_questions.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func (q *dailyQuestionsQ) Insert(quest data.DailyQuestion) error {
"correct_answer": quest.CorrectAnswer,
"num_correct_answers": quest.NumCorrectAnswers,
"num_incorrect_answers": quest.NumIncorrectAnswers,
"num_no_answer": quest.NumNoAnswer,
"num_all_participants": quest.NumAllParticipants,
})

Expand Down Expand Up @@ -127,7 +126,6 @@ func (q *dailyQuestionsQ) IncrementCorrectAnswer() error {
func (q *dailyQuestionsQ) IncrementIncorrectAnswer() error {
stmt := q.updater.
Set("num_incorrect_answers", squirrel.Expr("num_incorrect_answers + 1"))

if err := q.db.Exec(stmt); err != nil {
return fmt.Errorf("increment incorrect answer: %w", err)
}
Expand All @@ -136,9 +134,7 @@ func (q *dailyQuestionsQ) IncrementIncorrectAnswer() error {

func (q *dailyQuestionsQ) IncrementAllParticipants() error {
stmt := q.updater.
Set("num_all_participants", squirrel.Expr("num_all_participants + 1")).
Set("num_no_answer", squirrel.Expr("num_all_participants + 1 - num_correct_answers - num_incorrect_answers"))

Set("num_all_participants", squirrel.Expr("num_all_participants + 1"))
if err := q.db.Exec(stmt); err != nil {
return fmt.Errorf("increment no answer: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/service/handlers/daily_question_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func CheckDailyQuestion(w http.ResponseWriter, r *http.Request) {

deadline := cfg.GetDeadline(nullifier)
if deadline == nil {
Log(r).Errorf("The user's nullifier was not found in active requests, it does not exist, or the user has already answered: %s", nullifier)
Log(r).Errorf("The user's nullifier was not found in inactive requests, it does not exist, or the user has already answered: %s", nullifier)
ape.RenderErr(w, problems.Forbidden())
return
}
Expand Down Expand Up @@ -102,7 +102,7 @@ func CheckDailyQuestion(w http.ResponseWriter, r *http.Request) {
Log(r).Infof("Wrong answer for daily question: %v", req.Answer)
err = DailyQuestionsQ(r).FilterTodayQuestions(cfg.Timezone).IncrementIncorrectAnswer()
if err != nil {
Log(r).WithError(err).Errorf("Error incrementing question answers incorrect answered")
Log(r).WithError(err).Errorf("Error incrementing question answers incorect answered")
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/service/handlers/daily_question_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func GetDailyQuestion(w http.ResponseWriter, r *http.Request) {
}

nowTime := time.Now().UTC()
cfg.SetDeadlineTimer(Log(r), DailyQuestionsQ(r), questionEvent, balance.Nullifier, nowTime.Unix()+question.TimeForAnswer)
cfg.SetDeadlineTimer(Log(r), DailyQuestionsQ(r), balance.Nullifier, nowTime.Unix()+question.TimeForAnswer)

options, err := ConvertJsonbToDailyQuestionOptions(question.AnswerOptions)
if err != nil {
Expand Down

0 comments on commit b0fe406

Please sign in to comment.