forked from ebhomengo/niki
1
0
Fork 0

add service and repo layer for accepting kind box request

This commit is contained in:
Abolfazl Nourzad 2024-01-22 11:44:57 +03:30
parent 6e0d616036
commit e3109b1972
Signed by: abolfazl
GPG Key ID: 183534166EB62E0B
33 changed files with 547 additions and 389 deletions

View File

@ -1,10 +1,11 @@
package adminhandler package adminhandler
import ( import (
"net/http"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin" adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"net/http"
) )
func (h Handler) LoginByPhoneNumber(c echo.Context) error { func (h Handler) LoginByPhoneNumber(c echo.Context) error {

View File

@ -1,10 +1,11 @@
package adminhandler package adminhandler
import ( import (
"net/http"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin" adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"net/http"
) )
func (h Handler) Register(c echo.Context) error { func (h Handler) Register(c echo.Context) error {

View File

@ -0,0 +1,35 @@
package adminkindboxreqhandler
import (
"net/http"
"strconv"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
"github.com/labstack/echo/v4"
)
func (h Handler) Accept(c echo.Context) error {
var req param.KindBoxReqAcceptRequest
if bErr := c.Bind(&req); bErr != nil {
return echo.NewHTTPError(http.StatusBadRequest)
}
num, _ := strconv.ParseUint(c.Param("id"), 36, 10)
req.ID = uint(num)
// if fieldErrors, err := h.adminKindBoxReqVld.ValidateGetRequest(req); err != nil {
// msg, code := httpmsg.Error(err)
//
// return c.JSON(code, echo.Map{
// "message": msg,
// "errors": fieldErrors,
// })
//}
resp, sErr := h.adminKindBoxReqSvc.Accept(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,24 @@
package adminkindboxreqhandler package adminkindboxreqhandler
import ( // func (h Handler) Get(c echo.Context) error {
"net/http" // var req param.KindBoxReqGetRequest
// if bErr := c.Bind(&req); bErr != nil {
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" // return echo.NewHTTPError(http.StatusBadRequest)
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" // }
echo "github.com/labstack/echo/v4" // if fieldErrors, err := h.adminKindBoxReqVld.ValidateGetRequest(req); err != nil {
) // msg, code := httpmsg.Error(err)
//
func (h Handler) Get(c echo.Context) error { // return c.JSON(code, echo.Map{
var req param.KindBoxReqGetRequest // "message": msg,
if bErr := c.Bind(&req); bErr != nil { // "errors": fieldErrors,
return echo.NewHTTPError(http.StatusBadRequest) // })
} // }
if fieldErrors, err := h.adminKindBoxReqVld.ValidateGetRequest(req); err != nil { // resp, sErr := h.adminKindBoxReqSvc.Get(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.adminKindBoxReqSvc.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,25 @@
package adminkindboxreqhandler package adminkindboxreqhandler
import ( // import (
"net/http" // "net/http"
//
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" // param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
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) GetAll(c echo.Context) error { // func (h Handler) GetAll(c echo.Context) error {
var req param.KindBoxReqGetAllRequest // var req param.KindBoxReqGetAllRequest
if bErr := c.Bind(&req); bErr != nil { // if bErr := c.Bind(&req); bErr != nil {
return echo.NewHTTPError(http.StatusBadRequest) // return echo.NewHTTPError(http.StatusBadRequest)
} // }
//
resp, sErr := h.adminKindBoxReqSvc.GetAll(c.Request().Context(), req) // resp, sErr := h.adminKindBoxReqSvc.GetAll(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

@ -9,8 +9,9 @@ func (h Handler) SetRoutes(e *echo.Echo) {
//nolint:gocritic //nolint:gocritic
//r.POST("/", h.Add).Name = "admin-addkindboxreq" //r.POST("/", h.Add).Name = "admin-addkindboxreq"
r.GET("/:id", h.Get).Name = "admin-getkindboxreqbyid" //r.GET("/:id", h.Get).Name = "admin-getkindboxreqbyid"
r.GET("/", h.GetAll).Name = "admin-getallkindboxreq" //r.GET("/", h.GetAll).Name = "admin-getallkindboxreq"
//nolint:gocritic //nolint:gocritic
//r.PATCH("/:id", h.Update).Name = "admin-updatekindboxreq" //r.PATCH("/:id", h.Update).Name = "admin-updatekindboxreq"
r.PATCH("/accept-kind-box-req/:id", h.Accept)
} }

View File

@ -2,19 +2,22 @@ package httpserver
import ( import (
"fmt" "fmt"
adminhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/admin"
adminservice "git.gocasts.ir/ebhomengo/niki/service/admin/admin"
adminauthservice "git.gocasts.ir/ebhomengo/niki/service/auth/admin"
adminvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/admin"
config "git.gocasts.ir/ebhomengo/niki/config" config "git.gocasts.ir/ebhomengo/niki/config"
adminhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/admin"
adminkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/kind_box_req"
benefactorbasehandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/base" benefactorbasehandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/base"
benefactorhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/benefactor" benefactorhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/benefactor"
benefactorkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/kind_box_req" benefactorkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/kind_box_req"
adminservice "git.gocasts.ir/ebhomengo/niki/service/admin/admin"
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" authservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor"
benefactoraddressservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/address" benefactoraddressservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/address"
benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor" benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor"
benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req" benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req"
adminvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/admin"
adminkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box_req"
benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor" benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor"
benefactorkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/kind_box_req" benefactorkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/kind_box_req"
echo "github.com/labstack/echo/v4" echo "github.com/labstack/echo/v4"
@ -28,6 +31,7 @@ type Server struct {
benefactorKindBoxReqHandler benefactorkindboxreqhandler.Handler benefactorKindBoxReqHandler benefactorkindboxreqhandler.Handler
benefactorBaseHandler benefactorbasehandler.Handler benefactorBaseHandler benefactorbasehandler.Handler
adminHandler adminhandler.Handler adminHandler adminhandler.Handler
adminKindBoxReqHandler adminkindboxreqhandler.Handler
} }
func New( func New(
@ -41,6 +45,8 @@ func New(
adminSvc adminservice.Service, adminSvc adminservice.Service,
adminVld adminvalidator.Validator, adminVld adminvalidator.Validator,
adminAuthSvc adminauthservice.Service, adminAuthSvc adminauthservice.Service,
adminKinBoxReqSvc adminkindboxreqservice.Service,
adminKinBoxReqVld adminkindboxreqvalidator.Validator,
) Server { ) Server {
return Server{ return Server{
Router: echo.New(), Router: echo.New(),
@ -49,6 +55,7 @@ func New(
benefactorKindBoxReqHandler: benefactorkindboxreqhandler.New(cfg.Auth, authSvc, benefactorKindBoxReqSvc, benefactorKindBoxReqVld), benefactorKindBoxReqHandler: benefactorkindboxreqhandler.New(cfg.Auth, authSvc, benefactorKindBoxReqSvc, benefactorKindBoxReqVld),
benefactorBaseHandler: benefactorbasehandler.New(benefactorAddressSvc), benefactorBaseHandler: benefactorbasehandler.New(benefactorAddressSvc),
adminHandler: adminhandler.New(cfg.AdminAuth, adminAuthSvc, adminSvc, adminVld), adminHandler: adminhandler.New(cfg.AdminAuth, adminAuthSvc, adminSvc, adminVld),
adminKindBoxReqHandler: adminkindboxreqhandler.New(cfg.Auth, authSvc, adminKinBoxReqSvc, adminKinBoxReqVld),
} }
} }
@ -63,6 +70,7 @@ func (s Server) Serve() {
s.benefactorKindBoxReqHandler.SetRoutes(s.Router) s.benefactorKindBoxReqHandler.SetRoutes(s.Router)
s.benefactorBaseHandler.SetRoutes(s.Router) s.benefactorBaseHandler.SetRoutes(s.Router)
s.adminHandler.SetRoutes(s.Router) s.adminHandler.SetRoutes(s.Router)
s.adminKindBoxReqHandler.SetRoutes(s.Router)
// Start server // Start server
address := fmt.Sprintf(":%d", s.config.HTTPServer.Port) address := fmt.Sprintf(":%d", s.config.HTTPServer.Port)

View File

@ -14,7 +14,6 @@ type Admin struct {
} }
func (a *Admin) GetPassword() string { func (a *Admin) GetPassword() string {
return a.password return a.password
} }

View File

@ -12,4 +12,5 @@ type KindBoxReq struct {
Description string Description string
ReferDate time.Time ReferDate time.Time
AddressID uint AddressID uint
CreatedAt time.Time
} }

10
main.go
View File

@ -14,12 +14,14 @@ import (
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"
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"
benefactoraddressservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/address" benefactoraddressservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/address"
benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor" benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor"
benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req" benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req"
adminvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/admin" adminvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/admin"
adminkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box_req"
benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor" benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor"
benefactorkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/kind_box_req" benefactorkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/kind_box_req"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
@ -32,9 +34,9 @@ func main() {
mgr.Up() mgr.Up()
authSvc, benefactorSvc, benefactorVld, benefactorKindBoxReqSvc, benefactorKindBoxReqVld, benefactorAddressSvc, authSvc, benefactorSvc, benefactorVld, benefactorKindBoxReqSvc, benefactorKindBoxReqVld, benefactorAddressSvc,
adminSvc, adminVld, adminAuthSvc := setupServices(cfg) adminSvc, adminVld, adminAuthSvc, adminKindBoxReqSvc, adminKindBoxReqVld := setupServices(cfg)
server := httpserver.New(cfg, benefactorSvc, benefactorVld, authSvc, benefactorKindBoxReqSvc, benefactorKindBoxReqVld, server := httpserver.New(cfg, benefactorSvc, benefactorVld, authSvc, benefactorKindBoxReqSvc, benefactorKindBoxReqVld,
benefactorAddressSvc, adminSvc, adminVld, adminAuthSvc) benefactorAddressSvc, adminSvc, adminVld, adminAuthSvc, adminKindBoxReqSvc, adminKindBoxReqVld)
server.Serve() server.Serve()
} }
@ -43,6 +45,7 @@ func setupServices(cfg config.Config) (
authSvc authservice.Service, benefactorSvc benefactorservice.Service, benefactorVld benefactorvalidator.Validator, authSvc authservice.Service, benefactorSvc benefactorservice.Service, benefactorVld benefactorvalidator.Validator,
benefactorKindBoxReqSvc benefactorkindboxreqservice.Service, benefactorKindBoxReqVld benefactorkindboxreqvalidator.Validator, benefactorKindBoxReqSvc benefactorkindboxreqservice.Service, benefactorKindBoxReqVld benefactorkindboxreqvalidator.Validator,
benefactorAddressSvc benefactoraddressservice.Service, adminSvc adminservice.Service, adminVld adminvalidator.Validator, adminAuthSvc adminauthservice.Service, benefactorAddressSvc benefactoraddressservice.Service, adminSvc adminservice.Service, adminVld adminvalidator.Validator, adminAuthSvc adminauthservice.Service,
adminKindBoxReqSvc adminkindboxreqservice.Service, adminKindBoxReqVld adminkindboxreqvalidator.Validator,
) { ) {
authSvc = authservice.New(cfg.Auth) authSvc = authservice.New(cfg.Auth)
@ -64,6 +67,9 @@ func setupServices(cfg config.Config) (
benefactorKindBoxReqSvc = benefactorkindboxreqservice.New(benefactorKindBoxReqMysql) benefactorKindBoxReqSvc = benefactorkindboxreqservice.New(benefactorKindBoxReqMysql)
benefactorKindBoxReqVld = benefactorkindboxreqvalidator.New(benefactorKindBoxReqMysql, benefactorSvc, benefactorAddressSvc) benefactorKindBoxReqVld = benefactorkindboxreqvalidator.New(benefactorKindBoxReqMysql, benefactorSvc, benefactorAddressSvc)
adminKindBoxReqSvc = adminkindboxreqservice.New(benefactorKindBoxReqMysql)
adminKindBoxReqVld = adminkindboxreqvalidator.New(benefactorKindBoxReqMysql)
adminAuthSvc = adminauthservice.New(cfg.AdminAuth) adminAuthSvc = adminauthservice.New(cfg.AdminAuth)
adminMysql := mysqladmin.New(MysqlRepo) adminMysql := mysqladmin.New(MysqlRepo)
adminVld = adminvalidator.New(adminMysql) adminVld = adminvalidator.New(adminMysql)

View File

@ -0,0 +1,25 @@
package adminkindboxreqparam
import (
"time"
"git.gocasts.ir/ebhomengo/niki/entity"
)
type KindBoxReqAcceptRequest struct {
ID uint `json:"id"`
Description string `json:"description"`
CountAccepted uint `json:"count_accepted"`
}
type KindBoxReqAcceptResponse struct {
ID uint `json:"id"`
KindBoxType entity.KindBoxType `json:"kind_box_type"`
CountRequested uint `json:"count_requested"`
CountAccepted uint `json:"count_accepted"`
BenefactorID uint `json:"benefactor_id"`
Status entity.KindBoxReqStatus `json:"status"`
Description string `json:"description"`
ReferDate time.Time `json:"refer_date"`
AddressID uint `json:"address_id"`
}

View File

@ -2,6 +2,7 @@ package mysqladmin
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" 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"

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"database/sql" "database/sql"
"errors" "errors"
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"
) )

View File

@ -4,11 +4,12 @@ import (
"context" "context"
"database/sql" "database/sql"
"errors" "errors"
"time"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
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"
"git.gocasts.ir/ebhomengo/niki/repository/mysql" "git.gocasts.ir/ebhomengo/niki/repository/mysql"
"time"
) )
func (d DB) GetAdminByPhoneNumber(ctx context.Context, phoneNumber string) (entity.Admin, error) { func (d DB) GetAdminByPhoneNumber(ctx context.Context, phoneNumber string) (entity.Admin, error) {

View File

@ -2,7 +2,6 @@ package mysqlkindboxreq
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" 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"
@ -25,3 +24,31 @@ func (d DB) AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (en
return kindBoxReq, nil return kindBoxReq, nil
} }
func (d DB) AcceptKindBoxReq(ctx context.Context, kindBoxReqID uint, countAccepted uint, description string) error {
op := richerror.Op("mysqlkindboxreq.AcceptKindBoxReq")
statement, err := d.conn.Conn().
Prepare(`update kind_box_reqs set count_accepted = ? , description = ? , status = ? where id = ?`)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
_, eErr := statement.ExecContext(ctx, countAccepted, description, entity.KindBoxReqAcceptedStatus.String(), kindBoxReqID)
if eErr != nil {
return richerror.New(op).WithErr(eErr).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
return nil
}
func (d DB) FindByID(ctx context.Context, id uint) (entity.KindBoxReq, error) {
op := richerror.Op("mysqlkindboxreq.findByID")
row := d.conn.Conn().QueryRowContext(ctx, `select * from kind_box_reqs where id = ?`, id)
k, err := scanKindBoxReq(row)
if err != nil {
return entity.KindBoxReq{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
}
return k, nil
}

View File

@ -0,0 +1,19 @@
package mysqlkindboxreq
import (
"git.gocasts.ir/ebhomengo/niki/entity"
"git.gocasts.ir/ebhomengo/niki/repository/mysql"
)
func scanKindBoxReq(scanner mysql.Scanner) (entity.KindBoxReq, error) {
var kindBoxReq entity.KindBoxReq
var kindBoxStatus string
var kindBoxType string
err := scanner.Scan(&kindBoxReq.ID, &kindBoxReq.BenefactorID, &kindBoxType, &kindBoxReq.AddressID, &kindBoxReq.CountRequested, &kindBoxReq.CountAccepted,
&kindBoxReq.Description,
&kindBoxReq.ReferDate, &kindBoxStatus, &kindBoxReq.CreatedAt)
kindBoxReq.Status = entity.MapToKindBoxReqStatus(kindBoxStatus)
kindBoxReq.KindBoxType = entity.MapToKindBoxType(kindBoxType)
return kindBoxReq, err
}

View File

@ -2,6 +2,7 @@ package adminservice
import ( import (
"context" "context"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin" adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
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"
@ -36,5 +37,4 @@ func (s Service) LoginWithPhoneNumber(ctx context.Context, req adminserviceparam
RefreshToken: refreshToken, RefreshToken: refreshToken,
}, },
}, nil }, nil
} }

View File

@ -2,6 +2,7 @@ package adminservice
import ( import (
"context" "context"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin" adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"

View File

@ -3,6 +3,7 @@ package adminservice
import ( import (
"context" "context"
"fmt" "fmt"
"git.gocasts.ir/ebhomengo/niki/config" "git.gocasts.ir/ebhomengo/niki/config"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
@ -39,6 +40,7 @@ func GenerateHash(password *string) error {
return nil return nil
} }
func CompareHash(hashedPassword, password string) error { func CompareHash(hashedPassword, password string) error {
return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password)) return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
} }

View File

@ -0,0 +1,35 @@
package adminkindboxreqservice
import (
"context"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) Accept(ctx context.Context, req param.KindBoxReqAcceptRequest) (param.KindBoxReqAcceptResponse, error) {
const op = "adminkindboxreqservice.Accept"
err := s.repo.AcceptKindBoxReq(ctx, req.ID, req.CountAccepted, req.Description)
if err != nil {
return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(err)
}
// fire new event to create a kind-box.
//get kind box req
kindBoxReq, gErr := s.repo.FindByID(ctx, req.ID)
if gErr != nil {
return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(err)
}
return param.KindBoxReqAcceptResponse{
ID: kindBoxReq.ID,
KindBoxType: kindBoxReq.KindBoxType,
CountRequested: kindBoxReq.CountRequested,
CountAccepted: kindBoxReq.CountAccepted,
BenefactorID: kindBoxReq.BenefactorID,
Status: kindBoxReq.Status,
Description: kindBoxReq.Description,
ReferDate: kindBoxReq.ReferDate,
AddressID: kindBoxReq.AddressID,
}, nil
}

View File

@ -1,20 +1,21 @@
package adminkindboxreqservice package adminkindboxreqservice
import ( //
"context" // import (
// "context"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" //
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" // param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
) // richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
//)
func (s Service) Delete(ctx context.Context, req param.KindBoxReqDeleteRequest) (param.KindBoxReqDeleteResponse, error) { //
// TODO: Does business domain need to delete an kindboxreq ? // func (s Service) Delete(ctx context.Context, req param.KindBoxReqDeleteRequest) (param.KindBoxReqDeleteResponse, error) {
const op = "adminkindboxreqservice.Delete" // // TODO: Does business domain need to delete an kindboxreq ?
// const op = "adminkindboxreqservice.Delete"
dErr := s.repo.DeleteKindBoxReq(ctx, req.KindBoxReqID) //
if dErr != nil { // dErr := s.repo.DeleteKindBoxReq(ctx, req.KindBoxReqID)
return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithErr(dErr).WithKind(richerror.KindUnexpected) // if dErr != nil {
} // return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithErr(dErr).WithKind(richerror.KindUnexpected)
// }
return param.KindBoxReqDeleteResponse{}, nil //
} // return param.KindBoxReqDeleteResponse{}, nil
//}

View File

@ -1,20 +1,21 @@
package adminkindboxreqservice package adminkindboxreqservice
import ( //
"context" // import (
// "context"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" //
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" // param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
) // richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
//)
func (s Service) Get(ctx context.Context, req param.KindBoxReqGetRequest) (param.KindBoxReqGetResponse, error) { //
const op = "adminkindboxreqservice.Get" // func (s Service) Get(ctx context.Context, req param.KindBoxReqGetRequest) (param.KindBoxReqGetResponse, error) {
// const op = "adminkindboxreqservice.Get"
// TODO : ref to service.Update() //
kindBoxReq, err := s.repo.GetKindBoxReq(ctx, req.KindBoxReqID) // // TODO : ref to service.Update()
if err != nil { // kindBoxReq, err := s.repo.GetKindBoxReq(ctx, req.KindBoxReqID)
return param.KindBoxReqGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) // if err != nil {
} // return param.KindBoxReqGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
// }
return param.KindBoxReqGetResponse{KindBoxReq: kindBoxReq}, nil //
} // return param.KindBoxReqGetResponse{KindBoxReq: kindBoxReq}, nil
//}

View File

@ -1,20 +1,21 @@
package adminkindboxreqservice package adminkindboxreqservice
import ( //
"context" // import (
// "context"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" //
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" // param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
) // richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
//)
// TODO: Pagination, Filters, Sort. //
func (s Service) GetAll(ctx context.Context, _ param.KindBoxReqGetAllRequest) (param.KindBoxReqGetAllResponse, error) { //// TODO: Pagination, Filters, Sort.
const op = "adminkindboxreqservice.GetAll" // func (s Service) GetAll(ctx context.Context, _ param.KindBoxReqGetAllRequest) (param.KindBoxReqGetAllResponse, error) {
// const op = "adminkindboxreqservice.GetAll"
allKindBoxReq, err := s.repo.GetAllKindBoxReq(ctx) //
if err != nil { // allKindBoxReq, err := s.repo.GetAllKindBoxReq(ctx)
return param.KindBoxReqGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) // if err != nil {
} // return param.KindBoxReqGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
// }
return param.KindBoxReqGetAllResponse{AllKindBoxReq: allKindBoxReq}, nil //
} // return param.KindBoxReqGetAllResponse{AllKindBoxReq: allKindBoxReq}, nil
//}

View File

@ -2,17 +2,18 @@ package adminkindboxreqservice
import ( import (
"context" "context"
"git.gocasts.ir/ebhomengo/niki/entity"
entity "git.gocasts.ir/ebhomengo/niki/entity"
) )
type Repository interface { type Repository interface {
AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error) // AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error)
UpdateKindBoxReq(ctx context.Context, kindBoxReqID uint, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error) // UpdateKindBoxReq(ctx context.Context, kindBoxReqID uint, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error)
// TODO: can benefactor cancel its request ? // TODO: can benefactor cancel its request ?
DeleteKindBoxReq(ctx context.Context, kindBoxReqID uint) error // DeleteKindBoxReq(ctx context.Context, kindBoxReqID uint) error
GetAllKindBoxReq(ctx context.Context) ([]entity.KindBoxReq, error) // GetAllKindBoxReq(ctx context.Context) ([]entity.KindBoxReq, error)
GetKindBoxReq(ctx context.Context, kindBoxReqID uint) (entity.KindBoxReq, error) // GetKindBoxReq(ctx context.Context, kindBoxReqID uint) (entity.KindBoxReq, error)
AcceptKindBoxReq(ctx context.Context, kindBoxReqID uint, countAccepted uint, description string) error
FindByID(ctx context.Context, id uint) (entity.KindBoxReq, error)
} }
// TODO: check validation. // TODO: check validation.

View File

@ -1,10 +1,11 @@
package adminauthservice package adminauthservice
import ( import (
"git.gocasts.ir/ebhomengo/niki/entity"
"github.com/golang-jwt/jwt/v4"
"strings" "strings"
"time" "time"
"git.gocasts.ir/ebhomengo/niki/entity"
"github.com/golang-jwt/jwt/v4"
) )
type Config struct { type Config struct {

View File

@ -2,18 +2,19 @@ package adminvalidator
import ( import (
"errors" "errors"
"regexp"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin" adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
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"
"regexp"
) )
func (v Validator) ValidateLoginWithPhoneNumberRequest(req adminserviceparam.LoginWithPhoneNumberRequest) (map[string]string, error) { func (v Validator) ValidateLoginWithPhoneNumberRequest(req adminserviceparam.LoginWithPhoneNumberRequest) (map[string]string, error) {
const op = "adminvalidator.ValidateRegisterRequest" const op = "adminvalidator.ValidateRegisterRequest"
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
//TODO - add regex // TODO - add regex
validation.Field(&req.Password, validation.Required, validation.NotNil, validation.Field(&req.Password, validation.Required, validation.NotNil,
validation.Length(8, 0)), validation.Length(8, 0)),

View File

@ -2,12 +2,13 @@ package adminvalidator
import ( import (
"errors" "errors"
"regexp"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin" adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
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"
"regexp"
) )
func (v Validator) ValidateRegisterRequest(req adminserviceparam.RegisterRequest) (map[string]string, error) { func (v Validator) ValidateRegisterRequest(req adminserviceparam.RegisterRequest) (map[string]string, error) {
@ -19,7 +20,7 @@ func (v Validator) ValidateRegisterRequest(req adminserviceparam.RegisterRequest
validation.Field(&req.LastName, validation.Field(&req.LastName,
validation.Length(3, 40)), validation.Length(3, 40)),
//TODO - add regex // TODO - add regex
validation.Field(&req.Password, validation.Required, validation.NotNil, validation.Field(&req.Password, validation.Required, validation.NotNil,
validation.Length(8, 0)), validation.Length(8, 0)),
validation.Field(&req.Gender, validation.By(v.IsGenderValid)), validation.Field(&req.Gender, validation.By(v.IsGenderValid)),

View File

@ -3,6 +3,7 @@ package adminvalidator
import ( import (
"context" "context"
"fmt" "fmt"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
) )

View File

@ -1,46 +1,46 @@
package adminkindboxreqvalidator package adminkindboxreqvalidator
import ( // import (
"errors" // "errors"
//
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" // param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
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) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[string]string, error) { // func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[string]string, error) {
const op = "adminkindboxreqvalidator.ValidateAddRequest" // const op = "adminkindboxreqvalidator.ValidateAddRequest"
//
if err := validation.ValidateStruct(&req, // if err := validation.ValidateStruct(&req,
//
validation.Field(&req.CountRequested, validation.Required, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)), // validation.Field(&req.CountRequested, validation.Required, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)),
//
validation.Field(&req.BenefactorID, // validation.Field(&req.BenefactorID,
validation.Required, // validation.Required,
validation.By(v.doesBenefactorExist)), // validation.By(v.doesBenefactorExist)),
//
validation.Field(&req.TypeID, // validation.Field(&req.TypeID,
validation.Required, // validation.Required,
validation.By(v.doesTypeExist)), // validation.By(v.doesTypeExist)),
); 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,45 +1,45 @@
package adminkindboxreqvalidator package adminkindboxreqvalidator
import ( // import (
"errors" // "errors"
//
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" // param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
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.KindBoxReqDeleteRequest) (map[string]string, error) { // func (v Validator) ValidateDeleteRequest(req param.KindBoxReqDeleteRequest) (map[string]string, error) {
const op = "adminkindboxreqvalidator.ValidateDeleteRequest" // const op = "adminkindboxreqvalidator.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.KindBoxReqID, // validation.Field(&req.KindBoxReqID,
validation.Required, // validation.Required,
validation.By(v.hasPendingStatus), // validation.By(v.hasPendingStatus),
validation.By(v.doesKindBoxRequestExist), // validation.By(v.doesKindBoxRequestExist),
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 adminkindboxreqvalidator package adminkindboxreqvalidator
import ( // import (
"errors" // "errors"
//
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" // param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
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.KindBoxReqGetRequest) (map[string]string, error) { // func (v Validator) ValidateGetRequest(req param.KindBoxReqGetRequest) (map[string]string, error) {
const op = "adminkindboxreqvalidator.ValidateGetRequest" // const op = "adminkindboxreqvalidator.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.KindBoxReqID, // validation.Field(&req.KindBoxReqID,
validation.Required, // validation.Required,
validation.By(v.doesKindBoxRequestExist), // validation.By(v.doesKindBoxRequestExist),
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,50 +1,50 @@
package adminkindboxreqvalidator package adminkindboxreqvalidator
import ( // import (
"errors" // "errors"
//
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" // param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
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) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map[string]string, error) { // func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map[string]string, error) {
const op = "adminkindboxreqvalidator.ValidateUpdateRequest" // const op = "adminkindboxreqvalidator.ValidateUpdateRequest"
//
if err := validation.ValidateStruct(&req, // if err := validation.ValidateStruct(&req,
validation.Field(&req.CountRequested, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)), // validation.Field(&req.CountRequested, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)),
//
validation.Field(&req.BenefactorID, // validation.Field(&req.BenefactorID,
validation.Required, // validation.Required,
validation.By(v.doesBenefactorExist)), // validation.By(v.doesBenefactorExist)),
//
validation.Field(&req.KindBoxReqID, // validation.Field(&req.KindBoxReqID,
validation.Required, // validation.Required,
validation.By(v.doesKindBoxRequestExist), // validation.By(v.doesKindBoxRequestExist),
validation.By(v.hasPendingStatus), // validation.By(v.hasPendingStatus),
validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), // validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))),
//
validation.Field(&req.TypeID, // validation.Field(&req.TypeID,
validation.By(v.doesTypeExist)), // validation.By(v.doesTypeExist)),
); 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,23 +1,16 @@
package adminkindboxreqvalidator package adminkindboxreqvalidator
import (
"fmt"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
const ( const (
MinKindBoxReq = 1 MinKindBoxReq = 1
MaxKindBoxReq = 100 MaxKindBoxReq = 100
) )
type Repository interface { type Repository interface {
BenefactorExist(id int) (bool, error) // BenefactorExist(id int) (bool, error)
KindBoxRequestExist(id int) (bool, error) // KindBoxRequestExist(id int) (bool, error)
TypeExist(id int) (bool, error) // TypeExist(id int) (bool, error)
KindBoxBelongToBenefactor(benefactorID uint, kindboxID uint) (bool, error) // KindBoxBelongToBenefactor(benefactorID uint, kindboxID uint) (bool, error)
PendingStatus(id uint) (bool, error) // PendingStatus(id uint) (bool, error)
} }
type Validator struct { type Validator struct {
@ -28,69 +21,70 @@ func New(repo Repository) Validator {
return Validator{repo: repo} return Validator{repo: repo}
} }
func (v Validator) doesBenefactorExist(value interface{}) error { //
benefactorID, ok := value.(int) // func (v Validator) doesBenefactorExist(value interface{}) error {
if !ok { // benefactorID, ok := value.(int)
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) // if !ok {
} // return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
_, err := v.repo.BenefactorExist(benefactorID) // }
if err != nil { // _, err := v.repo.BenefactorExist(benefactorID)
return fmt.Errorf(errmsg.ErrorMsgNotFound) // if err != nil {
} // return fmt.Errorf(errmsg.ErrorMsgNotFound)
// }
return nil //
} // return nil
//}
func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc { //
return func(value interface{}) error { // func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc {
kbID, ok := value.(uint) // return func(value interface{}) error {
if !ok { // kbID, ok := value.(uint)
return fmt.Errorf(errmsg.ErrorMsgNotFound) // if !ok {
} // return fmt.Errorf(errmsg.ErrorMsgNotFound)
_, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbID) // }
if err != nil { // _, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbID)
return fmt.Errorf(errmsg.ErrorMsgNotFound) // if err != nil {
} // return fmt.Errorf(errmsg.ErrorMsgNotFound)
// }
return nil //
} // return nil
} // }
//}
func (v Validator) doesKindBoxRequestExist(value interface{}) error { //
kindboxreqID, ok := value.(int) // func (v Validator) doesKindBoxRequestExist(value interface{}) error {
if !ok { // kindboxreqID, ok := value.(int)
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) // if !ok {
} // return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
_, err := v.repo.KindBoxRequestExist(kindboxreqID) // }
if err != nil { // _, err := v.repo.KindBoxRequestExist(kindboxreqID)
return fmt.Errorf(errmsg.ErrorMsgNotFound) // if err != nil {
} // return fmt.Errorf(errmsg.ErrorMsgNotFound)
// }
return nil //
} // return nil
//}
func (v Validator) doesTypeExist(value interface{}) error { //
typeID, ok := value.(int) // func (v Validator) doesTypeExist(value interface{}) error {
if !ok { // typeID, ok := value.(int)
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) // if !ok {
} // return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
_, err := v.repo.TypeExist(typeID) // }
if err != nil { // _, err := v.repo.TypeExist(typeID)
return fmt.Errorf(errmsg.ErrorMsgNotFound) // if err != nil {
} // return fmt.Errorf(errmsg.ErrorMsgNotFound)
// }
return nil //
} // return nil
//}
func (v Validator) hasPendingStatus(value interface{}) error { //
kindboxID, ok := value.(uint) //func (v Validator) hasPendingStatus(value interface{}) error {
if !ok { // kindboxID, ok := value.(uint)
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) // if !ok {
} // return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
_, err := v.repo.PendingStatus(kindboxID) // }
if err != nil { // _, err := v.repo.PendingStatus(kindboxID)
return fmt.Errorf(errmsg.ErrorMsgNotFound) // if err != nil {
} // return fmt.Errorf(errmsg.ErrorMsgNotFound)
// }
return nil //
} // return nil
//}