-
Notifications
You must be signed in to change notification settings - Fork 4
Goapi get user cart #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: development
Are you sure you want to change the base?
Conversation
wsushmita
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@deepalipawade I have added some comments , pls do requested changes
config/config.go
Outdated
|
|
||
| // JWTExpiryDurationHours - returns duration for jwt expiry in int | ||
| func JWTExpiryDurationHours() int { | ||
| return int(ReadEnvInt("JWT_EXPIRY_DURATION_HOURS")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ReadEnvInt returns int , here you don't need to use int()
db/cart.go
Outdated
| var pids, quantities []int | ||
| var products []Product | ||
|
|
||
| err = s.db.Select(&pids, getCartQuery, user_id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- handle err != nil , log error & return
- Why are you using two query to get data from two tables? add a single query to fetch data from cart
db/cart.go
Outdated
| } | ||
|
|
||
| query, args, err := sqlx.In(getProductsQuery, pids) | ||
| query = s.db.Rebind(query) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here don't use sqlx.In with rebind .You can use exec() to fire any query
db/cart.go
Outdated
| for index, product := range products { | ||
| var category, image_urls []string | ||
| err = s.db.Select(&category, getCategoryQuery, product.CategoryId) | ||
| err = s.db.Select(&image_urls, getProductImageQuery, product.Id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use join to get details from other table
db/cart.go
Outdated
| logger.WithField("err", err.Error()).Error("Error listing cart") | ||
| return | ||
| } | ||
| fmt.Println(category, image_urls, index) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
always remove fmt
service/cart_http.go
Outdated
| // @Failure 400 {object} | ||
| func getCartHandler(deps Dependencies) http.HandlerFunc { | ||
| return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { | ||
| // request_params := mux.Vars(req) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove commented code
service/user_http.go
Outdated
| func listUsersHandler(deps Dependencies) http.HandlerFunc { | ||
| return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { | ||
| users, err := deps.Store.ListUsers(req.Context()) | ||
| fmt.Println(users) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove fmt
service/cart_http.go
Outdated
| cart_products, err := deps.Store.GetCart(req.Context(), int(userID)) | ||
| if err != nil { | ||
| logger.WithField("err", err.Error()).Error("Error fetching data") | ||
| rw.WriteHeader(http.StatusInternalServerError) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return error in json response
db/cart.go
Outdated
| return | ||
| } | ||
|
|
||
| for _, row := range joinCartProduct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are fetching category id for every record, you can join multiple tables using diff where clauses
db/cart.go
Outdated
| return | ||
| } | ||
|
|
||
| err = s.db.Select(&image_urls, getProductImageQuery, row.ProductId) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sane here , you can use join
20af582 to
8a4496f
Compare
Tasks to which I contributed and file names
. Create Schema for Cart -
migrations/1599535672_create_cart.up.sql
migrations/1599535672_create_cart.down.sql
Create Hanlder for Cart -
service/cart_http.go
service/cart_http_test.go
service/common_http_test.go
Database Interaction for Cart -
db/cart.go
db/cart_test.go
db/common_test.go
Added GetCart as Storer Interface method -
db/db.go
Added route for cart -
service/router.go