Skip to content
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

feature/user_signup_6 #9

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ require (
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.50.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/text v0.13.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVS
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=
golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM=
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -59,6 +61,8 @@ golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
23 changes: 22 additions & 1 deletion src/controllers/users.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package controllers

import (
"errors"
"onepixel_backend/src/models"

"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
)

Expand All @@ -19,13 +21,32 @@ func NewUsersController(db *gorm.DB) *UsersController {

// Create new user
func (c *UsersController) Create(email string, password string) error {
// Check if email is already registered
existingUser := &models.User{}
result := c.db.Where("email = ?", email).First(existingUser)
lohit97 marked this conversation as resolved.
Show resolved Hide resolved
if result.Error == nil {
return errors.New("email already registered")
}
if !errors.Is(result.Error, gorm.ErrRecordNotFound) {
return result.Error
}

// Hash the password
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
lohit97 marked this conversation as resolved.
Show resolved Hide resolved
return err
}

user := &models.User{
Email: email,
Password: password,
Password: string(hashedPassword),
}

// Save the user to the database
res := c.db.Create(user)
if res.Error != nil {
return res.Error
}

return nil
}
16 changes: 11 additions & 5 deletions src/main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package main

import (
"github.com/samber/lo"
"log"
"onepixel_backend/src/db"
"onepixel_backend/src/server"
)

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

// Initialize the database
lo.Must(db.InitDB())
dbConnection, err := db.InitDB()
if err != nil {
log.Fatalf("Failed to initialize database: %v", err)
lohit97 marked this conversation as resolved.
Show resolved Hide resolved
}

// Pass the dbConnection to CreateApp
app := server.CreateApp(dbConnection)

log.Fatal(app.Listen(":3000"))
// Start the application
if err := app.Listen(":3000"); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}
33 changes: 26 additions & 7 deletions src/routes/api/users.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
package api

import (
"onepixel_backend/src/controllers"
"onepixel_backend/src/dtos"

"github.com/gofiber/fiber/v2"
"github.com/samber/lo"
)

// UsersRoute /api/v1/users
func UsersRoute(router fiber.Router) {
func UsersRoute(router fiber.Router, usersController *controllers.UsersController) {
router.Get("/", getAllUsers)
router.Post("/", registerUser)
router.Post("/", func(c *fiber.Ctx) error {
return registerUser(c, usersController)
lohit97 marked this conversation as resolved.
Show resolved Hide resolved
})
router.Post("/login", loginUser)
}

func registerUser(ctx *fiber.Ctx) error {
var u = new(dtos.CreateUserRequest)
lo.Must0(ctx.BodyParser(u))
func registerUser(ctx *fiber.Ctx, usersController *controllers.UsersController) error {
var u = new(dtos.CreateUserRequest)

return ctx.SendString("RegisterUser")
// Parse incoming JSON request
if err := ctx.BodyParser(u); err != nil {
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Cannot parse JSON"})
}

// Attempt to create a new user
err := usersController.Create(u.Email, u.Password)
if err != nil {
// Check if the email is already registered
if err.Error() == "email already registered" {
return ctx.Status(fiber.StatusConflict).JSON(fiber.Map{"error": "Email already registered"})
}
// Return 500 for all other errors
return ctx.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
}

// Successfully created a new user
return ctx.JSON(fiber.Map{"status": "success"})
}


func loginUser(ctx *fiber.Ctx) error {
return ctx.SendString("LoginUser")
}
Expand Down
14 changes: 11 additions & 3 deletions src/server/server.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
package server

import (
"github.com/gofiber/fiber/v2"
"onepixel_backend/src/controllers"
"onepixel_backend/src/routes/api"

"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
)

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

usersController := controllers.NewUsersController(dbConn)

app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World 👋!")
})

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

apiV1.Route("/users", api.UsersRoute)
apiV1.Route("/users", func(router fiber.Router) {
lohit97 marked this conversation as resolved.
Show resolved Hide resolved
api.UsersRoute(router, usersController)
})

apiV1.Route("/urls", api.UrlsRoute)

return app
Expand Down