diff --git a/controllers/users.go b/controllers/users.go index 83a287e..af494fb 100644 --- a/controllers/users.go +++ b/controllers/users.go @@ -11,39 +11,40 @@ import ( "github.com/uptrace/bun" ) -func GetUserList(ctx *gin.Context, db *bun.DB) { - var users []models.User - err := db.NewSelect().Model(&users).OrderExpr("id ASC").Limit(10).Scan(ctx) - - if err != nil { - ctx.JSON(http.StatusInternalServerError, dtos.UserListResponse{ - Message: "Failed to fetch users: " + err.Error(), - }) - return - } - - if len(users) == 0 { - ctx.JSON(http.StatusNotFound, dtos.UserListResponse{ - Message: "No users found", - }) - return - } - - var dtoUsers []dtos.User - for _, user := range users { - dtoUsers = append(dtoUsers, dtos.User{ - ID: user.ID, - UserName: user.UserName, - Email: user.Email, - CreatedAt: user.CreatedAt, - }) - } - - ctx.JSON(http.StatusOK, dtos.UserListResponse{ - Message: "users fetched successfully", - Data: dtoUsers, - }) -} +// deprecated as of now +// func GetUserList(ctx *gin.Context, db *bun.DB) { +// var users []models.User +// err := db.NewSelect().Model(&users).OrderExpr("id ASC").Limit(10).Scan(ctx) + +// if err != nil { +// ctx.JSON(http.StatusInternalServerError, dtos.UserListResponse{ +// Message: "Failed to fetch users: " + err.Error(), +// }) +// return +// } + +// if len(users) == 0 { +// ctx.JSON(http.StatusNotFound, dtos.UserListResponse{ +// Message: "No users found", +// }) +// return +// } + +// var dtoUsers []dtos.User +// for _, user := range users { +// dtoUsers = append(dtoUsers, dtos.User{ +// ID: user.ID, +// UserName: user.UserName, +// Email: user.Email, +// CreatedAt: user.CreatedAt, +// }) +// } + +// ctx.JSON(http.StatusOK, dtos.UserListResponse{ +// Message: "users fetched successfully", +// Data: dtoUsers, +// }) +// } func GetUserByID(ctx *gin.Context, db *bun.DB) { id := ctx.Param("id") @@ -60,6 +61,7 @@ func GetUserByID(ctx *gin.Context, db *bun.DB) { dtoUser := dtos.User{ ID: user.ID, + UserName: user.UserName, Email: user.Email, CreatedAt: user.CreatedAt, } diff --git a/routes/users.go b/routes/users.go index 70e276d..39bc8a1 100644 --- a/routes/users.go +++ b/routes/users.go @@ -1,10 +1,10 @@ package routes import ( - "github.com/gin-gonic/gin" - "github.com/uptrace/bun" - "github.com/Real-Dev-Squad/tiny-site-backend/controllers" - "github.com/Real-Dev-Squad/tiny-site-backend/middlewares" + controller "github.com/Real-Dev-Squad/tiny-site-backend/controllers" + middleware "github.com/Real-Dev-Squad/tiny-site-backend/middlewares" + "github.com/gin-gonic/gin" + "github.com/uptrace/bun" ) func UserRoutes(rg *gin.RouterGroup, db *bun.DB) { @@ -14,9 +14,9 @@ func UserRoutes(rg *gin.RouterGroup, db *bun.DB) { users.Use(middleware.AuthMiddleware()) user.Use(middleware.AuthMiddleware()) - users.GET("", func(ctx *gin.Context) { - controller.GetUserList(ctx, db) - }) + // users.GET("", func(ctx *gin.Context) { + // controller.GetUserList(ctx, db) + // }) users.GET("/:id", func(ctx *gin.Context) { controller.GetUserByID(ctx, db) diff --git a/tests/integration/users_test.go b/tests/integration/users_test.go index 14b56a8..4678128 100644 --- a/tests/integration/users_test.go +++ b/tests/integration/users_test.go @@ -10,20 +10,20 @@ import ( ) // TestGetUsersSuccess tests the successful retrieval of a list of users. -func (suite *AppTestSuite) TestGetUsersSuccess() { - // Setup the router and route - router := gin.Default() - router.GET("/v1/users", func(ctx *gin.Context) { - controller.GetUserList(ctx, suite.db) - }) - - // Create a request and recorder to test the endpoint - req, _ := http.NewRequest("GET", "/v1/users", nil) - w := httptest.NewRecorder() - - router.ServeHTTP(w, req) - assert.Equal(suite.T(), http.StatusOK, w.Code, "Expected status code to be 200 for successful user retrieval") -} +// func (suite *AppTestSuite) TestGetUsersSuccess() { +// // Setup the router and route +// router := gin.Default() +// router.GET("/v1/users", func(ctx *gin.Context) { +// controller.GetUserList(ctx, suite.db) +// }) + +// // Create a request and recorder to test the endpoint +// req, _ := http.NewRequest("GET", "/v1/users", nil) +// w := httptest.NewRecorder() + +// router.ServeHTTP(w, req) +// assert.Equal(suite.T(), http.StatusOK, w.Code, "Expected status code to be 200 for successful user retrieval") +// } // TestGetUserByIDExistingUser tests the retrieval of a user by ID for an existing user and expects a successful response. func (suite *AppTestSuite) TestGetUserByIDExistingUser() {