Skip to content

Commit 7d9768b

Browse files
adding create product
1 parent 0e43215 commit 7d9768b

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

ecomm-api/storer/storer_mysql.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@ func NewMySQLStorer(db *sqlx.DB) *MySQLStorer {
1212
}
1313

1414

15-
func (ms *MySQLStorer) CreateProduct (ctx context.Context, p *Product)
15+
func (ms *MySQLStorer) CreateProduct (ctx context.Context, p *Product) (*Product,error) {
16+
17+
res,err := ns.db.NameExecContext (ctx,"INSERT INTO PRODUCTS (name,image,category,description,rating,num_reviews,price,count_in_stock) VALUES (:name,:image,:category,:description,:rating, :num_reviews, :price,:count_in_sock)")
18+
19+
if err !=nil {
20+
return nil, fmt.Error("Error inserting prodcut %w",err)
21+
}
22+
23+
id , err := res.LastInsertId()
24+
if err !=nil {
25+
return nil, fmt.Errorf("Error getting last insert id: %w",err)
26+
}
27+
28+
29+
p.ID = id
30+
31+
return p, nil
32+
}
33+
1634

1735

ecomm-api/storer/types.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package storer
2+
import (
3+
"time"
4+
)
5+
6+
7+
type Product struct {
8+
ID int64 `json:"id" db:"id"`
9+
Name string `json:"name" db:"name:"`
10+
Image string `json:"image" db:"image"`
11+
Category string `json:"category" db:"category"`
12+
Description string `json:"description" db:"description"`
13+
Rating int64 `json:"rating" db:"rating"`
14+
NumReviews int64 `json:"num_reviews" db:"num_reviews"`
15+
Price float64 `json:"price" db:"price"`
16+
CountInStock int64 `json:"count_in_stock" db:"count_in_stock"`
17+
CreatedAt time.Time `json:"created_at" db:"created_at"`
18+
UpdatedAt *time.Time `json:"udated_at" db:"updated_at"`
19+
}

0 commit comments

Comments
 (0)