Skip to content

Commit

Permalink
Merge pull request #134 from Real-Dev-Squad/develop
Browse files Browse the repository at this point in the history
Dev to Main sync
  • Loading branch information
iamitprakash authored Oct 11, 2024
2 parents 54b77e8 + 75af86c commit 1eb7eed
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 62 deletions.
34 changes: 0 additions & 34 deletions controllers/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,6 @@ 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,
})
}

func GetUserByID(ctx *gin.Context, db *bun.DB) {
id := ctx.Param("id")

Expand Down
4 changes: 0 additions & 4 deletions dtos/http_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ type URLNotFoundResponse struct {
Message string `json:"message"`
}

type UserListResponse struct {
Message string `json:"message"`
Data []User `json:"data,omitempty"`
}
type UserResponse struct {
Message string `json:"message"`
Data User `json:"data,omitempty"`
Expand Down
12 changes: 4 additions & 8 deletions routes/users.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -14,10 +14,6 @@ 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("/:id", func(ctx *gin.Context) {
controller.GetUserByID(ctx, db)
})
Expand Down
16 changes: 0 additions & 16 deletions tests/integration/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,6 @@ import (
"github.com/stretchr/testify/assert"
)

// 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")
}

// TestGetUserByIDExistingUser tests the retrieval of a user by ID for an existing user and expects a successful response.
func (suite *AppTestSuite) TestGetUserByIDExistingUser() {
router := gin.Default()
Expand Down

0 comments on commit 1eb7eed

Please sign in to comment.