2024-01-22 14:41:55 +00:00
|
|
|
package benefactoraddresshandler
|
2024-01-19 16:37:15 +00:00
|
|
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
// AddAddress godoc
|
2024-05-17 20:16:28 +00:00
|
|
|
// @Summary Add a new address for a benefactor
|
|
|
|
// @Description This endpoint allows an authenticated benefactor to add a new address to their account.
|
|
|
|
// @Tags Address
|
2024-05-14 13:07:09 +00:00
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
2024-05-17 20:16:28 +00:00
|
|
|
// @Param Request body param.BenefactorAddAddressRequest true "New address details"
|
|
|
|
// @Success 201 {object} param.BenefactorAddAddressResponse
|
2024-05-14 13:07:09 +00:00
|
|
|
// @Failure 400 {string} "Bad request"
|
2024-05-15 07:38:39 +00:00
|
|
|
// @Security AuthBearerBenefactor
|
2024-06-14 08:41:36 +00:00
|
|
|
// @Router /address/ [post].
|
2024-01-19 16:37:15 +00:00
|
|
|
func (h Handler) AddAddress(c echo.Context) error {
|
2024-01-22 14:41:55 +00:00
|
|
|
req := param.BenefactorAddAddressRequest{}
|
2024-01-19 16:37:15 +00:00
|
|
|
if bErr := c.Bind(&req); bErr != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest)
|
|
|
|
}
|
2024-01-22 14:41:55 +00:00
|
|
|
claims := claim.GetClaimsFromEchoContext(c)
|
|
|
|
req.BenefactorID = claims.UserID
|
2024-01-19 16:37:15 +00:00
|
|
|
|
2024-01-22 14:41:55 +00:00
|
|
|
resp, sErr := h.addressSvc.Add(c.Request().Context(), req)
|
2024-01-19 16:37:15 +00:00
|
|
|
|
|
|
|
if sErr != nil {
|
|
|
|
msg, code := httpmsg.Error(sErr)
|
2024-08-01 10:20:18 +00:00
|
|
|
if resp.FieldErrors != nil {
|
|
|
|
return c.JSON(code, echo.Map{
|
|
|
|
"message": msg,
|
|
|
|
"errors": resp.FieldErrors,
|
|
|
|
})
|
|
|
|
}
|
2024-01-19 16:37:15 +00:00
|
|
|
|
|
|
|
return echo.NewHTTPError(code, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusCreated, resp)
|
|
|
|
}
|