feat(niki): add benefactor get all addresses

This commit is contained in:
Erfan Mohammadi 2024-06-01 21:31:00 +03:30
parent b0f5d5e2f4
commit e66a7355e2
9 changed files with 216 additions and 0 deletions

View File

@ -0,0 +1,35 @@
package benefactoraddresshandler
import (
"net/http"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/address"
"git.gocasts.ir/ebhomengo/niki/pkg/claim"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
"github.com/labstack/echo/v4"
)
// GetAddresses godoc
// @Summary Get all benefactor addresses
// @Tags Address
// @Accept json
// @Produce json
// @Success 200 {object} param.GetAllAddressesResponse
// @Failure 400 {string} "Bad request"
// @Security AuthBearerBenefactor
// @Router /address/ [get]
func (h Handler) GetAddresses(c echo.Context) error {
var req param.GetAllAddressesRequest
claims := claim.GetClaimsFromEchoContext(c)
req.BenefactorID = claims.UserID
resp, sErr := h.addressSvc.GetAll(c.Request().Context(), req)
if sErr != nil {
msg, code := httpmsg.Error(sErr)
return echo.NewHTTPError(code, msg)
}
return c.JSON(http.StatusOK, resp)
}

View File

@ -15,4 +15,6 @@ func (h Handler) SetRoutes(e *echo.Echo) {
middleware.BenefactorAuthorization(entity.UserBenefactorRole))
r.GET("/:id", h.GetAddress, middleware.Auth(h.authSvc, h.authConfig),
middleware.BenefactorAuthorization(entity.UserBenefactorRole))
r.GET("/", h.GetAddresses, middleware.Auth(h.authSvc, h.authConfig),
middleware.BenefactorAuthorization(entity.UserBenefactorRole))
}

View File

@ -16,6 +16,37 @@ const docTemplate = `{
"basePath": "{{.BasePath}}",
"paths": {
"/address/": {
"get": {
"security": [
{
"AuthBearerBenefactor": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Address"
],
"summary": "Get all benefactor addresses",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/addressparam.GetAllAddressesResponse"
}
},
"400": {
"description": "Bad request",
"schema": {
"type": "string"
}
}
}
},
"post": {
"security": [
{
@ -772,6 +803,17 @@ const docTemplate = `{
}
}
},
"addressparam.GetAllAddressesResponse": {
"type": "object",
"properties": {
"all_addresses": {
"type": "array",
"items": {
"$ref": "#/definitions/entity.Address"
}
}
}
},
"addressparam.GetAllCitiesResponse": {
"type": "object",
"properties": {

View File

@ -5,6 +5,37 @@
},
"paths": {
"/address/": {
"get": {
"security": [
{
"AuthBearerBenefactor": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Address"
],
"summary": "Get all benefactor addresses",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/addressparam.GetAllAddressesResponse"
}
},
"400": {
"description": "Bad request",
"schema": {
"type": "string"
}
}
}
},
"post": {
"security": [
{
@ -761,6 +792,17 @@
}
}
},
"addressparam.GetAllAddressesResponse": {
"type": "object",
"properties": {
"all_addresses": {
"type": "array",
"items": {
"$ref": "#/definitions/entity.Address"
}
}
}
},
"addressparam.GetAllCitiesResponse": {
"type": "object",
"properties": {

View File

@ -30,6 +30,13 @@ definitions:
address:
$ref: '#/definitions/entity.Address'
type: object
addressparam.GetAllAddressesResponse:
properties:
all_addresses:
items:
$ref: '#/definitions/entity.Address'
type: array
type: object
addressparam.GetAllCitiesResponse:
properties:
cities:
@ -491,6 +498,25 @@ info:
contact: {}
paths:
/address/:
get:
consumes:
- application/json
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/addressparam.GetAllAddressesResponse'
"400":
description: Bad request
schema:
type: string
security:
- AuthBearerBenefactor: []
summary: Get all benefactor addresses
tags:
- Address
post:
consumes:
- application/json

View File

@ -0,0 +1,11 @@
package addressparam
import "git.gocasts.ir/ebhomengo/niki/entity"
type GetAllAddressesRequest struct {
BenefactorID uint
}
type GetAllAddressesResponse struct {
AllAddresses []entity.Address `json:"all_addresses"`
}

View File

@ -0,0 +1,38 @@
package mysqladdress
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) GetAddresses(ctx context.Context, benefactorID uint) ([]entity.Address, error) {
const op = "mysqladdress.GetAddresses"
rows, err := d.conn.Conn().QueryContext(ctx, `select * from addresses where benefactor_id = ?`, benefactorID)
if err != nil {
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
defer rows.Close()
addresses := make([]entity.Address, 0)
for rows.Next() {
address, sErr := scanAddress(rows)
if sErr != nil {
return nil, richerror.New(op).WithErr(sErr).
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
}
addresses = append(addresses, address)
}
if rErr := rows.Err(); rErr != nil {
return nil, richerror.New(op).WithErr(rErr).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
return addresses, nil
}

View File

@ -0,0 +1,19 @@
package benefactoraddressservice
import (
"context"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/address"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) GetAll(ctx context.Context, request param.GetAllAddressesRequest) (param.GetAllAddressesResponse, error) {
const op = "benefactoraddressservice.GetAll"
addresses, err := s.repo.GetAddresses(ctx, request.BenefactorID)
if err != nil {
return param.GetAllAddressesResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
}
return param.GetAllAddressesResponse{AllAddresses: addresses}, nil
}

View File

@ -12,6 +12,7 @@ type Repository interface {
GetAllProvinces(ctx context.Context) ([]entity.Province, error)
GetAllCities(ctx context.Context) ([]entity.City, error)
GetAddress(ctx context.Context, addressID uint, benefactorID uint) (entity.Address, error)
GetAddresses(ctx context.Context, benefactorID uint) ([]entity.Address, error)
}
type Service struct {