Skip to content

Conversation

@awartani
Copy link
Member

No description provided.

@@ -0,0 +1,399 @@
package database
Copy link
Member Author

Choose a reason for hiding this comment

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

  1. Use context.Context for database calls: Pass a context through the function calls and use it with the QueryRowContext and QueryContext methods to allow for proper cancellation and timeouts.

  2. Replace any with interface{}: The []any type is not valid in Go. Replace it with []interface{}.

  3. Error handling: Make sure to check the error returned by rows.Scan() even inside a loop. This will help in debugging issues if any.

  4. Use constants for case types: In the GetTopCountriesByCaseTypeForUser function, consider using constants for case types like "confirmed", "deaths", and "recovered" instead of string literals.

  5. Remove fmt.Println() calls in production code: Remove debugging calls like fmt.Println() before deploying the code to production.

  6. Avoid SQL injections: In the buildTopCountriesByCaseTypeForUserQuery function, directly concatenating the caseType string into the SQL query string might lead to SQL injection. Validate the input

return data, nil
}

func handleRateLimitingError(resp *http.Response, countryName string, status string) error {
Copy link
Member Author

Choose a reason for hiding this comment

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

it has a side effect of calling the FetchDailyDataForCountry function recursively. This may cause issues with the stack as retries increase. I would suggest refactoring it to return a boolean indicating whether a retry is needed and handling the retry in the FetchDailyDataForCountry function itself.

for _, country := range countries {
confirmedData, err := FetchDailyDataForCountry(country.Name, "confirmed")
if err != nil {
log.Printf("Error fetching confirmed data for country %s: %v", country.Name, err)
Copy link
Member Author

Choose a reason for hiding this comment

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

unhandled errors will go silent causing data issues

@@ -0,0 +1,648 @@
package api
Copy link
Member Author

Choose a reason for hiding this comment

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

the code can further improved by separating the API logic from the business logic. Consider creating separate files for the logic of each resource (e.g., users, countries, covid statistics) and then import them into the api.go file. This will make it easier to maintain and understand the codebase.

}
}

func AddCovidStatisticHandler(db *sql.DB) http.HandlerFunc {
Copy link
Member Author

Choose a reason for hiding this comment

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

why did you support this?

## To use the GraphQL UI to see all of the documentations for each query, head to `http://localhost:8080/` to see the UI. However, to interact with the API, you'll need to use `http://localhost:8080/query`
*Note: Keep in mind you'll need to provide authorization when using this approach to send requests.

## To use the REST API, you can use the following URLs to query the API by navigating to /api/
Copy link
Member Author

Choose a reason for hiding this comment

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

why did you create REST APIs and graphql support?

getCountryQuery := "SELECT id FROM countries WHERE name = ?"
row := db.QueryRow(getCountryQuery, name)
err := row.Scan(&id)
return err == nil
Copy link
Member Author

Choose a reason for hiding this comment

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

you can use sql.ErrNoRows

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.

3 participants