-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
122 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters