Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions cmd/quiz/start.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,45 @@
package quiz

import (
"fmt"

"github.com/spf13/cobra"
"github.com/vaibhav135/go-quiz-app/pkg/quiz/database"
"github.com/vaibhav135/go-quiz-app/pkg/quiz/gui"
)

var startCmd = &cobra.Command{
Use: "start",
Short: "Start the quiz and check your skillz...",
Args: func(cmd *cobra.Command, args []string) error {

return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
gui.Timer(3)
if timerDuration < 1 {
return fmt.Errorf("\n Timer duration cannot be less than 1 minute \n")
}

if totalQuestions < 5 {
return fmt.Errorf("\n Total questions can't be less than 5 \n")
}

// database.
quizContent := database.QuizInstance.List(totalQuestions)

// Since timeDuration is in seconds we are converting it to minutes.
timerDurationInMinutes := timerDuration * 60
gui.QuizGUI(timerDurationInMinutes, quizContent)

fmt.Println(quizContent)
return nil
},
}

var timer int
var timerDuration, totalQuestions int

func init() {
startCmd.Flags().IntVarP(&timer, "timer", "t", 1, "Timer for the quiz (time is in mins)")
startCmd.Flags().IntVarP(&timer, "totalQuestions", "q", 10, "Total no. of questions you want to have in the quiz")
startCmd.Flags().IntVarP(&timerDuration, "timer", "t", 1, "Timer for the quiz (time is in mins)")
startCmd.Flags().IntVarP(&totalQuestions, "totalQuestions", "q", 10, "Total no. of questions you want to have in the quiz")

rootCmd.AddCommand(startCmd)
}
8 changes: 4 additions & 4 deletions pkg/quiz/constants.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package quiz

const (
Json = "json"
Csv = "csv"
Json = "json"
Csv = "csv"
)

type QuizContent struct {
Question string `json:"question"`
Answer string `json:"answer"`
Question string `json:"question"`
Answer string `json:"answer"`
}
26 changes: 23 additions & 3 deletions pkg/quiz/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"sync"

_ "github.com/mattn/go-sqlite3"
"github.com/vaibhav135/go-quiz-app/pkg/quiz"
TQuiz "github.com/vaibhav135/go-quiz-app/pkg/quiz"
)

type Quiz struct {
Expand Down Expand Up @@ -73,10 +73,30 @@ func create() (*sql.DB, error) {

}

func (quiz *Quiz) List(numberOfQuiz int) {
func (quiz *Quiz) List(numberOfQuiz int) []TQuiz.QuizContent {
rows, err := quiz.DB.Query(quizListQuery, numberOfQuiz)

if err != nil {
log.Panic(err)
}

var quizContent = []TQuiz.QuizContent{}

for rows.Next() {
var (
Question string
Answer string
)
if err := rows.Scan(&Question, &Answer); err != nil {
log.Fatal(err)
}
quizContent = append(quizContent, TQuiz.QuizContent{Question: Question, Answer: Answer})
}

return quizContent
}

func (quiz *Quiz) BulkInsert(quizData []quiz.QuizContent) {
func (quiz *Quiz) BulkInsert(quizData []TQuiz.QuizContent) {
stmt, err := quiz.DB.Prepare(quizInsertQuery)

if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions pkg/quiz/database/query.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package database


var dbSeedQuery = `CREATE TABLE IF NOT EXISTS quiz (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
question TEXT UNIQUE NOT NULL,
answer VARCHAR(100) NOT NULL
)`
)`

var quizInsertQuery = `INSERT INTO quiz(question, answer) VALUES(?, ?)`

var quizListQuery = `SELECT question, answer FROM quiz ORDER BY RANDOM() LIMIT ?`
40 changes: 40 additions & 0 deletions pkg/quiz/gui/quiz_gui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package gui

import (
"github.com/rivo/tview"
"github.com/vaibhav135/go-quiz-app/pkg/quiz"
)

// Ref: https://github.com/rivo/tview/wiki/Grid
func QuizGUI(duration int, quizContent []quiz.QuizContent) {
app = tview.NewApplication()

newPrimitive := func(text string) tview.Primitive {
return tview.NewTextView().
SetTextAlign(tview.AlignCenter).
SetText(text)
}

timerPrimitive := func() tview.Primitive {
return timer(duration)
}

header := timerPrimitive()
main := newPrimitive("")
footer := newPrimitive("")

grid := tview.NewGrid().
SetRows(6, 0, 5).
SetBorders(true)

// Layout for screens narrower than 100 cells (menu and side bar are hidden).
grid.AddItem(header, 0, 0, 1, 3, 0, 0, false).
AddItem(main, 1, 0, 1, 3, 0, 0, false).
AddItem(footer, 2, 0, 1, 3, 0, 0, false)

app.SetRoot(grid, true).SetFocus(grid)

if err := app.Run(); err != nil {
panic(err)
}
}
8 changes: 3 additions & 5 deletions pkg/quiz/gui/timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,12 @@ func refresh() {
}
}

func Timer(durationArg int) {
func timer(durationArg int) tview.Primitive {
duration = durationArg

app = tview.NewApplication()
view = tview.NewBox().SetDrawFunc(drawTime)

go refresh()
if err := app.SetRoot(view, true).Run(); err != nil {
panic(err)
}

return view
}