Skip to content

Commit a40733c

Browse files
committed
user view
1 parent a08044a commit a40733c

File tree

8 files changed

+63
-17
lines changed

8 files changed

+63
-17
lines changed

main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"github.com/gin-gonic/gin"
77
"github.com/joho/godotenv"
88
"github.com/murillolamego/golang-basic/src/config/logger"
9+
"github.com/murillolamego/golang-basic/src/controller"
910
"github.com/murillolamego/golang-basic/src/controller/routes"
11+
"github.com/murillolamego/golang-basic/src/model/service"
1012
)
1113

1214
func main() {
@@ -16,9 +18,12 @@ func main() {
1618
log.Fatal("Could not load env file")
1719
}
1820

21+
service := service.NewUserDomainService()
22+
userController := controller.NewUserControllerInterface(service)
23+
1924
router := gin.Default()
2025

21-
routes.InitRoutes(&router.RouterGroup)
26+
routes.InitRoutes(&router.RouterGroup, userController)
2227

2328
if err := router.Run(":8080"); err != nil {
2429
log.Fatal(err)

src/controller/create_user.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ import (
88
"github.com/murillolamego/golang-basic/src/config/validation"
99
"github.com/murillolamego/golang-basic/src/controller/model/request"
1010
"github.com/murillolamego/golang-basic/src/model"
11-
"github.com/murillolamego/golang-basic/src/model/service"
11+
"github.com/murillolamego/golang-basic/src/view"
1212
"go.uber.org/zap"
1313
)
1414

1515
var (
1616
UserDomainInterface model.UserDomainInterface
1717
)
1818

19-
func CreateUser(c *gin.Context) {
19+
func (uc *userControllerInterface) CreateUser(c *gin.Context) {
2020
logger.Info("init CreateUser controller", zap.String("journey", "createUser"))
2121
var userRequest request.UserRequest
2222

@@ -35,15 +35,13 @@ func CreateUser(c *gin.Context) {
3535
userRequest.Age,
3636
)
3737

38-
service := service.NewUserDomainService()
39-
40-
user, err := service.CreateUser(domain)
38+
user, err := uc.service.CreateUser(domain)
4139
if err != nil {
4240
c.JSON(err.Code, err)
4341
return
4442
}
4543

4644
logger.Info("user created successfully", zap.String("journey", "createUser"))
4745

48-
c.JSON(http.StatusCreated, user)
46+
c.JSON(http.StatusCreated, view.ConvertDomainToResponse(user))
4947
}

src/controller/delete_user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ package controller
22

33
import "github.com/gin-gonic/gin"
44

5-
func DeleteUser(c *gin.Context) {}
5+
func (uc *userControllerInterface) DeleteUser(c *gin.Context) {}

src/controller/find_user.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ package controller
22

33
import "github.com/gin-gonic/gin"
44

5-
func FindUserById(c *gin.Context) {}
5+
func (uc *userControllerInterface) FindUserByID(c *gin.Context) {}
66

7-
func FindUserByEmail(c *gin.Context) {}
7+
func (uc *userControllerInterface) FindUserByEmail(c *gin.Context) {}

src/controller/routes/routes.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import (
55
"github.com/murillolamego/golang-basic/src/controller"
66
)
77

8-
func InitRoutes(r *gin.RouterGroup) {
8+
func InitRoutes(r *gin.RouterGroup, userController controller.UserControllerInterface) {
99

10-
r.GET("/getUserById/:userId", controller.FindUserById)
11-
r.GET("/getUserByEmail/:userEmail", controller.FindUserByEmail)
12-
r.POST("/createUser", controller.CreateUser)
13-
r.PUT("/updateUser/:userId", controller.UpdateUser)
14-
r.DELETE("/deleteUser/:userId", controller.DeleteUser)
10+
r.GET("/getUserById/:userId", userController.FindUserByID)
11+
r.GET("/getUserByEmail/:userEmail", userController.FindUserByEmail)
12+
r.POST("/createUser", userController.CreateUser)
13+
r.PUT("/updateUser/:userId", userController.UpdateUser)
14+
r.DELETE("/deleteUser/:userId", userController.DeleteUser)
1515
}

src/controller/update_user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ package controller
22

33
import "github.com/gin-gonic/gin"
44

5-
func UpdateUser(c *gin.Context) {}
5+
func (uc *userControllerInterface) UpdateUser(c *gin.Context) {}

src/controller/user_controller.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package controller
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
"github.com/murillolamego/golang-basic/src/model/service"
6+
)
7+
8+
func NewUserControllerInterface(
9+
serviceInterface service.UserDomainService,
10+
) UserControllerInterface {
11+
return &userControllerInterface{
12+
service: serviceInterface,
13+
}
14+
}
15+
16+
type UserControllerInterface interface {
17+
CreateUser(c *gin.Context)
18+
FindUserByID(c *gin.Context)
19+
FindUserByEmail(c *gin.Context)
20+
UpdateUser(c *gin.Context)
21+
DeleteUser(c *gin.Context)
22+
}
23+
24+
type userControllerInterface struct {
25+
service service.UserDomainService
26+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package view
2+
3+
import (
4+
"github.com/murillolamego/golang-basic/src/controller/model/response"
5+
"github.com/murillolamego/golang-basic/src/model"
6+
)
7+
8+
func ConvertDomainToResponse(
9+
userDomain model.UserDomainInterface,
10+
) response.UserResponse {
11+
return response.UserResponse{
12+
ID: "test",
13+
Email: userDomain.GetEmail(),
14+
Name: userDomain.GetName(),
15+
Age: userDomain.GetAge(),
16+
}
17+
}

0 commit comments

Comments
 (0)