forked from ebhomengo/niki
feat(niki): get all benefactor by admin
This commit is contained in:
parent
cf27483182
commit
046b292f9f
|
|
@ -0,0 +1,29 @@
|
||||||
|
package adminbenefactorhandler
|
||||||
|
|
||||||
|
import (
|
||||||
|
adminbenefactorparam "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor"
|
||||||
|
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetAllBenefactor godoc
|
||||||
|
// @Summary Get all benefactor by admin
|
||||||
|
// @Tags Admins Benefactors
|
||||||
|
// @Accept json
|
||||||
|
// @Produce json
|
||||||
|
// @Success 200 {object} adminbenefactorparam.GetAllBenefactorResponse
|
||||||
|
// @Failure 400 {string} "Bad request"
|
||||||
|
// @Security AuthBearerAdmin
|
||||||
|
// @Router /admins/benefactors [get].
|
||||||
|
func (h Handler) GetAllBenefactor(c echo.Context) error {
|
||||||
|
var resp adminbenefactorparam.GetAllBenefactorResponse
|
||||||
|
resp, sErr := h.benefactorSvc.GetAllBenefactor(c.Request().Context())
|
||||||
|
if sErr != nil {
|
||||||
|
msg, code := httpmsg.Error(sErr)
|
||||||
|
|
||||||
|
return echo.NewHTTPError(code, msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(http.StatusOK, resp)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,24 @@
|
||||||
|
package adminbenefactorhandler
|
||||||
|
|
||||||
|
import (
|
||||||
|
authorizeservice "git.gocasts.ir/ebhomengo/niki/service/admin/authorization"
|
||||||
|
benefactorservice "git.gocasts.ir/ebhomengo/niki/service/admin/benefactor"
|
||||||
|
authservice "git.gocasts.ir/ebhomengo/niki/service/auth"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Handler struct {
|
||||||
|
authSvc authservice.Service
|
||||||
|
benefactorSvc benefactorservice.Service
|
||||||
|
authorizeSvc authorizeservice.Service
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(authSvc authservice.Service,
|
||||||
|
benefactorSvc benefactorservice.Service,
|
||||||
|
authorizeSvc authorizeservice.Service,
|
||||||
|
) Handler {
|
||||||
|
return Handler{
|
||||||
|
authSvc: authSvc,
|
||||||
|
benefactorSvc: benefactorSvc,
|
||||||
|
authorizeSvc: authorizeSvc,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
package adminbenefactorhandler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.gocasts.ir/ebhomengo/niki/delivery/http_server/middleware"
|
||||||
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (h Handler) SetRoutes(e *echo.Echo) {
|
||||||
|
r := e.Group("/admins")
|
||||||
|
|
||||||
|
r.GET("/benefactors", h.GetAllBenefactor, middleware.Auth(h.authSvc), middleware.AdminAuthorization(h.authorizeSvc, entity.AdminBenefactorGetAllPermission))
|
||||||
|
}
|
||||||
|
|
@ -11,6 +11,7 @@ const (
|
||||||
AdminKindBoxReqDeliverPermission = AdminPermission("kindboxreq-deliver")
|
AdminKindBoxReqDeliverPermission = AdminPermission("kindboxreq-deliver")
|
||||||
AdminKindBoxReqAssignSenderAgentPermission = AdminPermission("kindboxreq-assign_sender_agent")
|
AdminKindBoxReqAssignSenderAgentPermission = AdminPermission("kindboxreq-assign_sender_agent")
|
||||||
AdminAdminGetAllAgentPermission = AdminPermission("admin-getall_agent")
|
AdminAdminGetAllAgentPermission = AdminPermission("admin-getall_agent")
|
||||||
|
AdminBenefactorGetAllPermission = AdminPermission("benefactor-getall")
|
||||||
AdminKindBoxReqGetAwaitingDeliveryPermission = AdminPermission("kindboxreq-get_awaiting_delivery")
|
AdminKindBoxReqGetAwaitingDeliveryPermission = AdminPermission("kindboxreq-get_awaiting_delivery")
|
||||||
AdminKindBoxGetPermission = AdminPermission("kindbox-get")
|
AdminKindBoxGetPermission = AdminPermission("kindbox-get")
|
||||||
AdminKindBoxAssignReceiverAgentPermission = AdminPermission("kindbox-assign_receiver_agent")
|
AdminKindBoxAssignReceiverAgentPermission = AdminPermission("kindbox-assign_receiver_agent")
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
package adminbenefactoreparam
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Data struct {
|
||||||
|
ID uint `json:"id"`
|
||||||
|
FirstName string `json:"first_name"`
|
||||||
|
LastName string `json:"last_name"`
|
||||||
|
PhoneNumber string `json:"phone_number"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
Email string `json:"email"`
|
||||||
|
Gender entity.Gender `json:"gender"`
|
||||||
|
BirthDate time.Time `json:"birth_date"`
|
||||||
|
Roll entity.UserRole `json:"roll"`
|
||||||
|
Status entity.BenefactorStatus `json:"status"`
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
package adminbenefactoreparam
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.gocasts.ir/ebhomengo/niki/param"
|
||||||
|
)
|
||||||
|
|
||||||
|
type GetAllBenefactorRequest struct {
|
||||||
|
Pagination param.PaginationRequest
|
||||||
|
Sort param.SortRequest
|
||||||
|
Filter param.FilterRequest
|
||||||
|
}
|
||||||
|
type GetAllBenefactorResponse struct {
|
||||||
|
Data []Data `json:"data"`
|
||||||
|
Pagination param.PaginationResponse `json:"pagination"`
|
||||||
|
FieldErrors map[string]string `json:"field_errors,omitempty"`
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,47 @@
|
||||||
|
package mysqlbenefactor
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
||||||
|
params "git.gocasts.ir/ebhomengo/niki/param"
|
||||||
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||||
|
builder "git.gocasts.ir/ebhomengo/niki/pkg/query_builder/mysql"
|
||||||
|
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (d *DB) GetAllBenefactor(ctx context.Context, filter params.FilterRequest, pagination params.PaginationRequest, sort params.SortRequest) ([]entity.Benefactor, uint, error) {
|
||||||
|
const op = "mysqlbenefactor.GetAllBenefactor"
|
||||||
|
|
||||||
|
baseQuery := `SELECT * FROM benefactors`
|
||||||
|
query, args := builder.BuildGetAllQuery(baseQuery, filter, pagination, sort)
|
||||||
|
rows, qErr := d.conn.Conn().QueryContext(ctx, query, args...)
|
||||||
|
if qErr != nil {
|
||||||
|
return nil, 0, richerror.New(op).WithErr(qErr).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
benefactors := make([]entity.Benefactor, 0)
|
||||||
|
for rows.Next() {
|
||||||
|
benefactor, sErr := scanBenefactor(rows)
|
||||||
|
if sErr != nil {
|
||||||
|
return nil, 0, richerror.New(op).WithErr(sErr).
|
||||||
|
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
|
||||||
|
}
|
||||||
|
benefactors = append(benefactors, benefactor)
|
||||||
|
}
|
||||||
|
|
||||||
|
if rErr := rows.Err(); rErr != nil {
|
||||||
|
return nil, 0, richerror.New(op).WithErr(rErr).
|
||||||
|
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
||||||
|
}
|
||||||
|
var total uint
|
||||||
|
baseQuery = `SELECT COUNT(*) FROM benefactors`
|
||||||
|
query, args = builder.BuildGetAllQuery(baseQuery, filter, pagination, sort)
|
||||||
|
qErr = d.conn.Conn().QueryRowContext(ctx, query, args...).Scan(&total)
|
||||||
|
if qErr != nil {
|
||||||
|
return nil, 0, richerror.New(op).WithErr(qErr).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
||||||
|
}
|
||||||
|
|
||||||
|
return benefactors, total, nil
|
||||||
|
}
|
||||||
|
|
@ -25,6 +25,7 @@ const (
|
||||||
StatementKeyBenefactorGetByID
|
StatementKeyBenefactorGetByID
|
||||||
StatementKeyBenefactorGetByPhoneNumber
|
StatementKeyBenefactorGetByPhoneNumber
|
||||||
StatementKeyBenefactorCreate
|
StatementKeyBenefactorCreate
|
||||||
|
StatementKeyBenefactorGetAll
|
||||||
StatementKeyKindBoxAdd
|
StatementKeyKindBoxAdd
|
||||||
StatementKeyKindBoxAssignReceiverAgent
|
StatementKeyKindBoxAssignReceiverAgent
|
||||||
StatementKeyKindBoxEnumerate
|
StatementKeyKindBoxEnumerate
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
package adminbenefactorservice
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
paginationparam "git.gocasts.ir/ebhomengo/niki/param"
|
||||||
|
param "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor"
|
||||||
|
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s Service) GetAllBenefactor(ctx context.Context, req param.GetAllBenefactorRequest) (param.GetAllBenefactorResponse, error) {
|
||||||
|
const op = "adminbenefactorservice.GetAllBenefactor"
|
||||||
|
|
||||||
|
benefactorInfo := make([]param.Data, 0)
|
||||||
|
|
||||||
|
benefactors, total, err := s.benefactorSvc.GetAllBenefactors(ctx, req.Filter, req.Pagination, req.Sort)
|
||||||
|
if err != nil {
|
||||||
|
return param.GetAllBenefactorResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, benefactor := range benefactors {
|
||||||
|
benefactorInfo = append(benefactorInfo, param.Data{
|
||||||
|
ID: benefactor.ID,
|
||||||
|
FirstName: benefactor.FirstName,
|
||||||
|
LastName: benefactor.LastName,
|
||||||
|
PhoneNumber: benefactor.PhoneNumber,
|
||||||
|
Description: benefactor.Description,
|
||||||
|
Email: benefactor.Email,
|
||||||
|
Gender: benefactor.Gender,
|
||||||
|
BirthDate: benefactor.BirthDate,
|
||||||
|
Roll: benefactor.Role,
|
||||||
|
Status: benefactor.Status,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return param.GetAllBenefactorResponse{Data: benefactorInfo, Pagination: paginationparam.PaginationResponse{
|
||||||
|
PageSize: req.Pagination.PageSize,
|
||||||
|
PageNumber: req.Pagination.PageNumber,
|
||||||
|
Total: total,
|
||||||
|
}}, nil
|
||||||
|
}
|
||||||
|
|
@ -2,11 +2,15 @@ package adminbenefactorservice
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"git.gocasts.ir/ebhomengo/niki/param"
|
||||||
|
|
||||||
"git.gocasts.ir/ebhomengo/niki/entity"
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
||||||
adminaddressparam "git.gocasts.ir/ebhomengo/niki/param/admin/address"
|
adminaddressparam "git.gocasts.ir/ebhomengo/niki/param/admin/address"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type BenefactorSvc interface {
|
||||||
|
GetAllBenefactors(ctx context.Context, filter param.FilterRequest, pagination param.PaginationRequest, sort param.SortRequest) ([]entity.Benefactor, uint, error)
|
||||||
|
}
|
||||||
type Repository interface {
|
type Repository interface {
|
||||||
IsExistBenefactorByID(ctx context.Context, id uint) (bool, error)
|
IsExistBenefactorByID(ctx context.Context, id uint) (bool, error)
|
||||||
GetByID(ctx context.Context, id uint) (entity.Benefactor, error)
|
GetByID(ctx context.Context, id uint) (entity.Benefactor, error)
|
||||||
|
|
@ -16,10 +20,11 @@ type AddressSvc interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
repo Repository
|
repo Repository
|
||||||
AddressSvc AddressSvc
|
addressSvc AddressSvc
|
||||||
|
benefactorSvc BenefactorSvc
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(repo Repository, addressSvc AddressSvc) Service {
|
func New(repo Repository, addressSvc AddressSvc, benefactorSvc BenefactorSvc) Service {
|
||||||
return Service{repo: repo, AddressSvc: addressSvc}
|
return Service{repo: repo, addressSvc: addressSvc, benefactorSvc: benefactorSvc}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
package benefactor
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
package benefactor
|
||||||
Loading…
Reference in New Issue