-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
73 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ build: | |
# clear | ||
# ./server | ||
|
||
run: build | ||
run: | ||
clear | ||
go run cmd/main.go | ||
|
||
|
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,7 +1,30 @@ | ||
package user | ||
|
||
import "github.com/0xsenzel/go-fiber-boilerplate/internal/services/user" | ||
import ( | ||
"log" | ||
|
||
func HelloWorldHandler() string { | ||
return user.GetHellowWorld() | ||
"github.com/0xsenzel/go-fiber-boilerplate/internal/services/user/models" | ||
"github.com/0xsenzel/go-fiber-boilerplate/internal/services/user/service" | ||
"github.com/gofiber/fiber/v3" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func CreateUserHandler(c fiber.Ctx, db *gorm.DB) error { | ||
var userRequestDto models.UserRequestDto | ||
if err := c.Bind().Body(&userRequestDto); err != nil { | ||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ | ||
"error": true, | ||
"message": "Invalid request payload", | ||
}) | ||
} | ||
log.Printf("Creating user with data:%+v", userRequestDto) | ||
|
||
createdUser, err := service.CreateUser(db, userRequestDto) | ||
if err != nil { | ||
c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error(), "message": "Failed to create user"}) | ||
return err | ||
} | ||
|
||
c.Status(fiber.StatusCreated).JSON(fiber.Map{"message": `User created successfully`, "user": createdUser}) | ||
return nil | ||
} |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package models | ||
|
||
type UserRequestDto struct { | ||
Name string `json:"name"` | ||
Email string `json:"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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package service | ||
|
||
import ( | ||
"github.com/0xsenzel/go-fiber-boilerplate/internal/services/user/models" | ||
"github.com/0xsenzel/go-fiber-boilerplate/internal/services/user/tables" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func CreateUser(db *gorm.DB, userRequestDto models.UserRequestDto) (*tables.User, error) { | ||
user := &tables.User{ | ||
Name: userRequestDto.Name, | ||
Email: userRequestDto.Email, | ||
} | ||
|
||
err := db.Create(user).Error | ||
if err!= nil { | ||
return nil, err | ||
} | ||
|
||
return user, nil | ||
} | ||
|
This file was deleted.
Oops, something went wrong.
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