-
Notifications
You must be signed in to change notification settings - Fork 0
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
Covid GraphQL APIs #1
base: base_branch
Are you sure you want to change the base?
Conversation
@@ -0,0 +1,399 @@ | |||
package database |
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
context.Context
for database calls: Pass a context through the function calls and use it with theQueryRowContext
andQueryContext
methods to allow for proper cancellation and timeouts. -
Replace
any
withinterface{}
: The[]any
type is not valid in Go. Replace it with[]interface{}
. -
Error handling: Make sure to check the error returned by
rows.Scan()
even inside a loop. This will help in debugging issues if any. -
Use constants for case types: In the
GetTopCountriesByCaseTypeForUser
function, consider using constants for case types like "confirmed", "deaths", and "recovered" instead of string literals. -
Remove
fmt.Println()
calls in production code: Remove debugging calls likefmt.Println()
before deploying the code to production. -
Avoid SQL injections: In the
buildTopCountriesByCaseTypeForUserQuery
function, directly concatenating thecaseType
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 { |
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.
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) |
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.
unhandled errors will go silent causing data issues
@@ -0,0 +1,648 @@ | |||
package api |
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.
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 { |
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.
why did you support this?
*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/ |
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.
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 |
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 can use sql.ErrNoRows
No description provided.