-
Notifications
You must be signed in to change notification settings - Fork 70
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
feat: Implement GET endpoint to fetch URLs for a given user and admin access #60
base: main
Are you sure you want to change the base?
feat: Implement GET endpoint to fetch URLs for a given user and admin access #60
Conversation
src/routes/api/urls.go
Outdated
@@ -22,6 +24,7 @@ func UrlsRoute() func(router fiber.Router) { | |||
|
|||
return func(router fiber.Router) { | |||
router.Get("/", getAllUrls) | |||
router.Get("/admin", security.MandatoryAdminApiKeyAuthMiddleware, getAllUrlsAdmin) |
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.
i was preferring not to have /admin/urls
or /urls/admin
separately but simply use the /urls
endpoint with the X-API-Key
to work for admins.
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.
Sure I'll do that
src/controllers/urls.go
Outdated
"math" | ||
"math/rand" | ||
"onepixel_backend/src/db" | ||
"onepixel_backend/src/db/models" | ||
"onepixel_backend/src/utils" | ||
"onepixel_backend/src/utils/applogger" | ||
"sync" | ||
|
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 the juggling of imports? needed?
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.
Whenever I save this file, it automatically juggles. As there are no spaces between them, it's prolly done by go itself.
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.
ah ok that's fine then
func getAllUrls(ctx *fiber.Ctx) error { | ||
return ctx.SendString("GetAllUsers") | ||
apiKey := ctx.Get("X-API-Key") |
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.
can we fail fast if neither JWT nor API Key is given?
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.
Sure, I'll do the same
src/controllers/users.go
Outdated
@@ -106,3 +107,12 @@ func (c *UsersController) VerifyEmailAndPassword(email string, password string) | |||
} | |||
return user, nil | |||
} | |||
|
|||
func (c *UrlsController) GetUrlsByUserId(userId uint64) ([]models.Url, 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.
getting URLs should not be happening in Users controller, it should happen in Urls Controller only
src/routes/api/urls.go
Outdated
userId = &user.ID | ||
} | ||
|
||
var urls []models.Url |
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.
what is the difference in the if condition here ?
and why do we want it to be different ?
if the userId came from LOCALS or it came from query (in case of admin) how does it differ? the logic for fetching the URLs will be exactly same in both those cases.
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.
Sir, top level JWT/X-API-KEY if() check is wrong.
-
But in isAdmin; here we can't check
userId
at LOCALS (and in query theuserId
will be string so we've to convert it) and vise-versa in users case(here user.ID is integer). So that's why it was a good approach to separate them using if-else: -
what we can add now is"
- If
userIdStr
is empty or invalid,userId
is explicitly set tonil
. - have a new if-else check after this completes which will check :
- if userId != null then
GetUrlsByUserId(*userId)
//works for both admin and users
elseGetAllUrls(nil)
//only work for admin to get all urls.
- If
-
This will maintain the userId consistency and also it checks the null values.
@championswimmer is this code works ? |
PR added:
Resolve #[#59]
Checklist before requesting a review