Skip to content
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

Go-challenge implementation #17

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open

Conversation

arhsxro
Copy link

@arhsxro arhsxro commented Jul 9, 2024

My implementation for the platform-go-challenge.

@wiz-gwi
Copy link

wiz-gwi bot commented Jul 9, 2024

Wiz Scan Summary

IaC Misconfigurations 0C 1H 1M 1L 0I
Vulnerabilities 0C 0H 0M 0L 0I
Sensitive Data 0C 0H 0M 0L 0I
Total 0C 1H 1M 1L 0I
Secrets 0🔑

@nmakro
Copy link

nmakro commented Jul 12, 2024

Hi @arhsxro,

Thank you very much for your time and effort.
I have put some comments in the PR that you can address if you like.
Have a nice day!

Comment on lines +20 to +23
GetUserFavoritesFunc func(ctx context.Context, userID, filterType string, page, pageSize int) ([]models.Asset, error)
AddFavoriteFunc func(ctx context.Context, userID string, asset models.Asset) error
RemoveFavoriteFunc func(ctx context.Context, userID, assetId string) error
UpdateDescriptionFunc func(ctx context.Context, userID, assetID, newDescription string) error
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice way of mocking!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the feedback!The truth is I spend a lot of time in tests.

db *sqlx.DB
}

func isValidAssetType(paramType string) bool {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice that you added a validation. Can you think of a better a way that this could be handled?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could probably move the validation to my handler in order not to proceed more if there is a validation err.
Also I could use a map instead of slice to avoid the loop.


// Adds a new favorite asset for a user in the database
func (store *PostgresStore) AddFavorite(ctx context.Context, userID string, asset models.Asset) error {
tx, err := store.db.Begin() //begin transaction
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you describe why you chose to use transactions?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The truth is that I wasn't 100% sure if i should use a transaction since it only adds/delete/edit a single asset to the db.Allthough i thought that maybe i get at the same time 2 requests where the first 1 is to delete an asset and the second one is to edit the same asset.So in this case with the use of transaction they do not interfere with each other, maintaining the integrity of the data.Another scenario could be that 1 request is to get the favorite assets and the 2 request to add an asset.

api/handlers.go Outdated
Comment on lines 156 to 159
go func() {
wg.Wait()
close(errCh)
}()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain the use of this goroutine?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically in this goroutine i wait for all the goroutines that add an asset to finish in order to close the error channel.By doing so i can then check for any errors in the channel

http.Error(w, "Request timed out", http.StatusGatewayTimeout)
} else {
log.Println("Error on executing the query for asset ID ", assetErr.Asset.ID+" and error message: ", assetErr.Err)
http.Error(w, assetErr.Err.Error(), http.StatusInternalServerError)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The information that this error has, what could it be?
Is it safe to send to the outside world these kind of messages?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this : assetErr.Err.Error() in the http.Error should not be displayed.It may contains more detailed information about what happened internally.(maybe information for our db or some other sensitive informaiton).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants