Skip to content

Commit

Permalink
add interface DailyQuestionsQ
Browse files Browse the repository at this point in the history
  • Loading branch information
kish1n committed Aug 12, 2024
1 parent 2e16695 commit 64700fc
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 5 deletions.
11 changes: 6 additions & 5 deletions internal/assets/migrations/004_daily_questions_tabel.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
-- +migrate Up
CREATE TABLE IF NOT EXISTS daily_questions (
id SERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
time_for_answer INTEGER NOT NULL,
bounty INTEGER NOT NULL,
answer_options JSONB NOT NULL
id SERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
time_for_answer INTEGER NOT NULL,
bounty INTEGER NOT NULL,
answer_options JSONB NOT NULL,
active BOOLEAN DEFAULT TRUE
);

-- +migrate Down
Expand Down
21 changes: 21 additions & 0 deletions internal/data/daily_questions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package data

type DailyQuestions struct {
ID int `db:"id"`
Title string `db:"title"`
TimeForAnswer int `db:"time_for_answer"`
Bounty int `db:"bounty"`
AnswerOptions string `db:"answer_options"`
Active bool `db:"active"`
}

type DailyQuestionsQ interface {
New() DailyQuestionsQ
Insert(DailyQuestions) error
Update(map[string]any) error

Count() (int64, error)
CountActive() (int64, error)

FilteredActive(status bool) DailyQuestionsQ
}
90 changes: 90 additions & 0 deletions internal/data/pg/daily_questions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package pg

import (
"fmt"

"github.com/Masterminds/squirrel"
"github.com/rarimo/geo-points-svc/internal/data"
"gitlab.com/distributed_lab/kit/pgdb"
)

const dailyQuestionsTable = "daily_questions"

type dailyQuestions struct {
db *pgdb.DB
selector squirrel.SelectBuilder
updater squirrel.UpdateBuilder
counter squirrel.SelectBuilder
}

func NewDailyQuestions(db *pgdb.DB) data.DailyQuestionsQ {
return &dailyQuestions{
db: db,
selector: squirrel.Select("*").From(dailyQuestionsTable),
updater: squirrel.Update(dailyQuestionsTable),
counter: squirrel.Select("COUNT(*) as count").From(dailyQuestionsTable),
}
}

func (q *dailyQuestions) New() data.DailyQuestionsQ {
return NewDailyQuestions(q.db)
}

func (q *dailyQuestions) Insert(quest data.DailyQuestions) error {
stmt := squirrel.Insert(dailyQuestionsTable).SetMap(map[string]interface{}{
"title": quest.Title,
"time_for_answer": quest.TimeForAnswer,
"bounty": quest.Bounty,
"answer_options": quest.AnswerOptions,
"active": quest.Active,
})

if err := q.db.Exec(stmt); err != nil {
return fmt.Errorf("insert daily_questions %+v: %w", quest, err)
}

return nil
}

func (q *dailyQuestions) Update(fields map[string]any) error {
if err := q.db.Exec(q.updater.SetMap(fields)); err != nil {
return fmt.Errorf("update daily_questions: %w", err)
}

return nil
}

func (q *dailyQuestions) Count() (int64, error) {
res := struct {
Count int64 `db:"count"`
}{}

if err := q.db.Get(&res, q.counter); err != nil {
return 0, fmt.Errorf("get daily_questions: %w", err)
}

return res.Count, nil
}

func (q *dailyQuestions) CountActive() (int64, error) {
res := struct {
Count int64 `db:"count"`
}{}

if err := q.db.Get(&res, q.counter.Where("active = TRUE")); err != nil {
return 0, fmt.Errorf("get active daily_questions: %w", err)
}

return res.Count, nil
}

func (q *dailyQuestions) FilteredActive(status bool) data.DailyQuestionsQ {
return q.applyCondition(squirrel.Eq{"active": status})
}

func (q *dailyQuestions) applyCondition(cond squirrel.Sqlizer) data.DailyQuestionsQ {
q.selector = q.selector.Where(cond)
q.updater = q.updater.Where(cond)
q.counter = q.counter.Where(cond)
return q
}
5 changes: 5 additions & 0 deletions internal/service/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ func Run(ctx context.Context, cfg config.Config) {
r.Use(authMW)
r.Post("/", handlers.CreateBalance)
r.Route("/{nullifier}", func(r chi.Router) {
r.Route("/daily_question", func(r chi.Router) {
r.Get("/status", handlers.GetStatusDailyQuestions)
//r.Get("/question", handlers)
//r.Post("/answer", handlers)
})
r.Get("/", handlers.GetBalance)
r.Patch("/", handlers.ActivateBalance)
r.Post("/verifypassport", handlers.VerifyInternalPassport)
Expand Down

0 comments on commit 64700fc

Please sign in to comment.