...

Source file src/github.com/go-chi/chi/_examples/versions/presenter/v3/article.go

Documentation: github.com/go-chi/chi/_examples/versions/presenter/v3

     1  package v3
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"net/http"
     7  
     8  	"github.com/go-chi/chi/_examples/versions/data"
     9  )
    10  
    11  // Article presented in API version 2.
    12  type Article struct {
    13  	*data.Article `json:",inline" xml:",inline"`
    14  
    15  	// Additional fields.
    16  	URL        string `json:"url" xml:"url"`
    17  	ViewsCount int64  `json:"views_count" xml:"views_count"`
    18  	APIVersion string `json:"api_version" xml:"api_version"`
    19  
    20  	// Omitted fields.
    21  	// Show custom_data explicitly for auth'd users only.
    22  	CustomDataForAuthUsers interface{} `json:"custom_data,omitempty" xml:"custom_data,omitempty"`
    23  }
    24  
    25  func (a *Article) Render(w http.ResponseWriter, r *http.Request) error {
    26  	a.ViewsCount = rand.Int63n(100000)
    27  	a.URL = fmt.Sprintf("http://localhost:3333/v3/?id=%v", a.ID)
    28  
    29  	// Only show to auth'd user.
    30  	if _, ok := r.Context().Value("auth").(bool); ok {
    31  		a.CustomDataForAuthUsers = a.Article.CustomDataForAuthUsers
    32  	}
    33  
    34  	return nil
    35  }
    36  
    37  func NewArticleResponse(article *data.Article) *Article {
    38  	return &Article{Article: article}
    39  }
    40  

View as plain text