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
import (
"net/http"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
"github.com/labstack/echo/v4"
"net/http"
)
func (h Handler) LoginByPhoneNumber(c echo.Context) error {

View File

@ -1,10 +1,11 @@
package adminhandler
import (
"net/http"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
"github.com/labstack/echo/v4"
"net/http"
)
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
import (
"net/http"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
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.KindBoxReqGetRequest
if bErr := c.Bind(&req); bErr != nil {
return echo.NewHTTPError(http.StatusBadRequest)
}
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.Get(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) Get(c echo.Context) error {
// var req param.KindBoxReqGetRequest
// if bErr := c.Bind(&req); bErr != nil {
// return echo.NewHTTPError(http.StatusBadRequest)
// }
// 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.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
import (
"net/http"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
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.KindBoxReqGetAllRequest
if bErr := c.Bind(&req); bErr != nil {
return echo.NewHTTPError(http.StatusBadRequest)
}
resp, sErr := h.adminKindBoxReqSvc.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_req"
// 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.KindBoxReqGetAllRequest
// if bErr := c.Bind(&req); bErr != nil {
// return echo.NewHTTPError(http.StatusBadRequest)
// }
//
// resp, sErr := h.adminKindBoxReqSvc.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

@ -9,8 +9,9 @@ func (h Handler) SetRoutes(e *echo.Echo) {
//nolint:gocritic
//r.POST("/", h.Add).Name = "admin-addkindboxreq"
r.GET("/:id", h.Get).Name = "admin-getkindboxreqbyid"
r.GET("/", h.GetAll).Name = "admin-getallkindboxreq"
//r.GET("/:id", h.Get).Name = "admin-getkindboxreqbyid"
//r.GET("/", h.GetAll).Name = "admin-getallkindboxreq"
//nolint:gocritic
//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 (
"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"
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"
benefactorhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/benefactor"
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"
benefactoraddressservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/address"
benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor"
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"
benefactorkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/kind_box_req"
echo "github.com/labstack/echo/v4"
@ -28,6 +31,7 @@ type Server struct {
benefactorKindBoxReqHandler benefactorkindboxreqhandler.Handler
benefactorBaseHandler benefactorbasehandler.Handler
adminHandler adminhandler.Handler
adminKindBoxReqHandler adminkindboxreqhandler.Handler
}
func New(
@ -41,6 +45,8 @@ func New(
adminSvc adminservice.Service,
adminVld adminvalidator.Validator,
adminAuthSvc adminauthservice.Service,
adminKinBoxReqSvc adminkindboxreqservice.Service,
adminKinBoxReqVld adminkindboxreqvalidator.Validator,
) Server {
return Server{
Router: echo.New(),
@ -49,6 +55,7 @@ func New(
benefactorKindBoxReqHandler: benefactorkindboxreqhandler.New(cfg.Auth, authSvc, benefactorKindBoxReqSvc, benefactorKindBoxReqVld),
benefactorBaseHandler: benefactorbasehandler.New(benefactorAddressSvc),
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.benefactorBaseHandler.SetRoutes(s.Router)
s.adminHandler.SetRoutes(s.Router)
s.adminKindBoxReqHandler.SetRoutes(s.Router)
// Start server
address := fmt.Sprintf(":%d", s.config.HTTPServer.Port)

View File

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

View File

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

View File

@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"errors"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)

View File

@ -4,11 +4,12 @@ import (
"context"
"database/sql"
"errors"
"time"
"git.gocasts.ir/ebhomengo/niki/entity"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
"git.gocasts.ir/ebhomengo/niki/repository/mysql"
"time"
)
func (d DB) GetAdminByPhoneNumber(ctx context.Context, phoneNumber string) (entity.Admin, error) {

View File

@ -2,7 +2,6 @@ package mysqlkindboxreq
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"
@ -25,3 +24,31 @@ func (d DB) AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (en
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 (
"context"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
@ -36,5 +37,4 @@ func (s Service) LoginWithPhoneNumber(ctx context.Context, req adminserviceparam
RefreshToken: refreshToken,
},
}, nil
}

View File

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

View File

@ -3,6 +3,7 @@ package adminservice
import (
"context"
"fmt"
"git.gocasts.ir/ebhomengo/niki/config"
"git.gocasts.ir/ebhomengo/niki/entity"
"golang.org/x/crypto/bcrypt"
@ -39,6 +40,7 @@ func GenerateHash(password *string) error {
return nil
}
func CompareHash(hashedPassword, password string) error {
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
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) Delete(ctx context.Context, req param.KindBoxReqDeleteRequest) (param.KindBoxReqDeleteResponse, error) {
// TODO: Does business domain need to delete an kindboxreq ?
const op = "adminkindboxreqservice.Delete"
dErr := s.repo.DeleteKindBoxReq(ctx, req.KindBoxReqID)
if dErr != nil {
return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithErr(dErr).WithKind(richerror.KindUnexpected)
}
return param.KindBoxReqDeleteResponse{}, nil
}
//
// 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) Delete(ctx context.Context, req param.KindBoxReqDeleteRequest) (param.KindBoxReqDeleteResponse, error) {
// // TODO: Does business domain need to delete an kindboxreq ?
// const op = "adminkindboxreqservice.Delete"
//
// dErr := s.repo.DeleteKindBoxReq(ctx, req.KindBoxReqID)
// if dErr != nil {
// return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithErr(dErr).WithKind(richerror.KindUnexpected)
// }
//
// return param.KindBoxReqDeleteResponse{}, nil
//}

View File

@ -1,20 +1,21 @@
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) 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)
if err != nil {
return param.KindBoxReqGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
}
return param.KindBoxReqGetResponse{KindBoxReq: kindBoxReq}, nil
}
//
// 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) 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)
// if err != nil {
// return param.KindBoxReqGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
// }
//
// return param.KindBoxReqGetResponse{KindBoxReq: kindBoxReq}, nil
//}

View File

@ -1,20 +1,21 @@
package adminkindboxreqservice
import (
"context"
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) {
const op = "adminkindboxreqservice.GetAll"
allKindBoxReq, err := s.repo.GetAllKindBoxReq(ctx)
if err != nil {
return param.KindBoxReqGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
}
return param.KindBoxReqGetAllResponse{AllKindBoxReq: allKindBoxReq}, nil
}
//
// import (
// "context"
//
// 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) {
// const op = "adminkindboxreqservice.GetAll"
//
// allKindBoxReq, err := s.repo.GetAllKindBoxReq(ctx)
// if err != nil {
// return param.KindBoxReqGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
// }
//
// return param.KindBoxReqGetAllResponse{AllKindBoxReq: allKindBoxReq}, nil
//}

View File

@ -2,17 +2,18 @@ package adminkindboxreqservice
import (
"context"
entity "git.gocasts.ir/ebhomengo/niki/entity"
"git.gocasts.ir/ebhomengo/niki/entity"
)
type Repository interface {
AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error)
UpdateKindBoxReq(ctx context.Context, kindBoxReqID uint, 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)
// TODO: can benefactor cancel its request ?
DeleteKindBoxReq(ctx context.Context, kindBoxReqID uint) error
GetAllKindBoxReq(ctx context.Context) ([]entity.KindBoxReq, error)
GetKindBoxReq(ctx context.Context, kindBoxReqID uint) (entity.KindBoxReq, error)
// DeleteKindBoxReq(ctx context.Context, kindBoxReqID uint) error
// GetAllKindBoxReq(ctx context.Context) ([]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.

View File

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

View File

@ -2,11 +2,12 @@ package adminvalidator
import (
"errors"
"regexp"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
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"
"regexp"
)
func (v Validator) ValidateLoginWithPhoneNumberRequest(req adminserviceparam.LoginWithPhoneNumberRequest) (map[string]string, error) {

View File

@ -2,12 +2,13 @@ package adminvalidator
import (
"errors"
"regexp"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
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"
"regexp"
)
func (v Validator) ValidateRegisterRequest(req adminserviceparam.RegisterRequest) (map[string]string, error) {

View File

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

View File

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