forked from ebhomengo/niki
1
0
Fork 0

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

View File

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

View File

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

View File

@ -1,32 +1,32 @@
package adminkindboxhandler package adminkindboxhandler
import ( // import (
"net/http" // "net/http"
//
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" // param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" // httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
echo "github.com/labstack/echo/v4" // echo "github.com/labstack/echo/v4"
) //)
//
func (h Handler) Update(c echo.Context) error { //func (h Handler) Update(c echo.Context) error {
var req param.KindBoxUpdateRequest // var req param.KindBoxUpdateRequest
if bErr := c.Bind(&req); bErr != nil { // if bErr := c.Bind(&req); bErr != nil {
return echo.NewHTTPError(http.StatusBadRequest) // return echo.NewHTTPError(http.StatusBadRequest)
} // }
if fieldErrors, err := h.adminKindBoxVld.ValidateUpdateRequest(req); err != nil { // if fieldErrors, err := h.adminKindBoxVld.ValidateUpdateRequest(req); err != nil {
msg, code := httpmsg.Error(err) // msg, code := httpmsg.Error(err)
//
return c.JSON(code, echo.Map{ // return c.JSON(code, echo.Map{
"message": msg, // "message": msg,
"errors": fieldErrors, // "errors": fieldErrors,
}) // })
} // }
resp, sErr := h.adminKindBoxSvc.Update(c.Request().Context(), req) // resp, sErr := h.adminKindBoxSvc.Update(c.Request().Context(), req)
if sErr != nil { // if sErr != nil {
msg, code := httpmsg.Error(sErr) // msg, code := httpmsg.Error(sErr)
//
return echo.NewHTTPError(code, msg) // return echo.NewHTTPError(code, msg)
} // }
//
return c.JSON(http.StatusCreated, resp) // 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/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // 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/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.17.0 // 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/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/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/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.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pascaldekloe/goe v0.1.0/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 h1:7utD74fnzVc/cpcyy8sjrlFr5vYpypUixARcHIMIGuI=
github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE= 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= 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" mysqladdress "git.gocasts.ir/ebhomengo/niki/repository/mysql/address"
mysqladmin "git.gocasts.ir/ebhomengo/niki/repository/mysql/admin" mysqladmin "git.gocasts.ir/ebhomengo/niki/repository/mysql/admin"
mysqlbenefactor "git.gocasts.ir/ebhomengo/niki/repository/mysql/benefactor" 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" mysqlkindboxreq "git.gocasts.ir/ebhomengo/niki/repository/mysql/kind_box_req"
redisotp "git.gocasts.ir/ebhomengo/niki/repository/redis/redis_otp" redisotp "git.gocasts.ir/ebhomengo/niki/repository/redis/redis_otp"
adminservice "git.gocasts.ir/ebhomengo/niki/service/admin/admin" 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" adminkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box_req"
adminauthservice "git.gocasts.ir/ebhomengo/niki/service/auth/admin" adminauthservice "git.gocasts.ir/ebhomengo/niki/service/auth/admin"
authservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor" authservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor"
@ -66,8 +68,9 @@ func setupServices(cfg config.Config) (
benefactorKindBoxReqMysql := mysqlkindboxreq.New(MysqlRepo) benefactorKindBoxReqMysql := mysqlkindboxreq.New(MysqlRepo)
benefactorKindBoxReqSvc = benefactorkindboxreqservice.New(benefactorKindBoxReqMysql) benefactorKindBoxReqSvc = benefactorkindboxreqservice.New(benefactorKindBoxReqMysql)
benefactorKindBoxReqVld = benefactorkindboxreqvalidator.New(benefactorKindBoxReqMysql, benefactorSvc, benefactorAddressSvc) benefactorKindBoxReqVld = benefactorkindboxreqvalidator.New(benefactorKindBoxReqMysql, benefactorSvc, benefactorAddressSvc)
mysqlkindboxRepo := mysqlkindbox.New(MysqlRepo)
adminKindBoxReqSvc = adminkindboxreqservice.New(benefactorKindBoxReqMysql) adminkindboxsvc := adminkindboxservice.New(mysqlkindboxRepo)
adminKindBoxReqSvc = adminkindboxreqservice.New(benefactorKindBoxReqMysql, adminkindboxsvc)
adminKindBoxReqVld = adminkindboxreqvalidator.New(benefactorKindBoxReqMysql) adminKindBoxReqVld = adminkindboxreqvalidator.New(benefactorKindBoxReqMysql)
adminAuthSvc = adminauthservice.New(cfg.AdminAuth) adminAuthSvc = adminauthservice.New(cfg.AdminAuth)

View File

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

View File

@ -1,8 +1,9 @@
package adminkindboxreqparam package adminkindboxreqparam
import ( import (
"git.gocasts.ir/ebhomengo/niki/entity"
"time" "time"
"git.gocasts.ir/ebhomengo/niki/entity"
) )
type KindBoxReqAcceptRequest struct { type KindBoxReqAcceptRequest struct {
@ -17,10 +18,4 @@ type KindBoxReqAcceptResponse struct {
CountAccepted uint `json:"count_accepted"` CountAccepted uint `json:"count_accepted"`
ReferDate time.Time `json:"refer_date"` ReferDate time.Time `json:"refer_date"`
AddressID uint `json:"address_id"` 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 ( import (
"context" "context"
"git.gocasts.ir/ebhomengo/niki/entity" "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) { func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([]entity.KindBox, error) {
return entity.KindBox{}, nil 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" entity "git.gocasts.ir/ebhomengo/niki/entity"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" 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) { func (s Service) AddKindBoxAfterAcceptingRequest(ctx context.Context, req param.KindBoxAddAfterAcceptingReqRequest) (param.KindBoxAddAfterAcceptingReqResponse, error) {
const op = "adminkindboxservice.Add" const op = "adminkindboxservice.AddKindBoxAfterAcceptingRequest"
kindBox, err := s.repo.AddKindBox(ctx, entity.KindBox{ var kindBoxes []entity.KindBox
BenefactorID: req.BenefactorID, for i := 0; i < int(req.Count); i++ {
kindBoxes = append(kindBoxes, entity.KindBox{
KindBoxReqID: req.KindBoxReqID, KindBoxReqID: req.KindBoxReqID,
BenefactorID: req.BenefactorID,
Type: req.Type,
Status: entity.KindBoxPendingSendStatus, Status: entity.KindBoxPendingSendStatus,
SerialNumber: ulid.Make().String(),
}) })
}
kindBoxes, err := s.repo.AddBatchKindBox(ctx, kindBoxes)
if err != nil { 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 package adminkindboxservice
//import ( // import (
// "context" // "context"
// //
// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" // param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" // 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 ? // // TODO: Does business domain need to delete an kindbox ?
// const op = "adminkindboxservice.Delete" // const op = "adminkindboxservice.Delete"
// //

View File

@ -1,13 +1,13 @@
package adminkindboxservice package adminkindboxservice
//import ( // import (
// "context" // "context"
// //
// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" // param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" // 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" // const op = "adminkindboxservice.Get"
// //
// kindBox, err := s.repo.GetKindBox(ctx, req.KindBoxID) // kindBox, err := s.repo.GetKindBox(ctx, req.KindBoxID)

View File

@ -1,13 +1,13 @@
package adminkindboxservice package adminkindboxservice
//import ( // import (
// "context" // "context"
// //
// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" // param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" // 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" // const op = "adminkindboxservice.GetAll"
// allKindBox, err := s.repo.GetAllKindBox(ctx) // allKindBox, err := s.repo.GetAllKindBox(ctx)
// if err != nil { // if err != nil {

View File

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

View File

@ -1,6 +1,6 @@
package adminkindboxservice package adminkindboxservice
//import ( // import (
// "context" // "context"
// //
// entity "git.gocasts.ir/ebhomengo/niki/entity" // entity "git.gocasts.ir/ebhomengo/niki/entity"
@ -8,7 +8,7 @@ package adminkindboxservice
// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" // 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: can benefactor update its Request ?
// // TODO: Is Update Mothod Service Responsible to check which kindboxreqID belongs to benefactorID ? // // 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 // // 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 ( import (
"context" "context"
adminkindboxparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" adminkindboxparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
@ -21,9 +22,11 @@ func (s Service) Accept(ctx context.Context, req param.KindBoxReqAcceptRequest)
return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(gErr) 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, BenefactorID: kindBoxReq.BenefactorID,
KindBoxReqID: kindBoxReq.ID, KindBoxReqID: kindBoxReq.ID,
Type: kindBoxReq.KindBoxType,
Count: kindBoxReq.CountAccepted,
}) })
if kErr != nil { if kErr != nil {
return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(kErr) return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(kErr)
@ -36,10 +39,5 @@ func (s Service) Accept(ctx context.Context, req param.KindBoxReqAcceptRequest)
CountAccepted: kindBoxReq.CountAccepted, CountAccepted: kindBoxReq.CountAccepted,
ReferDate: kindBoxReq.ReferDate, ReferDate: kindBoxReq.ReferDate,
AddressID: kindBoxReq.AddressID, AddressID: kindBoxReq.AddressID,
KindBoxID: res.KindBox.ID,
KindBoxType: kindBoxReq.KindBoxType,
BenefactorID: kindBoxReq.BenefactorID,
KindBoxStatus: res.KindBox.Status,
KindBoxSerialNumber: res.KindBox.SerialNumber,
}, nil }, nil
} }

View File

@ -2,9 +2,9 @@ package adminkindboxreqservice
import ( import (
"context" "context"
adminkindboxservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
adminkindboxparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
) )
type Repository interface { type Repository interface {
@ -19,6 +19,10 @@ type Repository interface {
RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) error 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. // TODO: check validation.
// type BenefactorService interface { // type BenefactorService interface {
// IsBenefactorExist(ctx context.Context, benefactorID uint) (bool, error) // IsBenefactorExist(ctx context.Context, benefactorID uint) (bool, error)
@ -26,12 +30,13 @@ type Repository interface {
type Service struct { type Service struct {
repo Repository repo Repository
kindBoxSvc adminkindboxservice.Service
// benefactorService BenefactorService // benefactorService BenefactorService
kindBoxClient KindBoxClient
} }
func New(repository Repository) Service { func New(repository Repository, kindBoxClient KindBoxClient) Service {
return Service{ return Service{
repo: repository, repo: repository,
kindBoxClient: kindBoxClient,
} }
} }

View File

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

View File

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

View File

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

View File

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