Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,21 @@ 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")
log.StdError(ctx, nil, nil, "[HelloHandler] Param product_id not exist")
fmt.Fprint(w, "No product id supplied")
return
}

// parse the product id
if len(keys) < 1 {
log.StdFatal(ctx, nil, nil, "No product id found")
log.StdError(ctx, nil, nil, "[HelloHandler] Param product_id is empty")
fmt.Fprint(w, "No product id supplied")
return
}

productID, err = strconv.Atoi(keys[0])
if err != nil {
log.StdFatalf(ctx, nil, nil, "Product id not valid %s", keys[0])
log.StdError(ctx, nil, nil, "[HelloHandler][strconv.Atoi] error parsing keys[0]")
fmt.Fprint(w, "No product id supplied")
return
}
Expand All @@ -68,12 +68,14 @@ func HelloHandler(w http.ResponseWriter, r *http.Request) {

product, err := GetProductFromDB(ctx, productID)
if err != nil {
log.StdError(ctx, productID, err, "[HelloHandler][GetProductFromDB]")
fmt.Fprint(w, "Invalid id")
return
}

err = CalculateDiscount(ctx, product)
if err != nil {
log.StdError(ctx, productID, err, "[HelloHandler][CalculateDiscount]")
fmt.Fprint(w, "Invalid id")
return
}
Expand All @@ -95,7 +97,7 @@ func GetProductFromDB(ctx context.Context, id int) (*external.Product, error) {
func CalculateDiscount(ctx context.Context, p *external.Product) error {
if p.Stock%2 == 0 {
p.Discount = 20
log.StdError(ctx, p, nil, "User get 20 discount")
log.StdDebug(ctx, p, nil, "[CalculateDiscount] User get 20 discount")
} else {
p.Discount = 0
}
Expand Down