feat(service): add get all kindboxreq by admin (#26)

Reviewed-on: ebhomengo/niki#26
Reviewed-by: hossein <h.nazari1990@gmail.com>
Co-authored-by: masoodk <keshvari.developer@gmail.com>
Co-committed-by: masoodk <keshvari.developer@gmail.com>
This commit is contained in:
Masood Keshvari 2024-02-03 04:56:27 +00:00 committed by hossein
parent 0aba9e6cbb
commit 3b3815ccec
7 changed files with 94 additions and 0 deletions

View File

@ -1 +1,25 @@
package adminkindboxreqhandler 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)
}

View File

@ -1,6 +1,7 @@
package adminkindboxreqhandler package adminkindboxreqhandler
import ( import (
"git.gocasts.ir/ebhomengo/niki/delivery/http_server/middleware"
echo "github.com/labstack/echo/v4" echo "github.com/labstack/echo/v4"
) )
@ -10,4 +11,5 @@ func (h Handler) SetRoutes(e *echo.Echo) {
// todo - add acl // todo - add acl
r.PATCH("/accept-kind-box-req/:id", h.Accept) r.PATCH("/accept-kind-box-req/:id", h.Accept)
r.PATCH("/reject-kind-box-req/:id", h.Reject) r.PATCH("/reject-kind-box-req/:id", h.Reject)
r.GET("/", h.GetAll, middleware.Auth(h.authSvc, h.authConfig))
} }

View File

@ -2,6 +2,7 @@ package mysqlkindbox
import ( import (
"context" "context"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
"git.gocasts.ir/ebhomengo/niki/logger" "git.gocasts.ir/ebhomengo/niki/logger"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"

View File

@ -0,0 +1,42 @@
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"
)
func (d DB) GetAllKindBoxReq(ctx context.Context) ([]entity.KindBoxReq, error) {
const op = "mysqlkindboxreq.GetAllKindBoxReq"
// TODO - add sort and filter
rows, err := d.conn.Conn().QueryContext(ctx, "select * from kind_box_reqs")
if err != nil {
return nil,
richerror.New(op).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithErr(err).WithKind(richerror.KindUnexpected)
}
defer rows.Close()
// An album slice to hold data from returned rows.
var kindBoxReqs []entity.KindBoxReq
// Loop through rows, using Scan to assign column data to struct fields.
for rows.Next() {
kindBoxReq, sErr := scanKindBoxReq(rows)
if sErr != nil {
return nil, richerror.New(op).WithErr(sErr).
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
}
kindBoxReqs = append(kindBoxReqs, kindBoxReq)
}
if rErr := rows.Err(); rErr != nil {
return nil, richerror.New(op).WithErr(rErr).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
return kindBoxReqs, nil
}

View File

@ -1 +1,20 @@
package adminkindboxreqservice 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
}

View File

@ -12,6 +12,7 @@ type Repository interface {
GetByID(ctx context.Context, id uint) (entity.KindBoxReq, error) GetByID(ctx context.Context, id uint) (entity.KindBoxReq, error)
RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) error RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) error
RollbackKindBoxRequestStatus(ctx context.Context, id uint) error RollbackKindBoxRequestStatus(ctx context.Context, id uint) error
GetAllKindBoxReq(ctx context.Context) ([]entity.KindBoxReq, error)
} }
type KindBoxClient interface { type KindBoxClient interface {

View File

@ -0,0 +1,5 @@
package adminkindboxreqvalidator
func (v Validator) ValidateGetAllRequest() (map[string]string, error) {
return map[string]string{}, nil
}