hashicorp-demoapp / product-api-go

Products API written in Go
Mozilla Public License 2.0
42 stars 26 forks source link

Unable to get a newly created coffee. #31

Open jamime opened 1 year ago

jamime commented 1 year ago

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. https://github.com/hashicorp-demoapp/product-api-go/blob/f7ef7a6288dd2bba69054b4ece7e751f359a3a30/data/connection.go#L430-L431

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"

https://github.com/hashicorp-demoapp/product-api-go/blob/f7ef7a6288dd2bba69054b4ece7e751f359a3a30/data/connection.go#L59

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