feat(niki): get all benefactor by admin

This commit is contained in:
AMiR 2024-09-30 12:44:59 +03:30 committed by Mohammadi, Erfan
parent cf27483182
commit 046b292f9f
12 changed files with 201 additions and 4 deletions

View File

@ -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)
}

View File

@ -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,
}
}

View File

@ -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))
}

View File

@ -11,6 +11,7 @@ const (
AdminKindBoxReqDeliverPermission = AdminPermission("kindboxreq-deliver")
AdminKindBoxReqAssignSenderAgentPermission = AdminPermission("kindboxreq-assign_sender_agent")
AdminAdminGetAllAgentPermission = AdminPermission("admin-getall_agent")
AdminBenefactorGetAllPermission = AdminPermission("benefactor-getall")
AdminKindBoxReqGetAwaitingDeliveryPermission = AdminPermission("kindboxreq-get_awaiting_delivery")
AdminKindBoxGetPermission = AdminPermission("kindbox-get")
AdminKindBoxAssignReceiverAgentPermission = AdminPermission("kindbox-assign_receiver_agent")

View File

@ -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"`
}

View File

@ -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"`
}

View File

@ -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
}

View File

@ -25,6 +25,7 @@ const (
StatementKeyBenefactorGetByID
StatementKeyBenefactorGetByPhoneNumber
StatementKeyBenefactorCreate
StatementKeyBenefactorGetAll
StatementKeyKindBoxAdd
StatementKeyKindBoxAssignReceiverAgent
StatementKeyKindBoxEnumerate

View File

@ -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
}

View File

@ -2,11 +2,15 @@ package adminbenefactorservice
import (
"context"
"git.gocasts.ir/ebhomengo/niki/param"
"git.gocasts.ir/ebhomengo/niki/entity"
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 {
IsExistBenefactorByID(ctx context.Context, id uint) (bool, error)
GetByID(ctx context.Context, id uint) (entity.Benefactor, error)
@ -17,9 +21,10 @@ type AddressSvc interface {
type Service struct {
repo Repository
AddressSvc AddressSvc
addressSvc AddressSvc
benefactorSvc BenefactorSvc
}
func New(repo Repository, addressSvc AddressSvc) Service {
return Service{repo: repo, AddressSvc: addressSvc}
func New(repo Repository, addressSvc AddressSvc, benefactorSvc BenefactorSvc) Service {
return Service{repo: repo, addressSvc: addressSvc, benefactorSvc: benefactorSvc}
}

View File

@ -0,0 +1 @@
package benefactor

View File

@ -0,0 +1 @@
package benefactor