Merge pull request 'feat(niki): add benefactor get and get all addresses' (#53) from stage/erfan/98-benefactor-get-and-get-all-address into develop

Reviewed-on: ebhomengo/niki#53
Reviewed-by: hossein <h.nazari1990@gmail.com>
This commit is contained in:
hossein 2024-06-05 05:38:10 +00:00
commit 4aec4d8ecb
14 changed files with 487 additions and 0 deletions

View File

@ -0,0 +1,46 @@
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"
)
// GetAddress godoc
// @Summary Get a benefactor address
// @Tags Address
// @Accept json
// @Produce json
// @Param id path int true "Address ID"
// @Success 200 {object} param.GetAddressResponse
// @Failure 400 {string} "Bad request"
// @Security AuthBearerBenefactor
// @Router /address/{id} [get]
func (h Handler) GetAddress(c echo.Context) error {
var req param.GetAddressRequest
if bErr := echo.PathParamsBinder(c).Uint("id", &req.AddressID).BindError(); bErr != nil {
return echo.NewHTTPError(http.StatusBadRequest)
}
claims := claim.GetClaimsFromEchoContext(c)
req.BenefactorID = claims.UserID
if fieldErrors, err := h.addressVld.ValidateGetAddress(req); err != nil {
msg, code := httpmsg.Error(err)
return c.JSON(code, echo.Map{
"message": msg,
"errors": fieldErrors,
})
}
resp, sErr := h.addressSvc.Get(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

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

@ -13,4 +13,8 @@ func (h Handler) SetRoutes(e *echo.Echo) {
r.GET("/cities", h.GetAllCities) r.GET("/cities", h.GetAllCities)
r.POST("/", h.AddAddress, middleware.Auth(h.authSvc, h.authConfig), r.POST("/", h.AddAddress, middleware.Auth(h.authSvc, h.authConfig),
middleware.BenefactorAuthorization(entity.UserBenefactorRole)) 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}}", "basePath": "{{.BasePath}}",
"paths": { "paths": {
"/address/": { "/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": { "post": {
"security": [ "security": [
{ {
@ -116,6 +147,48 @@ const docTemplate = `{
} }
} }
}, },
"/address/{id}": {
"get": {
"security": [
{
"AuthBearerBenefactor": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Address"
],
"summary": "Get a benefactor address",
"parameters": [
{
"type": "integer",
"description": "Address ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/addressparam.GetAddressResponse"
}
},
"400": {
"description": "Bad request",
"schema": {
"type": "string"
}
}
}
}
},
"/admin/kindboxreqs": { "/admin/kindboxreqs": {
"get": { "get": {
"security": [ "security": [
@ -763,6 +836,25 @@ const docTemplate = `{
} }
} }
}, },
"addressparam.GetAddressResponse": {
"type": "object",
"properties": {
"address": {
"$ref": "#/definitions/entity.Address"
}
}
},
"addressparam.GetAllAddressesResponse": {
"type": "object",
"properties": {
"all_addresses": {
"type": "array",
"items": {
"$ref": "#/definitions/entity.Address"
}
}
}
},
"addressparam.GetAllCitiesResponse": { "addressparam.GetAllCitiesResponse": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -5,6 +5,37 @@
}, },
"paths": { "paths": {
"/address/": { "/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": { "post": {
"security": [ "security": [
{ {
@ -105,6 +136,48 @@
} }
} }
}, },
"/address/{id}": {
"get": {
"security": [
{
"AuthBearerBenefactor": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Address"
],
"summary": "Get a benefactor address",
"parameters": [
{
"type": "integer",
"description": "Address ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/addressparam.GetAddressResponse"
}
},
"400": {
"description": "Bad request",
"schema": {
"type": "string"
}
}
}
}
},
"/admin/kindboxreqs": { "/admin/kindboxreqs": {
"get": { "get": {
"security": [ "security": [
@ -752,6 +825,25 @@
} }
} }
}, },
"addressparam.GetAddressResponse": {
"type": "object",
"properties": {
"address": {
"$ref": "#/definitions/entity.Address"
}
}
},
"addressparam.GetAllAddressesResponse": {
"type": "object",
"properties": {
"all_addresses": {
"type": "array",
"items": {
"$ref": "#/definitions/entity.Address"
}
}
}
},
"addressparam.GetAllCitiesResponse": { "addressparam.GetAllCitiesResponse": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -25,6 +25,18 @@ definitions:
address: address:
$ref: '#/definitions/entity.Address' $ref: '#/definitions/entity.Address'
type: object type: object
addressparam.GetAddressResponse:
properties:
address:
$ref: '#/definitions/entity.Address'
type: object
addressparam.GetAllAddressesResponse:
properties:
all_addresses:
items:
$ref: '#/definitions/entity.Address'
type: array
type: object
addressparam.GetAllCitiesResponse: addressparam.GetAllCitiesResponse:
properties: properties:
cities: cities:
@ -488,6 +500,25 @@ info:
contact: {} contact: {}
paths: paths:
/address/: /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: post:
consumes: consumes:
- application/json - application/json
@ -516,6 +547,32 @@ paths:
summary: Add a new address for a benefactor summary: Add a new address for a benefactor
tags: tags:
- Address - Address
/address/{id}:
get:
consumes:
- application/json
parameters:
- description: Address ID
in: path
name: id
required: true
type: integer
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/addressparam.GetAddressResponse'
"400":
description: Bad request
schema:
type: string
security:
- AuthBearerBenefactor: []
summary: Get a benefactor address
tags:
- Address
/address/cities: /address/cities:
get: get:
consumes: consumes:

View File

@ -8,3 +8,11 @@ type GetAddressByIDRequest struct {
type GetAddressByIDResponse struct { type GetAddressByIDResponse struct {
Address *entity.Address Address *entity.Address
} }
type GetAddressRequest struct {
BenefactorID uint
AddressID uint
}
type GetAddressResponse struct {
Address entity.Address `json:"address"`
}

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

@ -35,6 +35,29 @@ func (d *DB) GetAddressByID(ctx context.Context, id uint) (*entity.Address, erro
return &address, nil return &address, nil
} }
func (d *DB) GetAddress(ctx context.Context, addressID uint, benefactorID uint) (entity.Address, error) {
const op = "mysqladdress.GetAddress"
row := d.conn.Conn().QueryRowContext(ctx, `select * from addresses where id = ? and benefactor_id = ?`, addressID, benefactorID)
address, err := scanAddress(row)
if err != nil {
sErr := sql.ErrNoRows
//TODO-errorsas: second argument to errors.As should not be *error
//nolint
if errors.As(err, &sErr) {
return entity.Address{}, richerror.New(op).WithMessage(errmsg.ErrorMsgNotFound).
WithKind(richerror.KindNotFound)
}
// TODO - log unexpected error for better observability
return entity.Address{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
}
return address, nil
}
func scanAddress(scanner mysql.Scanner) (entity.Address, error) { func scanAddress(scanner mysql.Scanner) (entity.Address, error) {
var createdAt, updatedAt time.Time var createdAt, updatedAt time.Time
var address entity.Address var address entity.Address

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,18 @@
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) Get(ctx context.Context, req param.GetAddressRequest) (param.GetAddressResponse, error) {
const op = "benefactoraddressservice.Get"
address, err := s.repo.GetAddress(ctx, req.AddressID, req.BenefactorID)
if err != nil {
return param.GetAddressResponse{}, richerror.New(op).WithErr(err)
}
return param.GetAddressResponse{Address: address}, 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

@ -11,6 +11,8 @@ type Repository interface {
GetAddressByID(ctx context.Context, id uint) (*entity.Address, error) GetAddressByID(ctx context.Context, id uint) (*entity.Address, error)
GetAllProvinces(ctx context.Context) ([]entity.Province, error) GetAllProvinces(ctx context.Context) ([]entity.Province, error)
GetAllCities(ctx context.Context) ([]entity.City, 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 { type Service struct {

View File

@ -0,0 +1,42 @@
package benefactoraddressvalidator
import (
"errors"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/address"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
func (v Validator) ValidateGetAddress(req param.GetAddressRequest) (map[string]string, error) {
const op = "benefactorvalidator.ValidateGetRequest"
if err := validation.ValidateStruct(&req,
validation.Field(&req.BenefactorID, validation.Required,
validation.By(v.doesBenefactorExist)),
validation.Field(&req.AddressID, validation.Required,
validation.Min(uint(1))),
); err != nil {
fieldErrors := make(map[string]string)
var errV validation.Errors
if errors.As(err, &errV) {
for key, value := range errV {
if value != nil {
fieldErrors[key] = value.Error()
}
}
}
return fieldErrors, richerror.New(op).
WithMessage(errmsg.ErrorMsgInvalidInput).
WithKind(richerror.KindInvalid).
WithMeta(map[string]interface{}{"req": req}).
WithErr(err)
}
return map[string]string{}, nil
}