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
package polls | |
import ( | |
"context" | |
"log" | |
"net/http" | |
"testing" | |
"flamingo.me/flamingo/v3/framework/web" | |
"github.com/DATA-DOG/go-sqlmock" | |
"github.com/jinzhu/gorm" | |
) |
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
package polls | |
import ( | |
"context" | |
"strconv" | |
"flamingo.me/flamingo/v3/framework/web" | |
"github.com/jinzhu/gorm" | |
) | |
type controller struct { | |
responder *web.Responder | |
db *gorm.DB |
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
<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> |
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
//... | |
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() { |
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
//... | |
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) | |
} |
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
<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> |
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
<h1>{{ .Question.QuestionText }}</h1> |
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
//... | |
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 |
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
// ... | |
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 |
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
{{ 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 }} |
NewerOlder