Skip to content

Commit

Permalink
feat: basic working of user controller with user route
Browse files Browse the repository at this point in the history
Signed-off-by: Arnav Gupta <[email protected]>
  • Loading branch information
championswimmer committed Nov 4, 2023
1 parent 031a297 commit 98b54b3
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
.idea/**/detekt.xml
.idea/**/swagger-settings.xml

# AWS User-specific
.idea/**/aws.xml
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ func NewUsersController(db *gorm.DB) *UsersController {
}

// Create new user
func (c *UsersController) Create(email string, password string) error {
func (c *UsersController) Create(email string, password string) (*models.User, error) {
user := &models.User{
Email: email,
Password: password,
Password: password, // TODO: hash password
}
res := c.db.Create(user)
if res.Error != nil {
return res.Error
return nil, res.Error
}
return nil
return user, nil
}
9 changes: 9 additions & 0 deletions src/dtos/http_responses.go
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,
}
}
7 changes: 4 additions & 3 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
)

func main() {
app := server.CreateApp()

// Initialize the database
lo.Must(db.InitDB())
db := lo.Must(db.InitDB())

// Create the app
app := server.CreateApp(db)

log.Fatal(app.Listen(":3000"))
}
4 changes: 3 additions & 1 deletion src/models/users.go
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 {
Expand Down
30 changes: 23 additions & 7 deletions src/routes/api/users.go
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")
}
5 changes: 3 additions & 2 deletions src/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package server

import (
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
"onepixel_backend/src/routes/api"
)

func CreateApp() *fiber.App {
func CreateApp(db *gorm.DB) *fiber.App {
app := fiber.New()

app.Get("/", func(c *fiber.Ctx) error {
Expand All @@ -14,7 +15,7 @@ func CreateApp() *fiber.App {

apiV1 := app.Group("/api/v1")

apiV1.Route("/users", api.UsersRoute)
apiV1.Route("/users", api.UsersRoute(db))
apiV1.Route("/urls", api.UrlsRoute)

return app
Expand Down
9 changes: 0 additions & 9 deletions tests/routes/api/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@ import (

var app = server.CreateApp()

Check failure on line 12 in tests/routes/api/users_test.go

View workflow job for this annotation

GitHub Actions / test

not enough arguments in call to 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"}`)
Expand Down

0 comments on commit 98b54b3

Please sign in to comment.