hashicorp-demoapp / product-api-go

Products API written in Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to get a newly created coffee.

jamime opened this issue · comments

After following the tutorial I decided to implement my own resource to create a coffee.

I installed the latest client which includes support for client.CreateCoffee.

When creating a new coffee the api does not accept collection, origin or color.

`INSERT INTO coffees (name, teaser, description, price, image, created_at, updated_at)
VALUES(:name, :teaser, :description, :price, :image, now(), now())

When attempting to get the new coffee the following error is raised by the api because the db fields are NULL.

docker_compose-api-1  | 2023-01-06T15:21:53.779Z [ERROR] Unable to get products from database: error="sql: Scan error on column index 3, name "collection": converting NULL to string is unsupported"

err := c.db.Select(&cos, "SELECT * FROM coffees WHERE id = $1", &coffeeid)

Example

go get github.com/hashicorp-demoapp/hashicups-client-go@bd8bf734c61cc7f3863e53b2d6492d3b34797118
// hashicups/debug/debug.go
package main

import (
	"encoding/json"
	"fmt"
	"strconv"

	"github.com/hashicorp-demoapp/hashicups-client-go"
)

func main() {
	username := "education"
	password := "test123"
	c, err := hashicups.NewClient(nil, &username, &password)
	if err != nil {
		fmt.Println(err)
		return
	}

	coffee := hashicups.Coffee{
		Name:        "name",
		Collection:  "collection",
		Origin:      "origin",
		Color:       "color",
		Price:       500,
		Description: "description",
	}

	res, err := c.CreateCoffee(coffee, nil)

	if err != nil {
		fmt.Println(err)
		return
	}

	tmp, _ := json.Marshal(res)
	fmt.Println(string(tmp))

	res2, err := c.GetCoffee(strconv.Itoa(res.ID))

	if err != nil {
		fmt.Println(err)
		return
	}

	tmp, _ = json.Marshal(res2)
	fmt.Println(string(tmp))

}
go run hashicups/debug/debug.go
{"id":27,"name":"","teaser":"","collection":"","origin":"","color":"","description":"","price":0,"image":"","ingredients":null}
status: 500, body: Unable to list products