Skip to content

Instantly share code, notes, and snippets.

package polls
import (
"context"
"log"
"net/http"
"testing"
"flamingo.me/flamingo/v3/framework/web"
"github.com/DATA-DOG/go-sqlmock"
"github.com/jinzhu/gorm"
)
package polls
import (
"context"
"strconv"
"flamingo.me/flamingo/v3/framework/web"
"github.com/jinzhu/gorm"
)
type controller struct {
responder *web.Responder
db *gorm.DB
<h1>{{ .Question.QuestionText }}</h1>
<ul>
{{ range $choice := .Question.Choices }}
<li>{{ $choice.ChoiceText }} -- {{ $choice.Votes }} vote(s)</li>
{{ end }}
</ul>
<a href="{{ url "detail" "question_id" (print .Question.ID) }}">Vote again?</a>
//...
func (c *controller) results(ctx context.Context, request *web.Request) web.Result {
questionID, err := strconv.Atoi(request.Params["question_id"])
if err != nil {
return c.responder.ServerError(err)
}
var viewData struct {
Question Question
}
if c.db.Preload("Choices").First(&viewData.Question, questionID).RecordNotFound() {
//...
func (c *controller) vote(ctx context.Context, request *web.Request) web.Result {
questionID, err := strconv.Atoi(request.Params["question_id"])
if err != nil {
return c.responder.ServerError(err)
}
var question Question
if c.db.Preload("Choices").First(&question, questionID).RecordNotFound() {
return c.responder.NotFound(gorm.ErrRecordNotFound)
}
<h1>{{ .Question.QuestionText }}</h1>
{{ if .ErrorMessage }}<p><strong>{{ .ErrorMessage }}</strong></p>{{ end }}
<form action="{{ url "vote" "question_id" (print .Question.ID) }}" method="post">
{{ range $i, $choice := .Question.Choices }}
<input type="radio" name="choice" id="choice{{ $i }}" value="{{ $choice.ID }}">
<label for="choice{{ $i }}">{{ $choice.ChoiceText }}</label><br>
{{end}}
<input type="submit" value="Vote">
</form>
<h1>{{ .Question.QuestionText }}</h1>
//...
func (c *controller) detail(ctx context.Context, request *web.Request) web.Result {
questionID, err := strconv.Atoi(request.Params["question_id"])
if err != nil {
return c.responder.ServerError(err)
}
var viewData struct {
Question Question
// ...
import "github.com/jinzhu/gorm"
type controller struct {
responder *web.Responder
db *gorm.DB
}
func (c *controller) Inject(responder *web.Responder, db *gorm.DB) {
c.responder = responder
{{ if .LatestQuestionList }}
<ul>
{{ range $question := .LatestQuestionList }}
<li><a href="{{ url "detail" "question_id" (print $question.ID) }}">{{ $question.QuestionText }}</a></li>
{{ end }}
</ul>
{{ else }}
<p>No polls available.</p>
{{ end }}