-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: basic working of user controller with user route
Signed-off-by: Arnav Gupta <[email protected]>
- Loading branch information
1 parent
031a297
commit 98b54b3
Showing
8 changed files
with
48 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
package dtos | ||
|
||
import "onepixel_backend/src/models" | ||
|
||
type UserResponse struct { | ||
ID uint `json:"id"` | ||
Email string `json:"email"` | ||
} | ||
|
||
func UserResponseFromUser(user *models.User) UserResponse { | ||
return UserResponse{ | ||
ID: user.ID, | ||
Email: user.Email, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package models | ||
|
||
import "gorm.io/gorm" | ||
import ( | ||
"gorm.io/gorm" | ||
) | ||
|
||
// User db entity | ||
type User struct { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,46 @@ | ||
package api | ||
|
||
import ( | ||
"gorm.io/gorm" | ||
"onepixel_backend/src/controllers" | ||
"onepixel_backend/src/dtos" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
"github.com/samber/lo" | ||
) | ||
|
||
var usersController *controllers.UsersController | ||
|
||
// UsersRoute /api/v1/users | ||
func UsersRoute(router fiber.Router) { | ||
router.Get("/", getAllUsers) | ||
router.Post("/", registerUser) | ||
router.Post("/login", loginUser) | ||
func UsersRoute(db *gorm.DB) func(router fiber.Router) { | ||
usersController = controllers.NewUsersController(db) | ||
return func(router fiber.Router) { | ||
router.Post("/", registerUser) | ||
router.Post("/login", loginUser) | ||
router.Get("/:id", getUserInfo) | ||
router.Patch("/:id", updateUserInfo) | ||
} | ||
} | ||
|
||
func registerUser(ctx *fiber.Ctx) error { | ||
var u = new(dtos.CreateUserRequest) | ||
// TODO: handle error and show Bad-Request to client | ||
lo.Must0(ctx.BodyParser(u)) | ||
|
||
return ctx.SendString("RegisterUser") | ||
// TODO: handle the case of existing email and show to client | ||
savedUser := lo.Must(usersController.Create(u.Email, u.Password)) | ||
|
||
return ctx.Status(fiber.StatusCreated).JSON(dtos.UserResponseFromUser(savedUser)) | ||
} | ||
|
||
func loginUser(ctx *fiber.Ctx) error { | ||
return ctx.SendString("LoginUser") | ||
} | ||
|
||
func getAllUsers(ctx *fiber.Ctx) error { | ||
return ctx.SendString("GetAllUsers") | ||
func getUserInfo(ctx *fiber.Ctx) error { | ||
return ctx.SendString("GetUserInfo") | ||
} | ||
|
||
func updateUserInfo(ctx *fiber.Ctx) error { | ||
return ctx.SendString("UpdateUserInfo") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,15 +11,6 @@ import ( | |
|
||
var app = server.CreateApp() | ||
|
||
func TestUsersRoute_GetAllUsers(t *testing.T) { | ||
|
||
req := httptest.NewRequest("GET", "/api/v1/users", nil) | ||
resp := lo.Must(app.Test(req)) | ||
|
||
assert.Equal(t, 200, resp.StatusCode) | ||
|
||
} | ||
|
||
func TestUsersRoute_RegisterUser(t *testing.T) { | ||
|
||
reqBody := []byte(`{"email": "[email protected]", "password": "123456"}`) | ||
|