create kind box after accepting request

This commit is contained in:
Abolfazl Nourzad 2024-01-23 13:51:56 +03:30
parent dc9d931227
commit befdad0c50
Signed by: abolfazl
GPG Key ID: 183534166EB62E0B
24 changed files with 471 additions and 445 deletions

View File

@ -1,32 +1,24 @@
package adminkindboxhandler
import (
"net/http"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
echo "github.com/labstack/echo/v4"
)
func (h Handler) Add(c echo.Context) error {
var req param.KindBoxAddRequest
if bErr := c.Bind(&req); bErr != nil {
return echo.NewHTTPError(http.StatusBadRequest)
}
if fieldErrors, err := h.adminKindBoxVld.ValidateAddRequest(req); err != nil {
msg, code := httpmsg.Error(err)
return c.JSON(code, echo.Map{
"message": msg,
"errors": fieldErrors,
})
}
resp, sErr := h.adminKindBoxSvc.Add(c.Request().Context(), req)
if sErr != nil {
msg, code := httpmsg.Error(sErr)
return echo.NewHTTPError(code, msg)
}
return c.JSON(http.StatusCreated, resp)
}
// func (h Handler) Add(c echo.Context) error {
// var req param.KindBoxAddRequest
// if bErr := c.Bind(&req); bErr != nil {
// return echo.NewHTTPError(http.StatusBadRequest)
// }
// if fieldErrors, err := h.adminKindBoxVld.ValidateAddRequest(req); err != nil {
// msg, code := httpmsg.Error(err)
//
// return c.JSON(code, echo.Map{
// "message": msg,
// "errors": fieldErrors,
// })
// }
// resp, sErr := h.adminKindBoxSvc.Add(c.Request().Context(), req)
// if sErr != nil {
// msg, code := httpmsg.Error(sErr)
//
// return echo.NewHTTPError(code, msg)
// }
//
// return c.JSON(http.StatusCreated, resp)
//}

View File

@ -1,32 +1,32 @@
package adminkindboxhandler
import (
"net/http"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
echo "github.com/labstack/echo/v4"
)
func (h Handler) Get(c echo.Context) error {
var req param.KindBoxGetRequest
if bErr := c.Bind(&req); bErr != nil {
return echo.NewHTTPError(http.StatusBadRequest)
}
if fieldErrors, err := h.adminKindBoxVld.ValidateGetRequest(req); err != nil {
msg, code := httpmsg.Error(err)
return c.JSON(code, echo.Map{
"message": msg,
"errors": fieldErrors,
})
}
resp, sErr := h.adminKindBoxSvc.Get(c.Request().Context(), req)
if sErr != nil {
msg, code := httpmsg.Error(sErr)
return echo.NewHTTPError(code, msg)
}
return c.JSON(http.StatusCreated, resp)
}
// import (
// "net/http"
//
// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
// httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
// echo "github.com/labstack/echo/v4"
//)
//
//func (h Handler) Get(c echo.Context) error {
// var req param.KindBoxGetRequest
// if bErr := c.Bind(&req); bErr != nil {
// return echo.NewHTTPError(http.StatusBadRequest)
// }
// if fieldErrors, err := h.adminKindBoxVld.ValidateGetRequest(req); err != nil {
// msg, code := httpmsg.Error(err)
//
// return c.JSON(code, echo.Map{
// "message": msg,
// "errors": fieldErrors,
// })
// }
// resp, sErr := h.adminKindBoxSvc.Get(c.Request().Context(), req)
// if sErr != nil {
// msg, code := httpmsg.Error(sErr)
//
// return echo.NewHTTPError(code, msg)
// }
//
// return c.JSON(http.StatusCreated, resp)
//}

View File

@ -1,25 +1,26 @@
package adminkindboxhandler
import (
"net/http"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
echo "github.com/labstack/echo/v4"
)
func (h Handler) GetAll(c echo.Context) error {
var req param.KindBoxGetAllRequest
if bErr := c.Bind(&req); bErr != nil {
return echo.NewHTTPError(http.StatusBadRequest)
}
resp, sErr := h.adminKindBoxSvc.GetAll(c.Request().Context(), req)
if sErr != nil {
msg, code := httpmsg.Error(sErr)
return echo.NewHTTPError(code, msg)
}
return c.JSON(http.StatusCreated, resp)
}
//
// import (
// "net/http"
//
// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
// httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
// echo "github.com/labstack/echo/v4"
//)
//
//func (h Handler) GetAll(c echo.Context) error {
// var req param.KindBoxGetAllRequest
// if bErr := c.Bind(&req); bErr != nil {
// return echo.NewHTTPError(http.StatusBadRequest)
// }
//
// resp, sErr := h.adminKindBoxSvc.GetAll(c.Request().Context(), req)
// if sErr != nil {
// msg, code := httpmsg.Error(sErr)
//
// return echo.NewHTTPError(code, msg)
// }
//
// return c.JSON(http.StatusCreated, resp)
//}

View File

@ -5,10 +5,10 @@ import (
)
func (h Handler) SetRoutes(e *echo.Echo) {
r := e.Group("/admin/kindboxes")
r.POST("/", h.Add).Name = "admin-addkindbox"
r.GET("/:id", h.Get).Name = "admin-getkindboxbyid"
r.GET("/", h.GetAll).Name = "admin-getallkindbox"
r.PATCH("/:id", h.Update).Name = "admin-updatekindbox"
// r.POST("/", h.Add).Name = "admin-addkindbox"
// r.GET("/:id", h.Get).Name = "admin-getkindboxbyid"
// r.GET("/", h.GetAll).Name = "admin-getallkindbox"
// r.PATCH("/:id", h.Update).Name = "admin-updatekindbox"
}

View File

@ -1,32 +1,32 @@
package adminkindboxhandler
import (
"net/http"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
echo "github.com/labstack/echo/v4"
)
func (h Handler) Update(c echo.Context) error {
var req param.KindBoxUpdateRequest
if bErr := c.Bind(&req); bErr != nil {
return echo.NewHTTPError(http.StatusBadRequest)
}
if fieldErrors, err := h.adminKindBoxVld.ValidateUpdateRequest(req); err != nil {
msg, code := httpmsg.Error(err)
return c.JSON(code, echo.Map{
"message": msg,
"errors": fieldErrors,
})
}
resp, sErr := h.adminKindBoxSvc.Update(c.Request().Context(), req)
if sErr != nil {
msg, code := httpmsg.Error(sErr)
return echo.NewHTTPError(code, msg)
}
return c.JSON(http.StatusCreated, resp)
}
// import (
// "net/http"
//
// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
// httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
// echo "github.com/labstack/echo/v4"
//)
//
//func (h Handler) Update(c echo.Context) error {
// var req param.KindBoxUpdateRequest
// if bErr := c.Bind(&req); bErr != nil {
// return echo.NewHTTPError(http.StatusBadRequest)
// }
// if fieldErrors, err := h.adminKindBoxVld.ValidateUpdateRequest(req); err != nil {
// msg, code := httpmsg.Error(err)
//
// return c.JSON(code, echo.Map{
// "message": msg,
// "errors": fieldErrors,
// })
// }
// resp, sErr := h.adminKindBoxSvc.Update(c.Request().Context(), req)
// if sErr != nil {
// msg, code := httpmsg.Error(sErr)
//
// return echo.NewHTTPError(code, msg)
// }
//
// return c.JSON(http.StatusCreated, resp)
//}

1
go.mod
View File

@ -31,6 +31,7 @@ require (
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/oklog/ulid/v2 v2.1.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.17.0 // indirect

3
go.sum
View File

@ -228,8 +228,11 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/npillmayer/nestext v0.1.3/go.mod h1:h2lrijH8jpicr25dFY+oAJLyzlya6jhnuG+zWp9L0Uk=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/oklog/ulid/v2 v2.1.0 h1:+9lhoxAP56we25tyYETBBY1YLA2SaoLvUFgrP2miPJU=
github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
github.com/pelletier/go-toml v1.7.0 h1:7utD74fnzVc/cpcyy8sjrlFr5vYpypUixARcHIMIGuI=
github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=

View File

@ -11,9 +11,11 @@ import (
mysqladdress "git.gocasts.ir/ebhomengo/niki/repository/mysql/address"
mysqladmin "git.gocasts.ir/ebhomengo/niki/repository/mysql/admin"
mysqlbenefactor "git.gocasts.ir/ebhomengo/niki/repository/mysql/benefactor"
mysqlkindbox "git.gocasts.ir/ebhomengo/niki/repository/mysql/kind_box"
mysqlkindboxreq "git.gocasts.ir/ebhomengo/niki/repository/mysql/kind_box_req"
redisotp "git.gocasts.ir/ebhomengo/niki/repository/redis/redis_otp"
adminservice "git.gocasts.ir/ebhomengo/niki/service/admin/admin"
adminkindboxservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box"
adminkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box_req"
adminauthservice "git.gocasts.ir/ebhomengo/niki/service/auth/admin"
authservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor"
@ -66,8 +68,9 @@ func setupServices(cfg config.Config) (
benefactorKindBoxReqMysql := mysqlkindboxreq.New(MysqlRepo)
benefactorKindBoxReqSvc = benefactorkindboxreqservice.New(benefactorKindBoxReqMysql)
benefactorKindBoxReqVld = benefactorkindboxreqvalidator.New(benefactorKindBoxReqMysql, benefactorSvc, benefactorAddressSvc)
adminKindBoxReqSvc = adminkindboxreqservice.New(benefactorKindBoxReqMysql)
mysqlkindboxRepo := mysqlkindbox.New(MysqlRepo)
adminkindboxsvc := adminkindboxservice.New(mysqlkindboxRepo)
adminKindBoxReqSvc = adminkindboxreqservice.New(benefactorKindBoxReqMysql, adminkindboxsvc)
adminKindBoxReqVld = adminkindboxreqvalidator.New(benefactorKindBoxReqMysql)
adminAuthSvc = adminauthservice.New(cfg.AdminAuth)

View File

@ -2,11 +2,13 @@ package adminkindboxparam
import entity "git.gocasts.ir/ebhomengo/niki/entity"
type KindBoxAddRequest struct {
type KindBoxAddAfterAcceptingReqRequest struct {
BenefactorID uint
KindBoxReqID uint
Type entity.KindBoxType
Count uint
}
type KindBoxAddResponse struct {
KindBox entity.KindBox
type KindBoxAddAfterAcceptingReqResponse struct {
KindBoxes []entity.KindBox
}

View File

@ -1,8 +1,9 @@
package adminkindboxreqparam
import (
"git.gocasts.ir/ebhomengo/niki/entity"
"time"
"git.gocasts.ir/ebhomengo/niki/entity"
)
type KindBoxReqAcceptRequest struct {
@ -17,10 +18,4 @@ type KindBoxReqAcceptResponse struct {
CountAccepted uint `json:"count_accepted"`
ReferDate time.Time `json:"refer_date"`
AddressID uint `json:"address_id"`
KindBoxID uint `json:"kind_box_id"`
KindBoxType entity.KindBoxType `json:"kind_box_type"`
BenefactorID uint `json:"benefactor_id"`
KindBoxStatus entity.KindBoxStatus `json:"kind_box_status"`
KindBoxSerialNumber string `json:"kind_box_serial_number"`
}

View File

@ -2,9 +2,43 @@ package mysqlkindbox
import (
"context"
"git.gocasts.ir/ebhomengo/niki/entity"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (d DB) AddKindBox(ctx context.Context, kindBox entity.KindBox) (entity.KindBox, error) {
return entity.KindBox{}, nil
func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([]entity.KindBox, error) {
const op = "mysqlkindbox.AddBatchKindBox"
tx, tErr := d.conn.Conn().Begin()
if tErr != nil {
return nil, richerror.New(op).WithErr(tErr).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
for _, kindBox := range kindBoxes {
res, err := tx.
ExecContext(ctx,
"insert into kind_boxes (kind_box_req_id , benefactor_id , type ,serial_number , status) values (? , ? , ? , ? ,?);",
kindBox.KindBoxReqID, kindBox.BenefactorID, kindBox.Type.String(), kindBox.SerialNumber, kindBox.Status.String(),
)
if err != nil {
if rErr := tx.Rollback(); rErr != nil {
return nil, richerror.New(op).WithErr(rErr).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
//nolint
// err is always nil
id, _ := res.LastInsertId()
kindBox.ID = uint(id)
}
if cErr := tx.Commit(); cErr != nil {
return nil, richerror.New(op).WithErr(cErr).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
return kindBoxes, nil
}

View File

@ -6,19 +6,28 @@ import (
entity "git.gocasts.ir/ebhomengo/niki/entity"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
"github.com/oklog/ulid/v2"
)
func (s Service) Add(ctx context.Context, req param.KindBoxAddRequest) (param.KindBoxAddResponse, error) {
const op = "adminkindboxservice.Add"
func (s Service) AddKindBoxAfterAcceptingRequest(ctx context.Context, req param.KindBoxAddAfterAcceptingReqRequest) (param.KindBoxAddAfterAcceptingReqResponse, error) {
const op = "adminkindboxservice.AddKindBoxAfterAcceptingRequest"
kindBox, err := s.repo.AddKindBox(ctx, entity.KindBox{
BenefactorID: req.BenefactorID,
KindBoxReqID: req.KindBoxReqID,
Status: entity.KindBoxPendingSendStatus,
})
var kindBoxes []entity.KindBox
for i := 0; i < int(req.Count); i++ {
kindBoxes = append(kindBoxes, entity.KindBox{
KindBoxReqID: req.KindBoxReqID,
BenefactorID: req.BenefactorID,
Type: req.Type,
Status: entity.KindBoxPendingSendStatus,
SerialNumber: ulid.Make().String(),
})
}
kindBoxes, err := s.repo.AddBatchKindBox(ctx, kindBoxes)
if err != nil {
return param.KindBoxAddResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
return param.KindBoxAddAfterAcceptingReqResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
}
return param.KindBoxAddResponse{KindBox: kindBox}, nil
return param.KindBoxAddAfterAcceptingReqResponse{
KindBoxes: kindBoxes,
}, nil
}

View File

@ -1,13 +1,13 @@
package adminkindboxservice
//import (
// import (
// "context"
//
// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
//)
//
//func (s Service) Delete(ctx context.Context, req param.KindBoxDeleteRequest) (param.KindBoxDeleteResponse, error) {
// func (s Service) Delete(ctx context.Context, req param.KindBoxDeleteRequest) (param.KindBoxDeleteResponse, error) {
// // TODO: Does business domain need to delete an kindbox ?
// const op = "adminkindboxservice.Delete"
//

View File

@ -1,13 +1,13 @@
package adminkindboxservice
//import (
// import (
// "context"
//
// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
//)
//
//func (s Service) Get(ctx context.Context, req param.KindBoxGetRequest) (param.KindBoxGetResponse, error) {
// func (s Service) Get(ctx context.Context, req param.KindBoxGetRequest) (param.KindBoxGetResponse, error) {
// const op = "adminkindboxservice.Get"
//
// kindBox, err := s.repo.GetKindBox(ctx, req.KindBoxID)

View File

@ -1,13 +1,13 @@
package adminkindboxservice
//import (
// import (
// "context"
//
// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
//)
//
//func (s Service) GetAll(ctx context.Context, _ param.KindBoxGetAllRequest) (param.KindBoxGetAllResponse, error) {
// func (s Service) GetAll(ctx context.Context, _ param.KindBoxGetAllRequest) (param.KindBoxGetAllResponse, error) {
// const op = "adminkindboxservice.GetAll"
// allKindBox, err := s.repo.GetAllKindBox(ctx)
// if err != nil {

View File

@ -7,11 +7,11 @@ import (
)
type Repository interface {
AddKindBox(ctx context.Context, kindBox entity.KindBox) (entity.KindBox, error)
//UpdateKindBox(ctx context.Context, kindBoxID uint, kindBoxInput entity.KindBox) (entity.KindBox, error)
//DeleteKindBox(ctx context.Context, kindBoxID uint) error
//GetAllKindBox(ctx context.Context) ([]entity.KindBox, error)
//GetKindBox(ctx context.Context, kindBox uint) (entity.KindBox, error)
AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([]entity.KindBox, error)
// UpdateKindBox(ctx context.Context, kindBoxID uint, kindBoxInput entity.KindBox) (entity.KindBox, error)
// DeleteKindBox(ctx context.Context, kindBoxID uint) error
// GetAllKindBox(ctx context.Context) ([]entity.KindBox, error)
// GetKindBox(ctx context.Context, kindBox uint) (entity.KindBox, error)
}
type Service struct {

View File

@ -1,6 +1,6 @@
package adminkindboxservice
//import (
// import (
// "context"
//
// entity "git.gocasts.ir/ebhomengo/niki/entity"
@ -8,7 +8,7 @@ package adminkindboxservice
// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
//)
//
//func (s Service) Update(ctx context.Context, req param.KindBoxUpdateRequest) (param.KindBoxUpdateResponse, error) {
// func (s Service) Update(ctx context.Context, req param.KindBoxUpdateRequest) (param.KindBoxUpdateResponse, error) {
// // TODO: can benefactor update its Request ?
// // TODO: Is Update Mothod Service Responsible to check which kindboxreqID belongs to benefactorID ?
// // TODO: updating data(s) may have side-effect on other entities by masood-keshvary accepted -> rejected

View File

@ -2,6 +2,7 @@ package adminkindboxreqservice
import (
"context"
adminkindboxparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
@ -21,25 +22,22 @@ func (s Service) Accept(ctx context.Context, req param.KindBoxReqAcceptRequest)
return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(gErr)
}
res, kErr := s.kindBoxSvc.Add(ctx, adminkindboxparam.KindBoxAddRequest{
_, kErr := s.kindBoxClient.AddKindBoxAfterAcceptingRequest(ctx, adminkindboxparam.KindBoxAddAfterAcceptingReqRequest{
BenefactorID: kindBoxReq.BenefactorID,
KindBoxReqID: kindBoxReq.ID,
Type: kindBoxReq.KindBoxType,
Count: kindBoxReq.CountAccepted,
})
if kErr != nil {
return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(kErr)
}
return param.KindBoxReqAcceptResponse{
KindBoxReqID: kindBoxReq.ID,
KindBoxReqStatus: kindBoxReq.Status,
CountRequested: kindBoxReq.CountRequested,
CountAccepted: kindBoxReq.CountAccepted,
ReferDate: kindBoxReq.ReferDate,
AddressID: kindBoxReq.AddressID,
KindBoxID: res.KindBox.ID,
KindBoxType: kindBoxReq.KindBoxType,
BenefactorID: kindBoxReq.BenefactorID,
KindBoxStatus: res.KindBox.Status,
KindBoxSerialNumber: res.KindBox.SerialNumber,
KindBoxReqID: kindBoxReq.ID,
KindBoxReqStatus: kindBoxReq.Status,
CountRequested: kindBoxReq.CountRequested,
CountAccepted: kindBoxReq.CountAccepted,
ReferDate: kindBoxReq.ReferDate,
AddressID: kindBoxReq.AddressID,
}, nil
}

View File

@ -2,9 +2,9 @@ package adminkindboxreqservice
import (
"context"
adminkindboxservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box"
"git.gocasts.ir/ebhomengo/niki/entity"
adminkindboxparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
)
type Repository interface {
@ -19,19 +19,24 @@ type Repository interface {
RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) error
}
type KindBoxClient interface {
AddKindBoxAfterAcceptingRequest(ctx context.Context, param adminkindboxparam.KindBoxAddAfterAcceptingReqRequest) (adminkindboxparam.KindBoxAddAfterAcceptingReqResponse, error)
}
// TODO: check validation.
// type BenefactorService interface {
// IsBenefactorExist(ctx context.Context, benefactorID uint) (bool, error)
// }
type Service struct {
repo Repository
kindBoxSvc adminkindboxservice.Service
repo Repository
// benefactorService BenefactorService
kindBoxClient KindBoxClient
}
func New(repository Repository) Service {
func New(repository Repository, kindBoxClient KindBoxClient) Service {
return Service{
repo: repository,
repo: repository,
kindBoxClient: kindBoxClient,
}
}

View File

@ -1,49 +1,39 @@
package adminkindboxvalidator
import (
"errors"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
"github.com/go-ozzo/ozzo-validation/is"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
func (v Validator) ValidateAddRequest(req param.KindBoxAddRequest) (map[string]string, error) {
const op = "adminkindboxvalidator.KindBoxAddRequest"
if err := validation.ValidateStruct(&req,
validation.Field(&req.SerialNumber, validation.Required, is.Alphanumeric),
validation.Field(&req.BenefactorID,
validation.Required,
validation.By(v.doesBenefactorExist)),
validation.Field(&req.SenderID,
validation.Required,
validation.By(v.doesEmployeeExist)),
validation.Field(&req.KindBoxReqID,
validation.Required,
validation.By(v.doesKindBoxRequestExist)),
); err != nil {
fieldErrors := make(map[string]string)
var errV validation.Errors
if errors.As(err, &errV) {
for key, value := range errV {
if value != nil {
fieldErrors[key] = value.Error()
}
}
}
return fieldErrors, richerror.New(op).
WithMessage(errmsg.ErrorMsgInvalidInput).
WithKind(richerror.KindInvalid).
WithMeta(map[string]interface{}{"req": req}).
WithErr(err)
}
return map[string]string{}, nil
}
// func (v Validator) ValidateAddRequest(req param.KindBoxAddRequest) (map[string]string, error) {
// const op = "adminkindboxvalidator.KindBoxAddRequest"
//
// if err := validation.ValidateStruct(&req,
// validation.Field(&req.SerialNumber, validation.Required, is.Alphanumeric),
// validation.Field(&req.BenefactorID,
// validation.Required,
// validation.By(v.doesBenefactorExist)),
//
// validation.Field(&req.SenderID,
// validation.Required,
// validation.By(v.doesEmployeeExist)),
//
// validation.Field(&req.KindBoxReqID,
// validation.Required,
// validation.By(v.doesKindBoxRequestExist)),
// ); err != nil {
// fieldErrors := make(map[string]string)
//
// var errV validation.Errors
// if errors.As(err, &errV) {
// for key, value := range errV {
// if value != nil {
// fieldErrors[key] = value.Error()
// }
// }
// }
//
// return fieldErrors, richerror.New(op).
// WithMessage(errmsg.ErrorMsgInvalidInput).
// WithKind(richerror.KindInvalid).
// WithMeta(map[string]interface{}{"req": req}).
// WithErr(err)
// }
//
// return map[string]string{}, nil
//}

View File

@ -1,45 +1,45 @@
package adminkindboxvalidator
import (
"errors"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
func (v Validator) ValidateDeleteRequest(req param.KindBoxDeleteRequest) (map[string]string, error) {
const op = "adminkindboxvalidator.ValidateDeleteRequest"
if err := validation.ValidateStruct(&req,
validation.Field(&req.BenefactorID,
validation.Required,
validation.By(v.doesBenefactorExist)),
validation.Field(&req.KindBoxID,
validation.Required,
validation.By(v.hasPendingStatus),
validation.By(v.doesKindBoxExist),
validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))),
); err != nil {
fieldErrors := make(map[string]string)
var errV validation.Errors
if errors.As(err, &errV) {
for key, value := range errV {
if value != nil {
fieldErrors[key] = value.Error()
}
}
}
return fieldErrors, richerror.New(op).
WithMessage(errmsg.ErrorMsgInvalidInput).
WithKind(richerror.KindInvalid).
WithMeta(map[string]interface{}{"req": req}).
WithErr(err)
}
return map[string]string{}, nil
}
// import (
// "errors"
//
// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
// errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
// validation "github.com/go-ozzo/ozzo-validation/v4"
//)
//
// func (v Validator) ValidateDeleteRequest(req param.KindBoxDeleteRequest) (map[string]string, error) {
// const op = "adminkindboxvalidator.ValidateDeleteRequest"
//
// if err := validation.ValidateStruct(&req,
// validation.Field(&req.BenefactorID,
// validation.Required,
// validation.By(v.doesBenefactorExist)),
//
// validation.Field(&req.KindBoxID,
// validation.Required,
// validation.By(v.hasPendingStatus),
// validation.By(v.doesKindBoxExist),
// validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))),
// ); err != nil {
// fieldErrors := make(map[string]string)
//
// var errV validation.Errors
// if errors.As(err, &errV) {
// for key, value := range errV {
// if value != nil {
// fieldErrors[key] = value.Error()
// }
// }
// }
//
// return fieldErrors, richerror.New(op).
// WithMessage(errmsg.ErrorMsgInvalidInput).
// WithKind(richerror.KindInvalid).
// WithMeta(map[string]interface{}{"req": req}).
// WithErr(err)
// }
//
// return map[string]string{}, nil
//}

View File

@ -1,44 +1,44 @@
package adminkindboxvalidator
import (
"errors"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
func (v Validator) ValidateGetRequest(req param.KindBoxGetRequest) (map[string]string, error) {
const op = "adminkindboxvalidator.ValidateGetRequest"
if err := validation.ValidateStruct(&req,
validation.Field(&req.BenefactorID,
validation.Required,
validation.By(v.doesBenefactorExist)),
validation.Field(&req.KindBoxID,
validation.Required,
validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID)),
validation.By(v.doesKindBoxExist)),
); err != nil {
fieldErrors := make(map[string]string)
var errV validation.Errors
if errors.As(err, &errV) {
for key, value := range errV {
if value != nil {
fieldErrors[key] = value.Error()
}
}
}
return fieldErrors, richerror.New(op).
WithMessage(errmsg.ErrorMsgInvalidInput).
WithKind(richerror.KindInvalid).
WithMeta(map[string]interface{}{"req": req}).
WithErr(err)
}
return map[string]string{}, nil
}
// import (
// "errors"
//
// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
// errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
// validation "github.com/go-ozzo/ozzo-validation/v4"
//)
//
// func (v Validator) ValidateGetRequest(req param.KindBoxGetRequest) (map[string]string, error) {
// const op = "adminkindboxvalidator.ValidateGetRequest"
//
// if err := validation.ValidateStruct(&req,
// validation.Field(&req.BenefactorID,
// validation.Required,
// validation.By(v.doesBenefactorExist)),
//
// validation.Field(&req.KindBoxID,
// validation.Required,
// validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID)),
// validation.By(v.doesKindBoxExist)),
// ); err != nil {
// fieldErrors := make(map[string]string)
//
// var errV validation.Errors
// if errors.As(err, &errV) {
// for key, value := range errV {
// if value != nil {
// fieldErrors[key] = value.Error()
// }
// }
// }
//
// return fieldErrors, richerror.New(op).
// WithMessage(errmsg.ErrorMsgInvalidInput).
// WithKind(richerror.KindInvalid).
// WithMeta(map[string]interface{}{"req": req}).
// WithErr(err)
// }
//
// return map[string]string{}, nil
//}

View File

@ -1,59 +1,59 @@
package adminkindboxvalidator
import (
"errors"
"git.gocasts.ir/ebhomengo/niki/entity"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
"github.com/go-ozzo/ozzo-validation/is"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
func (v Validator) ValidateUpdateRequest(req param.KindBoxUpdateRequest) (map[string]string, error) {
const op = "adminkindboxvalidator.ValidateUpdateRequest"
if err := validation.ValidateStruct(&req,
validation.Field(&req.BenefactorID,
validation.Required,
validation.By(v.doesBenefactorExist)),
validation.Field(&req.KindBoxID,
validation.Required,
validation.By(v.doesKindBoxExist),
validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))),
validation.Field(&req.SerialNumber, is.Alphanumeric),
validation.Field(&req.TotalAmount, validation.Min(0)),
validation.Field(&req.SenderID,
validation.By(v.doesEmployeeExist)),
validation.Field(&req.ReceiverID,
validation.By(v.doesEmployeeExist)),
validation.Field(&req.Status,
validation.In(entity.AllKindBoxStatus())),
); err != nil {
fieldErrors := make(map[string]string)
var errV validation.Errors
if errors.As(err, &errV) {
for key, value := range errV {
if value != nil {
fieldErrors[key] = value.Error()
}
}
}
return fieldErrors, richerror.New(op).
WithMessage(errmsg.ErrorMsgInvalidInput).
WithKind(richerror.KindInvalid).
WithMeta(map[string]interface{}{"req": req}).
WithErr(err)
}
return map[string]string{}, nil
}
// import (
// "errors"
//
// "git.gocasts.ir/ebhomengo/niki/entity"
// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
// errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
// "github.com/go-ozzo/ozzo-validation/is"
// validation "github.com/go-ozzo/ozzo-validation/v4"
//)
//
// func (v Validator) ValidateUpdateRequest(req param.KindBoxUpdateRequest) (map[string]string, error) {
// const op = "adminkindboxvalidator.ValidateUpdateRequest"
//
// if err := validation.ValidateStruct(&req,
// validation.Field(&req.BenefactorID,
// validation.Required,
// validation.By(v.doesBenefactorExist)),
//
// validation.Field(&req.KindBoxID,
// validation.Required,
// validation.By(v.doesKindBoxExist),
// validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))),
//
// validation.Field(&req.SerialNumber, is.Alphanumeric),
//
// validation.Field(&req.TotalAmount, validation.Min(0)),
//
// validation.Field(&req.SenderID,
// validation.By(v.doesEmployeeExist)),
//
// validation.Field(&req.ReceiverID,
// validation.By(v.doesEmployeeExist)),
//
// validation.Field(&req.Status,
// validation.In(entity.AllKindBoxStatus())),
// ); err != nil {
// fieldErrors := make(map[string]string)
//
// var errV validation.Errors
// if errors.As(err, &errV) {
// for key, value := range errV {
// if value != nil {
// fieldErrors[key] = value.Error()
// }
// }
// }
//
// return fieldErrors, richerror.New(op).
// WithMessage(errmsg.ErrorMsgInvalidInput).
// WithKind(richerror.KindInvalid).
// WithMeta(map[string]interface{}{"req": req}).
// WithErr(err)
// }
//
// return map[string]string{}, nil
//}

View File

@ -1,12 +1,5 @@
package adminkindboxvalidator
import (
"fmt"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
type Repository interface {
KindBoxRequestExist(id uint) (bool, error)
EmployeeExist(id uint) (bool, error)
@ -25,72 +18,72 @@ func New(repo Repository) Validator {
return Validator{repo: repo}
}
func (v Validator) doesKindBoxRequestExist(value interface{}) error {
receiverID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
_, err := v.repo.KindBoxRequestExist(receiverID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
return nil
}
func (v Validator) doesEmployeeExist(value interface{}) error {
senderID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
_, err := v.repo.EmployeeExist(senderID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
return nil
}
func (v Validator) doesBenefactorExist(value interface{}) error {
benefactorID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
_, err := v.repo.BenefactorExist(benefactorID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
return nil
}
func (v Validator) doesKindBoxExist(value interface{}) error {
kindboxID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
_, err := v.repo.KindBoxExist(kindboxID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
return nil
}
func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc {
return func(value interface{}) error {
kbID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
_, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
return nil
}
}
// func (v Validator) doesKindBoxRequestExist(value interface{}) error {
// receiverID, ok := value.(uint)
// if !ok {
// return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
// }
// _, err := v.repo.KindBoxRequestExist(receiverID)
// if err != nil {
// return fmt.Errorf(errmsg.ErrorMsgNotFound)
// }
//
// return nil
//}
//
// func (v Validator) doesEmployeeExist(value interface{}) error {
// senderID, ok := value.(uint)
// if !ok {
// return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
// }
// _, err := v.repo.EmployeeExist(senderID)
// if err != nil {
// return fmt.Errorf(errmsg.ErrorMsgNotFound)
// }
//
// return nil
//}
//
// func (v Validator) doesBenefactorExist(value interface{}) error {
// benefactorID, ok := value.(uint)
// if !ok {
// return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
// }
// _, err := v.repo.BenefactorExist(benefactorID)
// if err != nil {
// return fmt.Errorf(errmsg.ErrorMsgNotFound)
// }
//
// return nil
//}
//
//func (v Validator) doesKindBoxExist(value interface{}) error {
// kindboxID, ok := value.(uint)
// if !ok {
// return fmt.Errorf(errmsg.ErrorMsgNotFound)
// }
// _, err := v.repo.KindBoxExist(kindboxID)
// if err != nil {
// return fmt.Errorf(errmsg.ErrorMsgNotFound)
// }
//
// return nil
//}
//
//func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc {
// return func(value interface{}) error {
// kbID, ok := value.(uint)
// if !ok {
// return fmt.Errorf(errmsg.ErrorMsgNotFound)
// }
// _, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbID)
// if err != nil {
// return fmt.Errorf(errmsg.ErrorMsgNotFound)
// }
//
// return nil
// }
//}
// TODO: this temperary to ignore linter error. (unused function)
// func (v Validator) hasCorrectStatus(value interface{}) error {
@ -106,15 +99,15 @@ func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.R
// return nil
// }
func (v Validator) hasPendingStatus(value interface{}) error {
kindboxID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
_, err := v.repo.PendingStatus(kindboxID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
return nil
}
// func (v Validator) hasPendingStatus(value interface{}) error {
// kindboxID, ok := value.(uint)
// if !ok {
// return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
// }
// _, err := v.repo.PendingStatus(kindboxID)
// if err != nil {
// return fmt.Errorf(errmsg.ErrorMsgNotFound)
// }
//
// return nil
//}