feat(niki): add benefactor delete address

This commit is contained in:
Erfan Mohammadi 2024-06-08 17:55:24 +03:30 committed by imirazimi
parent b7d5eb522b
commit d5add40d9e
16 changed files with 280 additions and 5 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"
)
// DeleteAddress godoc
// @Summary Delete address by benefactor
// @Description This endpoint is used to delete an address by benefactor
// @Tags Address
// @Param id path int true "Address ID"
// @Success 204
// @Failure 400 {string} "Bad request"
// @Security AuthBearerBenefactor
// @Router /address/{id} [delete]
func (h Handler) DeleteAddress(c echo.Context) error {
var req param.DeleteAddressRequest
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.ValidateDeleteRequest(req); err != nil {
msg, code := httpmsg.Error(err)
return c.JSON(code, echo.Map{
"message": msg,
"errors": fieldErrors,
})
}
dErr := h.addressSvc.Delete(c.Request().Context(), req)
if dErr != nil {
msg, code := httpmsg.Error(dErr)
return echo.NewHTTPError(code, msg)
}
return c.JSON(http.StatusNoContent, nil)
}

View File

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

View File

@ -187,6 +187,38 @@ const docTemplate = `{
}
}
}
},
"delete": {
"security": [
{
"AuthBearerBenefactor": []
}
],
"description": "This endpoint is used to delete an address by benefactor",
"tags": [
"Address"
],
"summary": "Delete address by benefactor",
"parameters": [
{
"type": "integer",
"description": "Address ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"204": {
"description": "No Content"
},
"400": {
"description": "Bad request",
"schema": {
"type": "string"
}
}
}
}
},
"/admin/kindboxreqs": {

View File

@ -176,6 +176,38 @@
}
}
}
},
"delete": {
"security": [
{
"AuthBearerBenefactor": []
}
],
"description": "This endpoint is used to delete an address by benefactor",
"tags": [
"Address"
],
"summary": "Delete address by benefactor",
"parameters": [
{
"type": "integer",
"description": "Address ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"204": {
"description": "No Content"
},
"400": {
"description": "Bad request",
"schema": {
"type": "string"
}
}
}
}
},
"/admin/kindboxreqs": {

View File

@ -553,6 +553,26 @@ paths:
tags:
- Address
/address/{id}:
delete:
description: This endpoint is used to delete an address by benefactor
parameters:
- description: Address ID
in: path
name: id
required: true
type: integer
responses:
"204":
description: No Content
"400":
description: Bad request
schema:
type: string
security:
- AuthBearerBenefactor: []
summary: Delete address by benefactor
tags:
- Address
get:
consumes:
- application/json

View File

@ -0,0 +1,6 @@
package addressparam
type DeleteAddressRequest struct {
AddressID uint
BenefactorID uint
}

View File

@ -25,4 +25,5 @@ const (
ErrorMsgReferTimeNotFound = "refer time not found"
ErrorMsgReferTimeIsNotActive = "refer time is not active"
ErrorMsgKindBoxReqDoesntBelongToBenefactor = "kind box req doesnt belong to benefactor"
ErrorMsgCantDeleteAddress = "can't delete address"
)

View File

@ -0,0 +1,20 @@
package mysqladdress
import (
"context"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (d *DB) DeleteBenefactorAddress(ctx context.Context, addressID uint, benefactorID uint) error {
const op = "mysqladdress.DeleteBenefactorAddress"
_, err := d.conn.Conn().ExecContext(ctx, `UPDATE addresses SET deleted_at = CURRENT_TIMESTAMP WHERE id = ? AND benefactor_id = ? AND deleted_at IS NULL`, addressID, benefactorID)
if err != nil {
return richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected).WithMessage(errmsg.ErrorMsgCantDeleteAddress)
}
return nil
}

View File

@ -0,0 +1,32 @@
package mysqladdress
import (
"context"
"database/sql"
"errors"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (d *DB) IsExistAddressByID(ctx context.Context, addressID uint, benefactorID uint) (bool, error) {
const op = "mysqladdress.IsExistAddressByID"
row := d.conn.Conn().QueryRowContext(ctx, `select * from addresses where id = ? and benefactor_id = ? and deleted_at is null `, addressID, benefactorID)
_, 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 false, nil
}
// TODO - log unexpected error for better observability
return false, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
}
return true, nil
}

View File

@ -16,7 +16,7 @@ import (
func (d *DB) GetAddressByID(ctx context.Context, id uint) (*entity.Address, error) {
const op = "mysqladdress.IsExistAddressByID"
row := d.conn.Conn().QueryRowContext(ctx, `select * from addresses where id = ?`, id)
row := d.conn.Conn().QueryRowContext(ctx, `select * from addresses where id = ? and deleted_at is null`, id)
address, err := scanAddress(row)
if err != nil {
@ -38,7 +38,7 @@ func (d *DB) GetAddressByID(ctx context.Context, id uint) (*entity.Address, erro
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)
row := d.conn.Conn().QueryRowContext(ctx, `select * from addresses where id = ? and benefactor_id = ? and deleted_at is null`, addressID, benefactorID)
address, err := scanAddress(row)
if err != nil {
@ -60,11 +60,12 @@ func (d *DB) GetAddress(ctx context.Context, addressID uint, benefactorID uint)
func scanAddress(scanner mysql.Scanner) (entity.Address, error) {
var createdAt, updatedAt time.Time
var deletedAt sql.NullTime
var address entity.Address
err := scanner.Scan(&address.ID, &address.PostalCode, &address.Address, &address.Lat, &address.Lon,
&address.Name, &address.CityID, &address.ProvinceID, &address.BenefactorID,
&createdAt, &updatedAt)
&createdAt, &updatedAt, &deletedAt)
return address, err
}

View File

@ -11,7 +11,7 @@ import (
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)
rows, err := d.conn.Conn().QueryContext(ctx, `select * from addresses where benefactor_id = ? and deleted_at is null`, benefactorID)
if err != nil {
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)

View File

@ -12,6 +12,7 @@ CREATE TABLE `addresses` (
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`deleted_at` TIMESTAMP,
FOREIGN KEY (`province_id`) REFERENCES `provinces` (`id`),
FOREIGN KEY (`city_id`) REFERENCES `cities` (`id`),
FOREIGN KEY (`benefactor_id`) REFERENCES `benefactors` (`id`)

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) Delete(ctx context.Context, req param.DeleteAddressRequest) error {
const op = "benefactoraddressservice.Delete"
err := s.repo.DeleteBenefactorAddress(ctx, req.AddressID, req.BenefactorID)
if err != nil {
return richerror.New(op).WithErr(err)
}
return nil
}

View File

@ -2,7 +2,6 @@ package benefactoraddressservice
import (
"context"
"git.gocasts.ir/ebhomengo/niki/entity"
)
@ -13,6 +12,7 @@ type Repository interface {
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)
DeleteBenefactorAddress(ctx context.Context, addressID uint, benefactorID uint) error
}
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) ValidateDeleteRequest(req param.DeleteAddressRequest) (map[string]string, error) {
const op = "benefactoraddressvalidator.ValidateDeleteRequest"
if err := validation.ValidateStruct(&req,
validation.Field(&req.BenefactorID,
validation.Required,
validation.By(v.doesBenefactorExist)),
validation.Field(&req.AddressID,
validation.Required,
validation.By(v.doesAddressExist(req.BenefactorID))),
); 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
}

View File

@ -6,6 +6,7 @@ import (
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactore"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
type BenefactorSvc interface {
@ -13,6 +14,7 @@ type BenefactorSvc interface {
}
type Repository interface {
IsExistCityByID(ctx context.Context, id uint) (bool, error)
IsExistAddressByID(ctx context.Context, addressID uint, benefactorID uint) (bool, error)
}
type Validator struct {
benefactorSvc BenefactorSvc
@ -51,3 +53,22 @@ func (v Validator) doesCityExist(value interface{}) error {
return nil
}
func (v Validator) doesAddressExist(benefactorID uint) validation.RuleFunc {
return func(value interface{}) error {
addressID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
isExisted, err := v.repository.IsExistAddressByID(context.Background(), addressID, benefactorID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
if !isExisted {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
return nil
}
}