diff --git a/main.go b/main.go index 0102520..50d2e5c 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( "context" "errors" - "fmt" "math/rand" "net/http" "strconv" @@ -23,7 +22,7 @@ func main() { }) if err != nil { - log.StdInfo(context.Background(), nil, err, "Failed to start Log") + log.StdFatal(context.Background(), nil, err, "Failed to start Log") } http.HandleFunc("/", HelloHandler) @@ -44,22 +43,19 @@ func HelloHandler(w http.ResponseWriter, r *http.Request) { keys, ok := r.URL.Query()["product_id"] if !ok { - log.StdFatal(ctx, nil, nil, "No product id supplied") - fmt.Fprint(w, "No product id supplied") + log.StdError(ctx, nil, err, "No product id supplied") return } // parse the product id if len(keys) < 1 { - log.StdFatal(ctx, nil, nil, "No product id found") - fmt.Fprint(w, "No product id supplied") + log.StdError(ctx, nil, err, "No product id found") return } productID, err = strconv.Atoi(keys[0]) if err != nil { - log.StdFatalf(ctx, nil, nil, "Product id not valid %s", keys[0]) - fmt.Fprint(w, "No product id supplied") + log.StdErrorf(ctx, nil, err, "Product id not valid %s", keys[0]) return } @@ -68,17 +64,13 @@ func HelloHandler(w http.ResponseWriter, r *http.Request) { product, err := GetProductFromDB(ctx, productID) if err != nil { - fmt.Fprint(w, "Invalid id") + log.StdError(ctx, nil, err, "Failed get GetProductFromDB") return } - err = CalculateDiscount(ctx, product) - if err != nil { - fmt.Fprint(w, "Invalid id") - return - } + CalculateDiscount(ctx, product) - fmt.Fprintf(w, "%+v", product) + log.StdDebug(ctx, product, err, "HelloHandler Response") } func GetProductFromDB(ctx context.Context, id int) (*external.Product, error) { @@ -92,12 +84,12 @@ func GetProductFromDB(ctx context.Context, id int) (*external.Product, error) { return &result, nil } -func CalculateDiscount(ctx context.Context, p *external.Product) error { +func CalculateDiscount(ctx context.Context, p *external.Product) { if p.Stock%2 == 0 { p.Discount = 20 - log.StdError(ctx, p, nil, "User get 20 discount") + log.StdDebug(ctx, p, nil, "User get 20 discount") } else { p.Discount = 0 } - return nil + return }