github.com/EngineerKamesh/gofullstack@v0.0.0-20180609171605-d41341d7d4ee/volume2/section4/gopherfaceform/endpoints/fetchposts.go (about)

     1  package endpoints
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  
     7  	"github.com/gorilla/mux"
     8  
     9  	"github.com/EngineerKamesh/gofullstack/volume2/section4/gopherfaceform/models/socialmedia"
    10  )
    11  
    12  func FetchPostsEndpoint(w http.ResponseWriter, r *http.Request) {
    13  
    14  	// TODO: Implement fetching posts for a given user
    15  
    16  	// We are going to create some mock data and send it out in JSON
    17  	// format.
    18  
    19  	// We will actually implement this endpoint, when we cover database
    20  	// persistence later in the course.
    21  
    22  	v := mux.Vars(r)
    23  
    24  	if v["username"] == "EngineerKamesh" {
    25  
    26  		mockPosts := make([]socialmedia.Post, 3)
    27  
    28  		post1 := socialmedia.NewPost("EngineerKamesh", socialmedia.Moods["thrilled"], "Go is awesome!", "Check out the Go web site!", "https://golang.org", "/images/gogopher.png", "", []string{"go", "golang", "programming language"})
    29  		post2 := socialmedia.NewPost("EngineerKamesh", socialmedia.Moods["happy"], "Tour of Go", "Check out the Tour of Go!", "https://tour.golang.org", "/images/gogopher.png", "", []string{"go", "golang", "programming language"})
    30  		post3 := socialmedia.NewPost("EngineerKamesh", socialmedia.Moods["hopeful"], "Go Playground", "Check out the Go Playground!", "https://playground.golang.org", "/images/gogopher.png", "", []string{"go", "golang", "programming language"})
    31  
    32  		mockPosts = append(mockPosts, *post1)
    33  		mockPosts = append(mockPosts, *post2)
    34  		mockPosts = append(mockPosts, *post3)
    35  		json.NewEncoder(w).Encode(mockPosts)
    36  
    37  	} else {
    38  		json.NewEncoder(w).Encode(nil)
    39  
    40  	}
    41  
    42  }