Skip to content

Commit 515eae1

Browse files
Merge pull request #2 from laironacosta/feature/user-service-repo
feat: implementing services and repository for users entity
2 parents 2a15275 + 990c01c commit 515eae1

File tree

9 files changed

+342
-43
lines changed

9 files changed

+342
-43
lines changed

controllers/dto/users_dto.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package dto
2+
3+
type User struct {
4+
Name string `json:"name" binding:"required"`
5+
Email string `json:"email" binding:"required,email"`
6+
}

controllers/users_controller.go

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,62 @@
11
package controllers
22

33
import (
4+
"context"
45
"fmt"
56
"github.com/gin-gonic/gin"
7+
"github.com/laironacosta/ms-gin-go/controllers/dto"
8+
"github.com/laironacosta/ms-gin-go/services"
69
"net/http"
710
)
811

9-
type User struct {
10-
Name string `json:"name" binding:"required"`
11-
Email string `json:"email" binding:"required,email"`
12+
type UserControllerInterface interface {
13+
Create(c *gin.Context)
14+
GetByEmail(c *gin.Context)
15+
UpdateByEmail(c *gin.Context)
16+
DeleteByEmail(c *gin.Context)
1217
}
1318

14-
func Create(c *gin.Context) {
15-
u := User{}
19+
type UserController struct {
20+
userService services.UserServiceInterface
21+
}
22+
23+
func NewUserController(userService services.UserServiceInterface) UserControllerInterface {
24+
return &UserController{
25+
userService,
26+
}
27+
}
28+
29+
func (ctr *UserController) Create(c *gin.Context) {
30+
u := dto.User{}
1631
if err := c.ShouldBindJSON(&u); err != nil {
1732
c.JSON(http.StatusBadRequest, gin.H{
1833
"error": err.Error(),
1934
})
2035
return
2136
}
2237

38+
if err := ctr.userService.Create(context.Background(), u); err != nil {
39+
c.JSON(http.StatusBadRequest, gin.H{
40+
"error": err.Error(),
41+
})
42+
return
43+
}
44+
2345
fmt.Printf("Request received: %+v \n", u)
2446
c.JSON(http.StatusOK, gin.H{"message": "created"})
2547
}
2648

27-
func GetByEmail(c *gin.Context) {
49+
func (ctr *UserController) GetByEmail(c *gin.Context) {
2850
e := c.Param("email")
2951

3052
fmt.Printf("Path param received: %+v \n", e)
31-
c.JSON(http.StatusOK, User{
53+
c.JSON(http.StatusOK, dto.User{
3254
Email: e,
3355
})
3456
}
3557

36-
func UpdateByEmail(c *gin.Context) {
37-
u := User{}
58+
func (ctr *UserController) UpdateByEmail(c *gin.Context) {
59+
u := dto.User{}
3860
if err := c.ShouldBindJSON(&u); err != nil {
3961
c.JSON(http.StatusBadRequest, gin.H{
4062
"error": err.Error(),
@@ -49,7 +71,7 @@ func UpdateByEmail(c *gin.Context) {
4971
c.JSON(http.StatusOK, u)
5072
}
5173

52-
func DeleteByEmail(c *gin.Context) {
74+
func (ctr *UserController) DeleteByEmail(c *gin.Context) {
5375
e := c.Param("email")
5476

5577
fmt.Printf("Path param received: %+v \n", e)

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ module github.com/laironacosta/ms-gin-go
33
go 1.16
44

55
require (
6+
github.com/Lairon/db-go v0.0.0-20210328034946-ed191835cebb
67
github.com/gin-gonic/gin v1.6.3
8+
github.com/go-pg/pg/v10 v10.9.0
79
github.com/go-playground/validator/v10 v10.4.1 // indirect
810
github.com/golang/protobuf v1.5.1 // indirect
911
github.com/json-iterator/go v1.1.10 // indirect
1012
github.com/leodido/go-urn v1.2.1 // indirect
1113
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
1214
github.com/modern-go/reflect2 v1.0.1 // indirect
1315
github.com/ugorji/go v1.2.4 // indirect
14-
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect
1516
golang.org/x/sys v0.0.0-20210326220804-49726bf1d181 // indirect
1617
gopkg.in/yaml.v2 v2.4.0 // indirect
1718
)

go.sum

Lines changed: 152 additions & 3 deletions
Large diffs are not rendered by default.

main.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
11
package main
22

33
import (
4-
"github.com/laironacosta/ms-gin-go/routes"
4+
"github.com/Lairon/db-go/pgdb"
5+
"github.com/gin-gonic/gin"
6+
"github.com/go-pg/pg/v10"
7+
"github.com/laironacosta/ms-gin-go/controllers"
8+
repo "github.com/laironacosta/ms-gin-go/repository"
9+
"github.com/laironacosta/ms-gin-go/router"
10+
"github.com/laironacosta/ms-gin-go/services"
511
)
612

713
func main() {
8-
r := routes.NewRouter()
9-
r.Run() // listen and serve on 0.0.0.0:8080
14+
gin := gin.Default()
15+
16+
db := pgdb.NewPgDB(&pg.Options{
17+
User: "postgres",
18+
Password: "postgres",
19+
Database: "pg-db-go",
20+
})
21+
22+
userRepo := repo.NewUserRepository(db)
23+
userService := services.NewUserService(userRepo)
24+
userController := controllers.NewUserController(userService)
25+
26+
r := router.NewRouter(gin, userController)
27+
r.Init()
28+
29+
gin.Run() // listen and serve on 0.0.0.0:8080
1030
}

repository/users_repository.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package repository
2+
3+
import (
4+
"context"
5+
"github.com/go-pg/pg/v10"
6+
"github.com/laironacosta/ms-gin-go/controllers/dto"
7+
)
8+
9+
type UserRepositoryInterface interface {
10+
Create(ctx context.Context, user dto.User) error
11+
GetByEmail(ctx context.Context, email string) (*dto.User, error)
12+
UpdateByEmail(ctx context.Context, email string) error
13+
DeleteByEmail(ctx context.Context, email string) error
14+
}
15+
16+
type UserRepository struct {
17+
DB *pg.DB
18+
}
19+
20+
func NewUserRepository(db *pg.DB) UserRepositoryInterface {
21+
return &UserRepository{
22+
db,
23+
}
24+
}
25+
26+
func (r *UserRepository) Create(ctx context.Context, user dto.User) error {
27+
return nil
28+
}
29+
30+
func (r *UserRepository) GetByEmail(ctx context.Context, email string) (*dto.User, error) {
31+
return &dto.User{}, nil
32+
}
33+
34+
func (r *UserRepository) UpdateByEmail(ctx context.Context, email string) error {
35+
return nil
36+
}
37+
38+
func (r *UserRepository) DeleteByEmail(ctx context.Context, email string) error {
39+
return nil
40+
}

router/router.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package router
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
"github.com/laironacosta/ms-gin-go/controllers"
6+
)
7+
8+
type Router struct {
9+
server *gin.Engine
10+
userController controllers.UserControllerInterface
11+
}
12+
13+
func NewRouter(server *gin.Engine, userController controllers.UserControllerInterface) *Router {
14+
return &Router{
15+
server,
16+
userController,
17+
}
18+
}
19+
20+
func (r *Router) Init() {
21+
//create a default router with default middleware
22+
basePath := r.server.Group("/ms-gin-go")
23+
24+
basePath.GET("/health", controllers.Health)
25+
26+
users := basePath.Group("/users")
27+
{
28+
users.POST("/", r.userController.Create)
29+
users.GET("/:email", r.userController.GetByEmail)
30+
users.PUT("/:email", r.userController.UpdateByEmail)
31+
users.PATCH("/:email", r.userController.UpdateByEmail)
32+
users.DELETE("/:email", r.userController.DeleteByEmail)
33+
}
34+
}

routes/router.go

Lines changed: 0 additions & 26 deletions
This file was deleted.

services/users_service.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package services
2+
3+
import (
4+
"context"
5+
"github.com/laironacosta/ms-gin-go/controllers/dto"
6+
repo "github.com/laironacosta/ms-gin-go/repository"
7+
)
8+
9+
type UserServiceInterface interface {
10+
Create(ctx context.Context, user dto.User) error
11+
GetByEmail(ctx context.Context, email string) (*dto.User, error)
12+
UpdateByEmail(ctx context.Context, email string) error
13+
DeleteByEmail(ctx context.Context, email string) error
14+
}
15+
16+
type UserService struct {
17+
userRepo repo.UserRepositoryInterface
18+
}
19+
20+
func NewUserService(userRepo repo.UserRepositoryInterface) UserServiceInterface {
21+
return &UserService{
22+
userRepo,
23+
}
24+
}
25+
26+
func (s *UserService) Create(ctx context.Context, user dto.User) error {
27+
if err := s.userRepo.Create(ctx, user); err != nil {
28+
return err
29+
}
30+
return nil
31+
}
32+
33+
func (s *UserService) GetByEmail(ctx context.Context, email string) (*dto.User, error) {
34+
u, err := s.userRepo.GetByEmail(ctx, email)
35+
if err != nil {
36+
return &dto.User{}, err
37+
}
38+
return u, nil
39+
}
40+
41+
func (s *UserService) UpdateByEmail(ctx context.Context, email string) error {
42+
if err := s.userRepo.UpdateByEmail(ctx, email); err != nil {
43+
return err
44+
}
45+
return nil
46+
}
47+
48+
func (s *UserService) DeleteByEmail(ctx context.Context, email string) error {
49+
if err := s.userRepo.UpdateByEmail(ctx, email); err != nil {
50+
return err
51+
}
52+
return nil
53+
}

0 commit comments

Comments
 (0)