diff --git a/commands/license_handler.go b/commands/license_handler.go index 2ea163d..ba3427d 100644 --- a/commands/license_handler.go +++ b/commands/license_handler.go @@ -1,8 +1,10 @@ package commands import ( - "netina/models" + m "netina/models" + c "netina/models/commands" l "netina/repositories/license" + "netina/validation" "time" ) @@ -12,8 +14,12 @@ type CreateLicenseHandler struct { } -func(r *CreateLicenseHandler) Handle (command models.CreateLicenseCommand) error { - license := &models.License{ +func(r *CreateLicenseHandler) Handle (command c.CreateLicenseCommand) error { + if err := validation.ValidateStruct(command); err != nil { + return err + } + + license := &m.License{ Plan_id: command.Plan_id, Period: command.Period, Created_at: time.Now(), @@ -29,8 +35,13 @@ type UpdateLicenseHandler struct { } -func (r *UpdateLicenseHandler) Handle (id uint , command models.UpdateLicenseCommand) (*models.License , error) { - license := &models.License{ +func (r *UpdateLicenseHandler) Handle (id uint , command c.UpdateLicenseCommand) (*m.License , error) { + + if err := validation.ValidateStruct(command); err != nil { + return nil , err + } + + license := &m.License{ Plan_id: command.Plan_id, Period: command.Period, Modified_by: command.Modified_by, diff --git a/commands/owner_handler.go b/commands/owner_handler.go index f2ef433..78bd170 100644 --- a/commands/owner_handler.go +++ b/commands/owner_handler.go @@ -1,8 +1,10 @@ package commands import ( - "netina/models" + m "netina/models" + c "netina/models/commands" o "netina/repositories/owner" + "netina/validation" "time" ) @@ -11,8 +13,11 @@ type CreateOwnerHandler struct{ Repository o.OwnerCommandRepository } -func (r *CreateOwnerHandler) Handle(command models.CreateOwnerCommand) error { - owner := &models.Owner{ +func (r *CreateOwnerHandler) Handle(command c.CreateOwnerCommand) error { + if err := validation.ValidateStruct(command); err != nil { + return err + } + owner := &m.Owner{ PhoneNumber: command.PhoneNumber, FirstName: command.FirstName, LastName: command.LastName, @@ -30,8 +35,13 @@ type UpdateOwnerHandler struct { } -func (r *UpdateOwnerHandler) Handle(id uint , command models.UpdateOwnerCommand)(*models.Owner , error) { - owner := &models.Owner{ +func (r *UpdateOwnerHandler) Handle(id uint , command c.UpdateOwnerCommand)(*m.Owner , error) { + + if err := validation.ValidateStruct(command); err != nil { + return nil , err + } + + owner := &m.Owner{ PhoneNumber: command.PhoneNumber, FirstName: command.FirstName, LastName: command.LastName, diff --git a/commands/plan_handler.go b/commands/plan_handler.go index 619f6b5..a963f90 100644 --- a/commands/plan_handler.go +++ b/commands/plan_handler.go @@ -1,8 +1,10 @@ package commands import ( - "netina/models" + m "netina/models" + c "netina/models/commands" p "netina/repositories/plan" + "netina/validation" "time" ) @@ -12,8 +14,13 @@ type CreatePlanHandler struct { -func (r *CreatePlanHandler) Handle(command models.CreatePlanCommand) error { - plan := &models.Plan{ +func (r *CreatePlanHandler) Handle(command c.CreatePlanCommand) error { + + if err := validation.ValidateStruct(command); err != nil { + return err + } + + plan := &m.Plan{ Price: command.Price, Priod: command.Priod, Partnership: command.Partnership, @@ -31,8 +38,12 @@ type UpdatePlanHandler struct { } -func (r *UpdatePlanHandler) Handle (id uint , command models.UpdatePlanCommand)(*models.Plan , error) { - plan := &models.Plan{ +func (r *UpdatePlanHandler) Handle (id uint , command c.UpdatePlanCommand)(*m.Plan , error) { + if err := validation.ValidateStruct(command); err != nil { + return nil , err + } + + plan := &m.Plan{ Price: command.Price, Priod: command.Priod, Partnership: command.Partnership, diff --git a/commands/store_handler.go b/commands/store_handler.go index cff0125..f869824 100644 --- a/commands/store_handler.go +++ b/commands/store_handler.go @@ -1,8 +1,10 @@ package commands import ( - "netina/models" + m "netina/models" + c "netina/models/commands" s "netina/repositories/store" + "netina/validation" "time" ) @@ -12,8 +14,13 @@ type CreateStoreHandler struct { } -func (r *CreateStoreHandler) Handle (command models.CreateStoreCommand)error { - store := &models.Store{ +func (r *CreateStoreHandler) Handle (command c.CreateStoreCommand)error { + + if err := validation.ValidateStruct(command); err != nil { + return err + } + + store := &m.Store{ Owner_id: command.Owner_id, Name: command.Name, Address: command.Address, @@ -38,8 +45,13 @@ type UpdateStoreHandler struct { } -func (r *UpdateStoreHandler) Handle (id uint ,command models.UpdateStoreCommand)(*models.Store , error) { - store := &models.Store{ +func (r *UpdateStoreHandler) Handle (id uint ,command c.UpdateStoreCommand)(*m.Store , error) { + + if err := validation.ValidateStruct(command); err != nil { + return nil,err + } + + store := &m.Store{ Owner_id: command.Owner_id, Name: command.Name, Address: command.Address, diff --git a/go.mod b/go.mod index fc23994..8039f7a 100644 --- a/go.mod +++ b/go.mod @@ -9,11 +9,18 @@ require ( ) require ( + github.com/gabriel-vasile/mimetype v1.4.3 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.20.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect github.com/jackc/pgx/v5 v5.4.3 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect - golang.org/x/crypto v0.14.0 // indirect - golang.org/x/text v0.13.0 // indirect + github.com/leodido/go-urn v1.4.0 // indirect + golang.org/x/crypto v0.19.0 // indirect + golang.org/x/net v0.21.0 // indirect + golang.org/x/sys v0.17.0 // indirect + golang.org/x/text v0.14.0 // indirect ) diff --git a/go.sum b/go.sum index 000df38..66e78d4 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,14 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= +github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8= +github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= @@ -13,6 +21,8 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= +github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -22,8 +32,16 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= +golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo= +golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= +golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= +golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= +golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= +golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 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= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/models/Store.go b/models/Store.go index ead5281..bd274ca 100644 --- a/models/Store.go +++ b/models/Store.go @@ -19,4 +19,4 @@ type Store struct { Modified_at time.Time Modified_by string Is_removed bool `gorm:"default:false"` -} \ No newline at end of file +} diff --git a/models/license_commands.go b/models/commands/license_commands.go similarity index 100% rename from models/license_commands.go rename to models/commands/license_commands.go diff --git a/models/owner_commands.go b/models/commands/owner_commands.go similarity index 100% rename from models/owner_commands.go rename to models/commands/owner_commands.go diff --git a/models/plan_commands.go b/models/commands/plan_commands.go similarity index 100% rename from models/plan_commands.go rename to models/commands/plan_commands.go diff --git a/models/store_commands.go b/models/commands/store_commands.go similarity index 100% rename from models/store_commands.go rename to models/commands/store_commands.go diff --git a/queries/license_handler.go b/queries/license_handler.go new file mode 100644 index 0000000..528246c --- /dev/null +++ b/queries/license_handler.go @@ -0,0 +1,16 @@ +package queries + +import ( + "netina/models" + l "netina/repositories/license" +) + + +type GetLicenseHandler struct { + Repository l.LicenseQueryRepository +} + + +func (r *GetLicenseHandler) Handle (id uint)(*models.License ,error) { + return r.Repository.GetLicense(id) +} \ No newline at end of file diff --git a/queries/owner_handler.go b/queries/owner_handler.go new file mode 100644 index 0000000..4fac347 --- /dev/null +++ b/queries/owner_handler.go @@ -0,0 +1,16 @@ +package queries + +import ( + "netina/models" + o "netina/repositories/owner" +) + + +type GetOwnerHandler struct { + Repository o.OwnerQueryRepository +} + + +func (r *GetOwnerHandler) Handle (id uint)(*models.Owner ,error) { + return r.Repository.GetOwner(id) +} \ No newline at end of file diff --git a/queries/plan_handler.go b/queries/plan_handler.go new file mode 100644 index 0000000..5f88173 --- /dev/null +++ b/queries/plan_handler.go @@ -0,0 +1,16 @@ +package queries + +import ( + "netina/models" + p "netina/repositories/plan" +) + + +type GetPlanHandler struct { + Repository p.PlanQueryRepository +} + + +func (r *GetPlanHandler) Handle (id uint)(*models.Plan ,error) { + return r.Repository.GetPlan(id) +} \ No newline at end of file diff --git a/queries/store_handler.go b/queries/store_handler.go new file mode 100644 index 0000000..6e2056e --- /dev/null +++ b/queries/store_handler.go @@ -0,0 +1,26 @@ +package queries + +import ( + "netina/models" + s "netina/repositories/store" +) + + +type GetStoreHandler struct { + Repository s.StoreQueryRepository +} + + +func (r *GetStoreHandler) Handle (id uint)(*models.Store ,error) { + return r.Repository.GetStore(id) +} + + +type GetStoreListHandler struct { + Repository s.StoreQueryRepository +} + +// get owner id and return owner's stores +func (r *GetStoreListHandler) Handle (id uint)([]models.Store ,error) { + return r.Repository.GetStores(id) +} \ No newline at end of file diff --git a/validation/validation.go b/validation/validation.go new file mode 100644 index 0000000..7a3e5df --- /dev/null +++ b/validation/validation.go @@ -0,0 +1,15 @@ +package validation + +import ( + "github.com/go-playground/validator/v10" +) + +var validate *validator.Validate + +func init() { + validate = validator.New() +} + +func ValidateStruct(s interface{}) error { + return validate.Struct(s) +}