feat(niki): add api to fetch refer time list for benefactor and admin

This commit is contained in:
Hamed Xamani 2024-10-08 00:20:26 +03:30
parent 5f7ce41418
commit 78db09faf4
21 changed files with 563 additions and 3 deletions

View File

@ -0,0 +1,31 @@
package adminrefertimehandler
import (
"net/http"
refertimeparam "git.gocasts.ir/ebhomengo/niki/param/admin/refer_time"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
echo "github.com/labstack/echo/v4"
)
// GetAll godoc
// @Summary Get all refer times
// @Tags Admins ReferTimes
// @Accept json
// @Produce json
// @Success 200 {object} adminrefertimeparam.GetAllReferTimeResponse
// @Failure 400 {string} "Bad request"
// @Security AuthBearerAdmin
// @Router /admins/refer-times [get].
func (h Handler) GetAll(c echo.Context) error {
var req refertimeparam.GetAllReferTimeRequest
listCities, err := h.adminReferTimeSvc.GetAll(c.Request().Context(), req)
if err != nil {
msg, code := httpmsg.Error(err)
return echo.NewHTTPError(code, msg)
}
return c.JSON(http.StatusOK, listCities)
}

View File

@ -0,0 +1,24 @@
package adminrefertimehandler
import (
adminauthorizationservice "git.gocasts.ir/ebhomengo/niki/service/admin/authorization"
adminrefertimeservice "git.gocasts.ir/ebhomengo/niki/service/admin/refer_time"
authservice "git.gocasts.ir/ebhomengo/niki/service/auth"
)
type Handler struct {
authSvc authservice.Service
adminReferTimeSvc adminrefertimeservice.Service
adminAuthorizeSvc adminauthorizationservice.Service
}
func New(authSvc authservice.Service,
adminReferTimeSvc adminrefertimeservice.Service,
adminAuthorizeSvc adminauthorizationservice.Service,
) Handler {
return Handler{
authSvc: authSvc,
adminReferTimeSvc: adminReferTimeSvc,
adminAuthorizeSvc: adminAuthorizeSvc,
}
}

View File

@ -0,0 +1,19 @@
package adminrefertimehandler
import (
"git.gocasts.ir/ebhomengo/niki/delivery/http_server/middleware"
"git.gocasts.ir/ebhomengo/niki/entity"
echo "github.com/labstack/echo/v4"
)
func (h Handler) SetRoutes(e *echo.Echo) {
r := e.Group("/admins/refer-times")
r.Use(middleware.Auth(h.authSvc))
r.Use(
middleware.Auth(h.authSvc),
middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxReqAddPermission),
)
r.GET("", h.GetAll)
}

View File

@ -0,0 +1,31 @@
package benefactorrefertimehandler
import (
"net/http"
refertimeparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/refer_time"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
"github.com/labstack/echo/v4"
)
// GetAll godoc
// @Summary Get all refer times
// @Tags Benefactors ReferTimes
// @Accept json
// @Produce json
// @Success 200 {object} benefactorrefertimeparam.GetAllReferTimeResponse
// @Failure 400 {string} "Bad request"
// @Security AuthBearerBenefactor
// @Router /benefactors/refer-times [get].
func (h Handler) GetAll(c echo.Context) error {
var req refertimeparam.GetAllReferTimeRequest
listReferTimes, err := h.benefactorReferTimeSvc.GetAll(c.Request().Context(), req)
if err != nil {
msg, code := httpmsg.Error(err)
return echo.NewHTTPError(code, msg)
}
return c.JSON(http.StatusOK, listReferTimes)
}

View File

@ -0,0 +1,20 @@
package benefactorrefertimehandler
import (
authservice "git.gocasts.ir/ebhomengo/niki/service/auth"
benefactorrefertimeservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/refer_time"
)
type Handler struct {
authSvc authservice.Service
benefactorReferTimeSvc benefactorrefertimeservice.Service
}
func New(authSvc authservice.Service,
benefactorReferTimeSvc benefactorrefertimeservice.Service,
) Handler {
return Handler{
authSvc: authSvc,
benefactorReferTimeSvc: benefactorReferTimeSvc,
}
}

View File

@ -0,0 +1,18 @@
package benefactorrefertimehandler
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("/benefactors/refer-times")
r.Use(
middleware.Auth(h.authSvc),
middleware.BenefactorAuthorization(entity.UserBenefactorRole),
)
r.GET("", h.GetAll)
}

View File

@ -2,18 +2,20 @@ package httpserver
import (
"fmt"
agentkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/agent/kind_box_req"
"git.gocasts.ir/ebhomengo/niki/config"
adminhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/admin"
adminagenthandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/agent"
adminKindBoxHandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/kind_box"
adminkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/kind_box_req"
adminrefertimehandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/refer_time"
agentkindboxhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/agent/kind_box"
agentkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/agent/kind_box_req"
benefactoraddresshandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/address"
benefactorhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/benefactor"
benefactorkindboxhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/kind_box"
benefactorkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/kind_box_req"
benefactorrefertimehandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/refer_time"
"git.gocasts.ir/ebhomengo/niki/docs"
"git.gocasts.ir/ebhomengo/niki/service"
echo "github.com/labstack/echo/v4"
@ -28,12 +30,14 @@ type Server struct {
adminKindBoxReqHandler adminkindboxreqhandler.Handler
adminKindBoxHandler adminKindBoxHandler.Handler
adminAgentHandler adminagenthandler.Handler
adminReferTimeHandler adminrefertimehandler.Handler
agentKindBoxHandler agentkindboxhandler.Handler
agentKindBoxReqHandler agentkindboxreqhandler.Handler
benefactorHandler benefactorhandler.Handler
benefactorKindBoxReqHandler benefactorkindboxreqhandler.Handler
benefactorAddressHandler benefactoraddresshandler.Handler
benefactorKindBoxHandler benefactorkindboxhandler.Handler
benefactorReferTimeHandler benefactorrefertimehandler.Handler
}
func New(
@ -47,12 +51,14 @@ func New(
adminKindBoxReqHandler: adminkindboxreqhandler.New(svc.AdminAuthSvc, svc.AdminKindBoxReqSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc),
adminKindBoxHandler: adminKindBoxHandler.New(svc.AdminAuthSvc, svc.AdminKindBoxSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc),
adminAgentHandler: adminagenthandler.New(svc.AdminAuthSvc, svc.AdminAgentSvc, svc.AdminAuthorizeSvc),
adminReferTimeHandler: adminrefertimehandler.New(svc.AdminAuthSvc, svc.AdminReferTimeSvc, svc.AdminAuthorizeSvc),
agentKindBoxHandler: agentkindboxhandler.New(svc.AdminAuthSvc, svc.AgentKindBoxSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc),
agentKindBoxReqHandler: agentkindboxreqhandler.New(svc.AdminAuthSvc, svc.AgentKindBoxReqSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc),
benefactorHandler: benefactorhandler.New(svc.BenefactorAuthSvc, svc.BenefactorSvc),
benefactorKindBoxReqHandler: benefactorkindboxreqhandler.New(svc.BenefactorAuthSvc, svc.BenefactorKindBoxReqSvc, svc.NotificationSvc),
benefactorAddressHandler: benefactoraddresshandler.New(svc.BenefactorAuthSvc, svc.BenefactorAddressSvc),
benefactorKindBoxHandler: benefactorkindboxhandler.New(svc.BenefactorAuthSvc, svc.BenefactorKindBoxSvc, svc.NotificationSvc),
benefactorReferTimeHandler: benefactorrefertimehandler.New(svc.BenefactorAuthSvc, svc.BenefactorReferTimeSvc),
}
}
@ -81,10 +87,12 @@ func (s Server) RegisterRoutes() {
s.benefactorKindBoxReqHandler.SetRoutes(s.Router)
s.benefactorAddressHandler.SetRoutes(s.Router)
s.benefactorKindBoxHandler.SetRoutes(s.Router)
s.benefactorReferTimeHandler.SetRoutes(s.Router)
s.adminHandler.SetRoutes(s.Router)
s.adminAgentHandler.SetRoutes(s.Router)
s.adminKindBoxReqHandler.SetRoutes(s.Router)
s.adminKindBoxHandler.SetRoutes(s.Router)
s.adminReferTimeHandler.SetRoutes(s.Router)
s.agentKindBoxHandler.SetRoutes(s.Router)
s.agentKindBoxReqHandler.SetRoutes(s.Router)
}

View File

@ -1013,6 +1013,39 @@ const docTemplate = `{
}
}
},
"/admins/refer-times": {
"get": {
"security": [
{
"AuthBearerAdmin": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Admins ReferTimes"
],
"summary": "Get all refer times",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/adminrefertimeparam.GetAllReferTimeResponse"
}
},
"400": {
"description": "Bad request",
"schema": {
"type": "string"
}
}
}
}
},
"/admins/refresh-access": {
"post": {
"consumes": [
@ -2538,6 +2571,39 @@ const docTemplate = `{
}
}
},
"/benefactors/refer-times": {
"get": {
"security": [
{
"AuthBearerBenefactor": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Benefactors ReferTimes"
],
"summary": "Get all refer times",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/benefactorrefertimeparam.GetAllReferTimeResponse"
}
},
"400": {
"description": "Bad request",
"schema": {
"type": "string"
}
}
}
}
},
"/benefactors/refresh-access": {
"post": {
"consumes": [
@ -3170,6 +3236,17 @@ const docTemplate = `{
}
}
},
"adminrefertimeparam.GetAllReferTimeResponse": {
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/entity.ReferTime"
}
}
}
},
"adminserviceparam.Data": {
"type": "object",
"properties": {
@ -3926,6 +4003,17 @@ const docTemplate = `{
}
}
},
"benefactorrefertimeparam.GetAllReferTimeResponse": {
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/entity.ReferTime"
}
}
}
},
"entity.AdminRole": {
"type": "integer",
"enum": [
@ -4033,6 +4121,31 @@ const docTemplate = `{
}
}
},
"entity.ReferTime": {
"type": "object",
"properties": {
"duration": {
"type": "string"
},
"id": {
"type": "integer"
},
"status": {
"$ref": "#/definitions/entity.ReferTimeStatus"
}
}
},
"entity.ReferTimeStatus": {
"type": "string",
"enum": [
"active",
"inactive"
],
"x-enum-varnames": [
"ReferTimeActiveStatus",
"ReferTimeInactiveStatus"
]
},
"entity.UserRole": {
"type": "string",
"enum": [

View File

@ -1002,6 +1002,39 @@
}
}
},
"/admins/refer-times": {
"get": {
"security": [
{
"AuthBearerAdmin": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Admins ReferTimes"
],
"summary": "Get all refer times",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/adminrefertimeparam.GetAllReferTimeResponse"
}
},
"400": {
"description": "Bad request",
"schema": {
"type": "string"
}
}
}
}
},
"/admins/refresh-access": {
"post": {
"consumes": [
@ -2527,6 +2560,39 @@
}
}
},
"/benefactors/refer-times": {
"get": {
"security": [
{
"AuthBearerBenefactor": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Benefactors ReferTimes"
],
"summary": "Get all refer times",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/benefactorrefertimeparam.GetAllReferTimeResponse"
}
},
"400": {
"description": "Bad request",
"schema": {
"type": "string"
}
}
}
}
},
"/benefactors/refresh-access": {
"post": {
"consumes": [
@ -3159,6 +3225,17 @@
}
}
},
"adminrefertimeparam.GetAllReferTimeResponse": {
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/entity.ReferTime"
}
}
}
},
"adminserviceparam.Data": {
"type": "object",
"properties": {
@ -3915,6 +3992,17 @@
}
}
},
"benefactorrefertimeparam.GetAllReferTimeResponse": {
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/entity.ReferTime"
}
}
}
},
"entity.AdminRole": {
"type": "integer",
"enum": [
@ -4022,6 +4110,31 @@
}
}
},
"entity.ReferTime": {
"type": "object",
"properties": {
"duration": {
"type": "string"
},
"id": {
"type": "integer"
},
"status": {
"$ref": "#/definitions/entity.ReferTimeStatus"
}
}
},
"entity.ReferTimeStatus": {
"type": "string",
"enum": [
"active",
"inactive"
],
"x-enum-varnames": [
"ReferTimeActiveStatus",
"ReferTimeInactiveStatus"
]
},
"entity.UserRole": {
"type": "string",
"enum": [

View File

@ -362,6 +362,13 @@ definitions:
example: 1
type: integer
type: object
adminrefertimeparam.GetAllReferTimeResponse:
properties:
data:
items:
$ref: '#/definitions/entity.ReferTime'
type: array
type: object
adminserviceparam.Data:
properties:
description:
@ -854,6 +861,13 @@ definitions:
- $ref: '#/definitions/entity.KindBoxType'
example: cylindrical
type: object
benefactorrefertimeparam.GetAllReferTimeResponse:
properties:
data:
items:
$ref: '#/definitions/entity.ReferTime'
type: array
type: object
entity.AdminRole:
enum:
- 1
@ -934,6 +948,23 @@ definitions:
name:
type: string
type: object
entity.ReferTime:
properties:
duration:
type: string
id:
type: integer
status:
$ref: '#/definitions/entity.ReferTimeStatus'
type: object
entity.ReferTimeStatus:
enum:
- active
- inactive
type: string
x-enum-varnames:
- ReferTimeActiveStatus
- ReferTimeInactiveStatus
entity.UserRole:
enum:
- benefactor
@ -1621,6 +1652,26 @@ paths:
summary: "Admin login by\tPhoneNumber"
tags:
- Admins
/admins/refer-times:
get:
consumes:
- application/json
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/adminrefertimeparam.GetAllReferTimeResponse'
"400":
description: Bad request
schema:
type: string
security:
- AuthBearerAdmin: []
summary: Get all refer times
tags:
- Admins ReferTimes
/admins/refresh-access:
post:
consumes:
@ -2627,6 +2678,26 @@ paths:
summary: Login or register a benefactor
tags:
- Benefactors
/benefactors/refer-times:
get:
consumes:
- application/json
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/benefactorrefertimeparam.GetAllReferTimeResponse'
"400":
description: Bad request
schema:
type: string
security:
- AuthBearerBenefactor: []
summary: Get all refer times
tags:
- Benefactors ReferTimes
/benefactors/refresh-access:
post:
consumes:

View File

@ -1,4 +1,4 @@
package param
package adminrefertimeparam
import "git.gocasts.ir/ebhomengo/niki/entity"

View File

@ -0,0 +1,9 @@
package adminrefertimeparam
import "git.gocasts.ir/ebhomengo/niki/entity"
type GetAllReferTimeRequest struct{}
type GetAllReferTimeResponse struct {
Data []entity.ReferTime `json:"data"`
}

View File

@ -1,4 +1,4 @@
package param
package benefactorrefertimeparam
import "git.gocasts.ir/ebhomengo/niki/entity"

View File

@ -0,0 +1,9 @@
package benefactorrefertimeparam
import "git.gocasts.ir/ebhomengo/niki/entity"
type GetAllReferTimeRequest struct{}
type GetAllReferTimeResponse struct {
Data []entity.ReferTime `json:"data"`
}

View File

@ -44,4 +44,5 @@ const (
StatementKeyKindBoxReqUpdate
StatementKeyKindBoxUpdate
StatementKeyReferTimeGetByID
StatementKeyReferTimeGetAll
)

View File

@ -0,0 +1,47 @@
package mysqlrefertime
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"
"git.gocasts.ir/ebhomengo/niki/repository/mysql"
)
func (d *DB) GetAll(ctx context.Context) ([]entity.ReferTime, error) {
const op = "mysqlrefertime.GetAll"
query := `SELECT * FROM refer_times where status = 'active';`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyReferTimeGetAll, query)
if err != nil {
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
rows, err := stmt.QueryContext(ctx)
if err != nil {
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
defer rows.Close()
referTimes := make([]entity.ReferTime, 0)
for rows.Next() {
referTime, err := scanReferTime(rows)
if err != nil {
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
referTimes = append(referTimes, referTime)
}
if err = rows.Err(); err != nil {
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
return referTimes, nil
}

View File

@ -0,0 +1,19 @@
package adminrefertimeservice
import (
"context"
param "git.gocasts.ir/ebhomengo/niki/param/admin/refer_time"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) GetAll(ctx context.Context, _ param.GetAllReferTimeRequest) (param.GetAllReferTimeResponse, error) {
const op = "adminrefertimeservice.GetAllReferTime"
referTimes, err := s.repo.GetAll(ctx)
if err != nil {
return param.GetAllReferTimeResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
}
return param.GetAllReferTimeResponse{Data: referTimes}, nil
}

View File

@ -11,6 +11,7 @@ type Service struct {
}
type Repository interface {
Get(ctx context.Context, referTimeID uint) (entity.ReferTime, error)
GetAll(ctx context.Context) ([]entity.ReferTime, error)
}
func New(repo Repository) Service {

View File

@ -0,0 +1,19 @@
package benefactorrefertimeservice
import (
"context"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/refer_time"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) GetAll(ctx context.Context, _ param.GetAllReferTimeRequest) (param.GetAllReferTimeResponse, error) {
const op = "benefactorrefertimeservice.GetAllReferTime"
referTimes, err := s.repo.GetAll(ctx)
if err != nil {
return param.GetAllReferTimeResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
}
return param.GetAllReferTimeResponse{Data: referTimes}, nil
}

View File

@ -11,6 +11,7 @@ type Service struct {
}
type Repository interface {
Get(ctx context.Context, referTimeID uint) (entity.ReferTime, error)
GetAll(ctx context.Context) ([]entity.ReferTime, error)
}
func New(repo Repository) Service {

View File

@ -56,6 +56,8 @@ type Service struct {
BenefactorAddressSvc benefactoraddressservice.Service
BenefactorSvc benefactorservice.Service
NotificationSvc notification.Service
BenefactorReferTimeSvc benefactorrefertimeservice.Service
AdminReferTimeSvc adminrefertimeservice.Service
}
func New(cfg config.Config, db *mysql.DB, rds *redis.Adapter, smsAdapter smscontract.SmsAdapter) *Service {
@ -102,6 +104,8 @@ func New(cfg config.Config, db *mysql.DB, rds *redis.Adapter, smsAdapter smscont
BenefactorKindBoxReqSvc = benefactorkindboxreqservice.New(kindBoxReqRepo, BenefactorKindBoxReqVld)
BenefactorKindBoxVld = benefactorkindboxvalidator.New(kindBoxRepo, BenefactorSvc, BenefactorAddressSvc, BenefactorReferTimeSvc)
BenefactorKindBoxSvc = benefactorkindboxservice.New(kindBoxRepo, BenefactorKindBoxVld)
benefactorReferTimeSvc = benefactorrefertimeservice.New(referTimeRepo)
adminReferTimeSvc = adminrefertimeservice.New(referTimeRepo)
)
NotificationSvc := notification.New(cfg.NotificationSvc, smsAdapter, AdminKindBoxReqSvc, AdminBenefactorSvc, AdminSvc, AdminKindBoxSvc)
@ -120,5 +124,7 @@ func New(cfg config.Config, db *mysql.DB, rds *redis.Adapter, smsAdapter smscont
BenefactorAddressSvc: BenefactorAddressSvc,
BenefactorSvc: BenefactorSvc,
NotificationSvc: NotificationSvc,
BenefactorReferTimeSvc: benefactorReferTimeSvc,
AdminReferTimeSvc: adminReferTimeSvc,
}
}