Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ RUN apk --no-cache add ca-certificates

WORKDIR /root/

# Copy migrations folder
COPY ./migrations ./migrations

# Copy the Pre-built binary file from the previous stage. Observe we also copied the .env file
COPY --from=builder /app/main .
COPY --from=builder /app/.env .
Expand Down
6 changes: 6 additions & 0 deletions controllers/dto/create_user_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dto

type CreateUserRequest struct {
Name string `json:"name" binding:"required"`
Email string `json:"email" binding:"required,email"`
}
5 changes: 5 additions & 0 deletions controllers/dto/update_user_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dto

type UpdateUserRequest struct {
Name string `json:"name" binding:"required"`
}
File renamed without changes.
33 changes: 26 additions & 7 deletions controllers/users_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewUserController(userService services.UserServiceInterface) UserController
}

func (ctr *UserController) Create(c *gin.Context) {
u := dto.User{}
u := dto.CreateUserRequest{}
if err := c.ShouldBindJSON(&u); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
Expand All @@ -48,15 +48,20 @@ func (ctr *UserController) Create(c *gin.Context) {

func (ctr *UserController) GetByEmail(c *gin.Context) {
e := c.Param("email")

fmt.Printf("Path param received: %+v \n", e)
c.JSON(http.StatusOK, dto.User{
Email: e,
})

u, err := ctr.userService.GetByEmail(context.Background(), e)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
return
}
c.JSON(http.StatusOK, u)
}

func (ctr *UserController) UpdateByEmail(c *gin.Context) {
u := dto.User{}
u := dto.UpdateUserRequest{}
if err := c.ShouldBindJSON(&u); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
Expand All @@ -68,12 +73,26 @@ func (ctr *UserController) UpdateByEmail(c *gin.Context) {

fmt.Printf("Request received: %+v \n", u)
fmt.Printf("Path param received: %+v \n", e)
c.JSON(http.StatusOK, u)

if err := ctr.userService.UpdateByEmail(context.Background(), u, e); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{"message": "updated"})
}

func (ctr *UserController) DeleteByEmail(c *gin.Context) {
e := c.Param("email")

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

if err := ctr.userService.DeleteByEmail(context.Background(), e); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{"message": "deleted"})
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.16
require (
github.com/Lairon/db-go v0.0.0-20210328034946-ed191835cebb
github.com/gin-gonic/gin v1.6.3
github.com/go-pg/migrations/v8 v8.1.0
github.com/go-pg/pg/v10 v10.9.0
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/golang/protobuf v1.5.1 // indirect
Expand Down
20 changes: 20 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/go-pg/migrations/v8 v8.1.0 h1:bc1wQwFoWRKvLdluXCRFRkeaw9xDU4qJ63uCAagh66w=
github.com/go-pg/migrations/v8 v8.1.0/go.mod h1:o+CN1u572XHphEHZyK6tqyg2GDkRvL2bIoLNyGIewus=
github.com/go-pg/pg/v10 v10.4.0/go.mod h1:BfgPoQnD2wXNd986RYEHzikqv9iE875PrFaZ9vXvtNM=
github.com/go-pg/pg/v10 v10.9.0 h1:mNIxE7H7/5fHOniVrLgUXNoIgHiJXXvhiNY+PxqtV6k=
github.com/go-pg/pg/v10 v10.9.0/go.mod h1:rgmTPgHgl5EN2CNKKoMwC7QT62t8BqsdpEkUQuiZMQs=
github.com/go-pg/zerochecker v0.2.0 h1:pp7f72c3DobMWOb2ErtZsnrPaSvHd2W4o9//8HtF4mU=
Expand All @@ -35,8 +38,10 @@ github.com/go-redis/redis/v8 v8.8.0/go.mod h1:F7resOH5Kdug49Otu24RjHWwgK7u9AmtqW
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/golang/protobuf v1.3.4/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
Expand All @@ -53,6 +58,7 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
Expand Down Expand Up @@ -111,13 +117,17 @@ github.com/ugorji/go/codec v1.2.4 h1:C5VurWRRCKjuENsbM6GYVw8W++WVW9rSxoACKIvxzz8
github.com/ugorji/go/codec v1.2.4/go.mod h1:bWBu1+kIRWcF8uMklKaJrR6fTWQOwAlrIzX22pHwryA=
github.com/vmihailenco/bufpool v0.1.11 h1:gOq2WmBrq0i2yW5QJ16ykccQ4wH9UyEsgLm6czKAd94=
github.com/vmihailenco/bufpool v0.1.11/go.mod h1:AFf/MOy3l2CFTKbxwt0mp2MwnqjNEs5H/UxrkA5jxTQ=
github.com/vmihailenco/msgpack/v4 v4.3.11/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4=
github.com/vmihailenco/msgpack/v5 v5.0.0-beta.1/go.mod h1:xlngVLeyQ/Qi05oQxhQ+oTuqa03RjMwMfk/7/TCs+QI=
github.com/vmihailenco/msgpack/v5 v5.3.0 h1:8G3at/kelmBKeHY6d6cKnGsYO3BLn+uubitdOtOhyNI=
github.com/vmihailenco/msgpack/v5 v5.3.0/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
github.com/vmihailenco/tagparser v0.1.2 h1:gnjoVuB/kljJ5wICEEOpx98oXMWPLj22G67Vbd1qPqc=
github.com/vmihailenco/tagparser v0.1.2/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
go.opentelemetry.io/otel v0.13.0/go.mod h1:dlSNewoRYikTkotEnxdmuBHgzT+k/idJSfDv/FxEnOY=
go.opentelemetry.io/otel v0.19.0 h1:Lenfy7QHRXPZVsw/12CWpxX6d/JkrX8wrx2vO8G80Ng=
go.opentelemetry.io/otel v0.19.0/go.mod h1:j9bF567N9EfomkSidSfmMwIwIBuP37AMAIzVW85OxSg=
go.opentelemetry.io/otel/metric v0.19.0 h1:dtZ1Ju44gkJkYvo+3qGqVXmf88tc+a42edOywypengg=
Expand All @@ -130,6 +140,8 @@ golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b/go.mod h1:6SG95UA2DQfeDnf
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 h1:It14KIkyBFYkHkwZ7k45minvA9aorojkyjGk9KJ5B/w=
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand All @@ -143,9 +155,13 @@ golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20201006153459-a7d1128ccaa0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw=
Expand All @@ -166,6 +182,8 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201017003518-b09fb700fbb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -190,6 +208,8 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1N
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/go-pg/pg/v10"
"github.com/laironacosta/ms-gin-go/controllers"
"github.com/laironacosta/ms-gin-go/migrations"
repo "github.com/laironacosta/ms-gin-go/repository"
"github.com/laironacosta/ms-gin-go/router"
"github.com/laironacosta/ms-gin-go/services"
Expand All @@ -20,6 +21,7 @@ func main() {
Password: os.Getenv("DB_PASSWORD"),
Database: os.Getenv("DB_NAME"),
})
migrations.Init(db)

userRepo := repo.NewUserRepository(db)
userService := services.NewUserService(userRepo)
Expand Down
1 change: 1 addition & 0 deletions migrations/1_create_users_table.tx.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE users;
7 changes: 7 additions & 0 deletions migrations/1_create_users_table.tx.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE public.users (
id serial NOT NULL,
name varchar(40) NULL,
email varchar(254) NOT NULL,
CONSTRAINT users_email_key UNIQUE (email),
CONSTRAINT users_pkey PRIMARY KEY (id)
);
28 changes: 28 additions & 0 deletions migrations/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package migrations

import (
"fmt"
"github.com/go-pg/migrations/v8"
"github.com/go-pg/pg/v10"
)

func Init(db *pg.DB) {
// create a new collection with gopg_migrations table
c := migrations.NewCollection()
c.DisableSQLAutodiscover(true)
c.DiscoverSQLMigrations(fmt.Sprintf("migrations"))

// create gopg_migrations table if not exists
_, _, _ = c.Run(db, "init")

// run migration files
oldVersion, newVersion, err := c.Run(db, "up")
if err != nil {
panic(err.Error())
}
if newVersion != oldVersion {
fmt.Printf("migrated from version %d to %d\n", oldVersion, newVersion)
} else {
fmt.Printf("version is %d\n", oldVersion)
}
}
35 changes: 29 additions & 6 deletions repository/users_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
)

type UserRepositoryInterface interface {
Create(ctx context.Context, user dto.User) error
Create(ctx context.Context, request dto.CreateUserRequest) error
GetByEmail(ctx context.Context, email string) (*dto.User, error)
UpdateByEmail(ctx context.Context, email string) error
UpdateByEmail(ctx context.Context, request dto.UpdateUserRequest, email string) error
DeleteByEmail(ctx context.Context, email string) error
}

type UserRepository struct {
DB *pg.DB
db *pg.DB
}

func NewUserRepository(db *pg.DB) UserRepositoryInterface {
Expand All @@ -23,18 +23,41 @@ func NewUserRepository(db *pg.DB) UserRepositoryInterface {
}
}

func (r *UserRepository) Create(ctx context.Context, user dto.User) error {
func (r *UserRepository) Create(ctx context.Context, request dto.CreateUserRequest) error {
u := dto.User{
request.Name,
request.Email,
}
_, err := r.db.Model(&u).Context(ctx).Insert()
if err != nil {
return err
}
return nil
}

func (r *UserRepository) GetByEmail(ctx context.Context, email string) (*dto.User, error) {
return &dto.User{}, nil
u := dto.User{}
err := r.db.Model(&u).Context(ctx).Where("email = ?", email).Select()
if err != nil {
return &u, err
}
return &u, nil
}

func (r *UserRepository) UpdateByEmail(ctx context.Context, email string) error {
func (r *UserRepository) UpdateByEmail(ctx context.Context, request dto.UpdateUserRequest, email string) error {
u := dto.User{}
_, err := r.db.Model(&u).Context(ctx).Set("name = ?", request.Name).Where("email = ?", email).Update()
if err != nil {
return err
}
return nil
}

func (r *UserRepository) DeleteByEmail(ctx context.Context, email string) error {
u := dto.User{}
_, err := r.db.Model(&u).Context(ctx).Where("email = ?", email).Delete()
if err != nil {
return err
}
return nil
}
34 changes: 26 additions & 8 deletions services/users_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package services

import (
"context"
"errors"
"github.com/laironacosta/ms-gin-go/controllers/dto"
repo "github.com/laironacosta/ms-gin-go/repository"
"strings"
)

type UserServiceInterface interface {
Create(ctx context.Context, user dto.User) error
Create(ctx context.Context, request dto.CreateUserRequest) error
GetByEmail(ctx context.Context, email string) (*dto.User, error)
UpdateByEmail(ctx context.Context, email string) error
UpdateByEmail(ctx context.Context, request dto.UpdateUserRequest, email string) error
DeleteByEmail(ctx context.Context, email string) error
}

Expand All @@ -23,30 +25,46 @@ func NewUserService(userRepo repo.UserRepositoryInterface) UserServiceInterface
}
}

func (s *UserService) Create(ctx context.Context, user dto.User) error {
if err := s.userRepo.Create(ctx, user); err != nil {
func (s *UserService) Create(ctx context.Context, request dto.CreateUserRequest) error {
if err := s.userRepo.Create(ctx, request); err != nil {
return err
}
return nil
}

func (s *UserService) GetByEmail(ctx context.Context, email string) (*dto.User, error) {
u, err := s.userRepo.GetByEmail(ctx, email)
e := strings.TrimSpace(email)
if e == "" {
return &dto.User{}, errors.New("email could not be empty")
}

u, err := s.userRepo.GetByEmail(ctx, e)
if err != nil {
return &dto.User{}, err
}
return u, nil
}

func (s *UserService) UpdateByEmail(ctx context.Context, email string) error {
if err := s.userRepo.UpdateByEmail(ctx, email); err != nil {
func (s *UserService) UpdateByEmail(ctx context.Context, request dto.UpdateUserRequest, email string) error {
e := strings.TrimSpace(email)
if e == "" {
return errors.New("email could not be empty")
}

if err := s.userRepo.UpdateByEmail(ctx, request, e); err != nil {
return err
}
return nil
}

func (s *UserService) DeleteByEmail(ctx context.Context, email string) error {
if err := s.userRepo.UpdateByEmail(ctx, email); err != nil {
e := strings.TrimSpace(email)
if e == "" {
return errors.New("email could not be empty")
}

err := s.userRepo.DeleteByEmail(ctx, e)
if err != nil {
return err
}
return nil
Expand Down