-
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?
Changes from 1 commit
d67467c
e9ec78a
42ebf6e
3769123
9625ad7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,15 @@ package controllers | |
|
||
import ( | ||
"errors" | ||
"github.com/google/uuid" | ||
"gorm.io/gorm" | ||
"onepixel_backend/src/config" | ||
"onepixel_backend/src/db" | ||
"onepixel_backend/src/db/models" | ||
"onepixel_backend/src/security" | ||
"onepixel_backend/src/utils/applogger" | ||
"sync" | ||
|
||
"github.com/google/uuid" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type AuthError struct { | ||
|
@@ -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 commentThe 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 |
||
var urls []models.Url | ||
res := c.db.Where("creator_id =?", userId).Find(&urls) | ||
if res.Error != nil { | ||
return nil, res.Error | ||
} | ||
return urls, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,8 @@ import ( | |
"onepixel_backend/src/security" | ||
"onepixel_backend/src/server/parsers" | ||
"onepixel_backend/src/server/validators" | ||
"onepixel_backend/src/utils/applogger" | ||
"strconv" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. i was preferring not to have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure I'll do that |
||
router.Post("/", security.MandatoryJwtAuthMiddleware, createRandomUrl) | ||
router.Put("/:shortcode", security.MandatoryJwtAuthMiddleware, createSpecificUrl) | ||
} | ||
|
@@ -38,7 +41,13 @@ func UrlsRoute() func(router fiber.Router) { | |
// @Router /urls [get] | ||
// @security BearerToken | ||
func getAllUrls(ctx *fiber.Ctx) error { | ||
return ctx.SendString("GetAllUsers") | ||
user := ctx.Locals(config.LOCALS_USER).(*models.User) | ||
|
||
urls, err := urlsController.GetUrlsByUserId(user.ID) | ||
if err != nil { | ||
return ctx.Status(fiber.StatusInternalServerError).JSON(dtos.CreateErrorResponse(fiber.StatusInternalServerError, "Failed to fetch URLs")) | ||
} | ||
return ctx.Status(fiber.StatusOK).JSON(dtos.CreateUrlsResponse(urls)) | ||
} | ||
|
||
// createRandomUrl | ||
|
@@ -137,3 +146,45 @@ func createGroupedRandomUrl(ctx *fiber.Ctx) error { | |
func createGroupedSpecificUrl(ctx *fiber.Ctx) error { | ||
return ctx.SendString("createGroupedSpecificUrl") | ||
} | ||
|
||
// getAllUrlsAdmin | ||
// | ||
// @Summary Get all URLs (admin only) | ||
// @Description Get all URLs with optional user ID filter (admin access required) | ||
// @Tags admin, urls | ||
// @Accept json | ||
// @Produce json | ||
// @Param X-API-Key header string true "Admin API Key" | ||
// @Param userid query string false "Filter URLs by user ID" | ||
// @Success 200 {array} dtos.UrlResponse | ||
// @Failure 400 {object} dtos.ErrorResponse "Invalid user ID" | ||
// @Failure 401 {object} dtos.ErrorResponse "Unauthorized" | ||
// @Failure 500 {object} dtos.ErrorResponse "Failed to fetch URLs" | ||
// @Router /urls/admin [get] | ||
// @Security ApiKeyAuth | ||
func getAllUrlsAdmin(ctx *fiber.Ctx) error { | ||
userIdStr := ctx.Query("userid") | ||
|
||
var userId *uint64 | ||
if userIdStr != "" { | ||
parsedId, err := strconv.ParseUint(userIdStr, 10, 64) | ||
if err != nil { | ||
applogger.Error("Invalid user ID in admin URL request", "user_id", userIdStr, "error", err) | ||
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{ | ||
"message": "Invalid user ID", | ||
}) | ||
} | ||
userId = &parsedId | ||
} | ||
|
||
urls, err := urlsController.GetAllUrls(userId) | ||
if err != nil { | ||
applogger.Error("Failed to fetch URL in admin request", "user_id", userId, "error", err) | ||
return ctx.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ | ||
"message": "Failed to fetch URLs", | ||
}) | ||
} | ||
|
||
applogger.Info("Admin fetched URLs", "user_id", userId, "url_count", len(urls)) | ||
return ctx.Status(fiber.StatusOK).JSON(dtos.CreateUrlsResponse(urls)) | ||
} |
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