Esempio n. 1
0
func GetAllPets(w http.ResponseWriter, r *http.Request) {
	jsonapiRuntime := jsonapi.NewRuntime().Instrument("/")
	w.WriteHeader(200)
	w.Header().Set("Content-Type", "application/vnd.api+json")
	pets := AllPets()
	if err := jsonapiRuntime.MarshalManyPayload(w, pets); err != nil {
		http.Error(w, err.Error(), 500)
	}
}
Esempio n. 2
0
func CreateNewPet(w http.ResponseWriter, r *http.Request) {
	jsonapiRuntime := jsonapi.NewRuntime().Instrument("/")
	pet := new(Pet)
	if err := jsonapiRuntime.UnmarshalPayload(r.Body, pet); err != nil {
		http.Error(w, err.Error(), 500)
		return
	}
	NewPet(pet)
	w.WriteHeader(201)
	w.Header().Set("Content-Type", "application/vnd.api+json")
}
Esempio n. 3
0
func listBlogs(w http.ResponseWriter, r *http.Request) {
	jsonapiRuntime := jsonapi.NewRuntime().Instrument("blogs.list")
	// ...fetch your blogs, filter, offset, limit, etc...

	// but, for now
	blogs := testBlogsForList()

	w.WriteHeader(200)
	w.Header().Set("Content-Type", "application/vnd.api+json")
	if err := jsonapiRuntime.MarshalManyPayload(w, blogs); err != nil {
		http.Error(w, err.Error(), 500)
	}
}
Esempio n. 4
0
func GetOnePet(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id, err := strconv.Atoi(vars["id"])
	if err != nil {
		fmt.Println("Invalid pet id")
		return
	}
	jsonapiRuntime := jsonapi.NewRuntime().Instrument("/")
	w.WriteHeader(200)
	w.Header().Set("Content-Type", "application/vnd.api+json")
	pet := ReadPet(&Pet{Id: id})
	if err := jsonapiRuntime.MarshalOnePayload(w, pet); err != nil {
		http.Error(w, err.Error(), 500)
	}
}
// GetAll retrieves all adventure resources
func (ac AdventureController) GetAll(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	jsonapiRuntime := jsonapi.NewRuntime().Instrument("adventures.get-all")

	as := models.AdventureTests
	asInterface := make([]interface{}, len(as))

	for i, a := range as {
		asInterface[i] = a
	}

	w.Header().Set("Content-Type", "application/vnd.api+json")
	w.WriteHeader(http.StatusOK)

	if err := jsonapiRuntime.MarshalManyPayload(w, asInterface); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}
Esempio n. 6
0
func createBlog(w http.ResponseWriter, r *http.Request) {
	jsonapiRuntime := jsonapi.NewRuntime().Instrument("blogs.create")

	blog := new(Blog)

	if err := jsonapiRuntime.UnmarshalPayload(r.Body, blog); err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	// ...do stuff with your blog...

	w.WriteHeader(201)
	w.Header().Set("Content-Type", "application/vnd.api+json")

	if err := jsonapiRuntime.MarshalOnePayload(w, blog); err != nil {
		http.Error(w, err.Error(), 500)
	}
}
// Get retrieves an individual adventure resource
func (ac AdventureController) Get(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	jsonapiRuntime := jsonapi.NewRuntime().Instrument("adventures.get")

	// Stub an example adventure
	aID, _ := strconv.Atoi(p.ByName("id"))
	a := models.Adventure{}

	for _, adventure := range models.AdventureTests {
		if aID == adventure.ID {
			a = *adventure
			break
		}
	}

	// Write content-type, statuscode, payload
	w.Header().Set("Content-Type", "application/vnd.api+json")
	w.WriteHeader(http.StatusOK)

	if err := jsonapiRuntime.MarshalOnePayload(w, &a); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}
Esempio n. 8
0
func showBlog(w http.ResponseWriter, r *http.Request) {
	id := r.FormValue("id")

	// ...fetch your blog...

	intId, err := strconv.Atoi(id)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	jsonapiRuntime := jsonapi.NewRuntime().Instrument("blogs.show")

	// but, for now
	blog := testBlogForCreate(intId)
	w.WriteHeader(200)

	w.Header().Set("Content-Type", "application/vnd.api+json")
	if err := jsonapiRuntime.MarshalOnePayload(w, blog); err != nil {
		http.Error(w, err.Error(), 500)
	}
}