Compare commits

...

1 Commits

13 changed files with 403 additions and 25 deletions

View File

@ -17,4 +17,6 @@ func (h Handler) SetRoutes(e *echo.Echo) {
r.POST("/", h.Add)
r.GET("/:id", h.Get)
r.DELETE("/:id", h.Delete)
r.PATCH("/:id", h.Update)
}

View File

@ -0,0 +1,58 @@
package benefactorkindboxreqhandler
import (
"net/http"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
claim "git.gocasts.ir/ebhomengo/niki/pkg/claim"
echo "github.com/labstack/echo/v4"
)
// UpdateKindBoxReq godoc
// @Summary update kindBoxReq
// @Tags KindBoxReq
// @Accept json
// @Produce json
// @Param id path int true "KindBoxReq ID"
// @Param Request body param.KindBoxReqUpdateRequest true "Update kind box req fields"
// @Success 204 {object} param.KindBoxReqUpdateResponse
// @Failure 400 {string} "Bad request"
// @Security AuthBearerBenefactor
// @Router /benefactor/kindboxreqs/{id} [patch]
func (h Handler) Update(c echo.Context) error {
var req param.KindBoxReqUpdateRequest
if err := c.Bind(&req); err != nil {
return c.JSON(http.StatusBadRequest, echo.Map{
"message": errmsg.ErrBadRequest,
})
}
if bErr := echo.PathParamsBinder(c).Uint("id", &req.KindBoxReqID).BindError(); bErr != nil {
return echo.NewHTTPError(http.StatusBadRequest)
}
claims := claim.GetClaimsFromEchoContext(c)
req.BenefactorID = claims.UserID
if fieldErrors, err := h.benefactorKindBoxReqVld.ValidateUpdateRequest(req); err != nil {
msg, code := httpmsg.Error(err)
return c.JSON(code, echo.Map{
"message": msg,
"errors": fieldErrors,
})
}
resp, err := h.benefactorKindBoxReqSvc.UpdateKindBoxReq(c.Request().Context(), req)
if err != nil {
msg, code := httpmsg.Error(err)
return echo.NewHTTPError(code, msg)
}
return c.JSON(http.StatusOK, resp)
}

View File

@ -868,6 +868,55 @@ const docTemplate = `{
}
}
}
},
"patch": {
"security": [
{
"AuthBearerBenefactor": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"KindBoxReq"
],
"summary": "update kindBoxReq",
"parameters": [
{
"type": "integer",
"description": "KindBoxReq ID",
"name": "id",
"in": "path",
"required": true
},
{
"description": "Update kind box req fields",
"name": "Request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/benefactorkindboxreqparam.KindBoxReqUpdateRequest"
}
}
],
"responses": {
"204": {
"description": "No Content",
"schema": {
"$ref": "#/definitions/benefactorkindboxreqparam.KindBoxReqUpdateResponse"
}
},
"400": {
"description": "Bad request",
"schema": {
"type": "string"
}
}
}
}
},
"/benefactor/login-register": {
@ -1574,6 +1623,47 @@ const docTemplate = `{
}
}
},
"benefactorkindboxreqparam.KindBoxReqUpdateRequest": {
"type": "object",
"properties": {
"count_requested": {
"type": "integer",
"example": 2
},
"deliver_address_id": {
"type": "integer",
"example": 2
},
"deliver_refer_date": {
"type": "string",
"example": "2025-01-02 15:04:05"
},
"deliver_refer_time_id": {
"type": "integer",
"example": 1
},
"description": {
"type": "string",
"example": "this is a description for..."
},
"kind_box_type": {
"allOf": [
{
"$ref": "#/definitions/entity.KindBoxType"
}
],
"example": 1
}
}
},
"benefactorkindboxreqparam.KindBoxReqUpdateResponse": {
"type": "object",
"properties": {
"kind_box_req": {
"$ref": "#/definitions/entity.KindBoxReq"
}
}
},
"entity.Address": {
"type": "object",
"properties": {

View File

@ -857,6 +857,55 @@
}
}
}
},
"patch": {
"security": [
{
"AuthBearerBenefactor": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"KindBoxReq"
],
"summary": "update kindBoxReq",
"parameters": [
{
"type": "integer",
"description": "KindBoxReq ID",
"name": "id",
"in": "path",
"required": true
},
{
"description": "Update kind box req fields",
"name": "Request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/benefactorkindboxreqparam.KindBoxReqUpdateRequest"
}
}
],
"responses": {
"204": {
"description": "No Content",
"schema": {
"$ref": "#/definitions/benefactorkindboxreqparam.KindBoxReqUpdateResponse"
}
},
"400": {
"description": "Bad request",
"schema": {
"type": "string"
}
}
}
}
},
"/benefactor/login-register": {
@ -1563,6 +1612,47 @@
}
}
},
"benefactorkindboxreqparam.KindBoxReqUpdateRequest": {
"type": "object",
"properties": {
"count_requested": {
"type": "integer",
"example": 2
},
"deliver_address_id": {
"type": "integer",
"example": 2
},
"deliver_refer_date": {
"type": "string",
"example": "2025-01-02 15:04:05"
},
"deliver_refer_time_id": {
"type": "integer",
"example": 1
},
"description": {
"type": "string",
"example": "this is a description for..."
},
"kind_box_type": {
"allOf": [
{
"$ref": "#/definitions/entity.KindBoxType"
}
],
"example": 1
}
}
},
"benefactorkindboxreqparam.KindBoxReqUpdateResponse": {
"type": "object",
"properties": {
"kind_box_req": {
"$ref": "#/definitions/entity.KindBoxReq"
}
}
},
"entity.Address": {
"type": "object",
"properties": {

View File

@ -408,6 +408,33 @@ definitions:
kind_box_req:
$ref: '#/definitions/entity.KindBoxReq'
type: object
benefactorkindboxreqparam.KindBoxReqUpdateRequest:
properties:
count_requested:
example: 2
type: integer
deliver_address_id:
example: 2
type: integer
deliver_refer_date:
example: "2025-01-02 15:04:05"
type: string
deliver_refer_time_id:
example: 1
type: integer
description:
example: this is a description for...
type: string
kind_box_type:
allOf:
- $ref: '#/definitions/entity.KindBoxType'
example: 1
type: object
benefactorkindboxreqparam.KindBoxReqUpdateResponse:
properties:
kind_box_req:
$ref: '#/definitions/entity.KindBoxReq'
type: object
entity.Address:
properties:
address:
@ -1110,6 +1137,37 @@ paths:
summary: Get a kind box request for a benefactor
tags:
- KindBoxReq
patch:
consumes:
- application/json
parameters:
- description: KindBoxReq ID
in: path
name: id
required: true
type: integer
- description: Update kind box req fields
in: body
name: Request
required: true
schema:
$ref: '#/definitions/benefactorkindboxreqparam.KindBoxReqUpdateRequest'
produces:
- application/json
responses:
"204":
description: No Content
schema:
$ref: '#/definitions/benefactorkindboxreqparam.KindBoxReqUpdateResponse'
"400":
description: Bad request
schema:
type: string
security:
- AuthBearerBenefactor: []
summary: update kindBoxReq
tags:
- KindBoxReq
/benefactor/login-register:
post:
consumes:

View File

@ -1,14 +1,21 @@
package benefactorkindboxreqparam
import entity "git.gocasts.ir/ebhomengo/niki/entity"
import (
entity "git.gocasts.ir/ebhomengo/niki/entity"
)
type KindBoxReqUpdateRequest struct {
BenefactorID uint
KindBoxReqID uint
TypeID entity.KindBoxType
CountRequested uint
KindBoxReqID uint `json:"-"`
BenefactorID uint `json:"-"`
KindBoxType entity.KindBoxType `json:"kind_box_type" example:"1"`
CountRequested uint `json:"count_requested" example:"2"`
Description string `json:"description" example:"this is a description for..."`
DeliverReferDate string `json:"deliver_refer_date" example:"2025-01-02 15:04:05"`
DeliverReferTimeID uint `json:"deliver_refer_time_id" example:"1"`
DeliverAddressID uint `json:"deliver_address_id" example:"2"`
}
type KindBoxReqUpdateResponse struct {
KindBoxReq entity.KindBoxReq
KindBoxReq entity.KindBoxReq `json:"kind_box_req"`
}

View File

@ -12,8 +12,8 @@ import (
func (d DB) GetAwaitingDeliveryByAgent(ctx context.Context, kindBoxReqID uint, agentID uint) (entity.KindBoxReq, error) {
const op = "mysqlkindboxreq.GetAwaitingDeliveryByAgent"
query := `SELECT * FROM kind_box_reqs WHERE id = ? AND sender_agent_id = ? AND status = 'assigned-sender-agent' AND deleted_at IS NULL `
row := d.conn.Conn().QueryRowContext(ctx, query, kindBoxReqID, agentID)
query := `SELECT * FROM kind_box_reqs WHERE id = ? AND sender_agent_id = ? AND status = ? AND deleted_at IS NULL `
row := d.conn.Conn().QueryRowContext(ctx, query, kindBoxReqID, agentID, entity.KindBoxReqAssignedSenderAgentStatus.String())
k, err := scanKindBoxReq(row)
if err != nil {
if err == sql.ErrNoRows {

View File

@ -0,0 +1,43 @@
package mysqlkindboxreq
import (
"context"
entity "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) UpdateKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error) {
const op = "mysqlkindboxreq.UpdateKindBoxReq"
query := `UPDATE kind_box_reqs SET
kind_box_type = ?, count_requested = ?, description = ?, deliver_refer_time_id = ?,
deliver_refer_date = ?, deliver_address_id = ?
WHERE id = ? AND benefactor_id = ? AND deleted_at IS NULL`
_, uErr := d.conn.Conn().ExecContext(ctx, query,
kindBoxReq.KindBoxType, kindBoxReq.CountRequested,
kindBoxReq.Description, kindBoxReq.DeliverReferTimeID, kindBoxReq.DeliverReferDate,
kindBoxReq.DeliverAddressID, kindBoxReq.ID, kindBoxReq.BenefactorID)
if uErr != nil {
return entity.KindBoxReq{}, richerror.New(op).WithErr(uErr).WithMessage(errmsg.ErrorMsgCantUpdateRecord).
WithKind(richerror.KindUnexpected)
}
updatedKindBoxReq, gErr := d.GetKindBoxReqByID(ctx, kindBoxReq.ID)
if gErr != nil {
return entity.KindBoxReq{}, richerror.New(op).WithErr(gErr).WithMessage(errmsg.ErrorMsgNotFound).
WithKind(richerror.KindUnexpected)
}
return updatedKindBoxReq, nil
}

View File

@ -10,6 +10,7 @@ type Repository interface {
AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error)
GetKindBoxReqByID(ctx context.Context, kindBoxReqID uint) (entity.KindBoxReq, error)
DeleteKindBoxReqByID(ctx context.Context, kindBoxReqID uint) error
UpdateKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error)
}
type Service struct {

View File

@ -0,0 +1,37 @@
package benefactorkindboxreqservice
import (
"context"
"time"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
entity "git.gocasts.ir/ebhomengo/niki/entity"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) UpdateKindBoxReq(ctx context.Context, req param.KindBoxReqUpdateRequest) (param.KindBoxReqUpdateResponse, error) {
const op = "benefactorkindboxreqservice.UpdateKindBoxReq"
t, tErr := time.Parse(time.DateTime, req.DeliverReferDate)
if tErr != nil {
return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithErr(tErr).WithKind(richerror.KindInvalid)
}
updatedKindBoxReq, err := s.repo.UpdateKindBoxReq(ctx, entity.KindBoxReq{
ID: req.KindBoxReqID,
BenefactorID: req.BenefactorID,
KindBoxType: req.KindBoxType,
CountRequested: req.CountRequested,
Description: req.Description,
DeliverReferDate: t,
DeliverReferTimeID: req.DeliverReferTimeID,
DeliverAddressID: req.DeliverAddressID,
})
if err != nil {
return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
}
return param.KindBoxReqUpdateResponse{KindBoxReq: updatedKindBoxReq}, nil
}

View File

@ -21,7 +21,7 @@ func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[strin
validation.Field(&req.TypeID,
validation.Required,
validation.By(v.doesTypeExist)),
validation.By(v.doesKindBoxTypeExist)),
validation.Field(&req.DeliverAddressID,
validation.Required,

View File

@ -10,24 +10,16 @@ import (
)
func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map[string]string, error) {
const op = "userkindboxreqvalidator.ValidateUpdateRequest"
const op = "benefactorkindboxreqvalidator.ValidateUpdateRequest"
if err := validation.ValidateStruct(&req,
validation.Field(&req.KindBoxReqID, validation.Required, validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID)), validation.By(v.doesKindBoxRequestHavePendingStatus)),
validation.Field(&req.KindBoxType, validation.Required, validation.By(v.doesTypeExist)),
validation.Field(&req.CountRequested, validation.Required, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)),
validation.Field(&req.BenefactorID,
validation.Required,
validation.By(v.doesBenefactorExist)),
// validation.Field(&req.KindBoxReqID,
// validation.Required,
// validation.By(v.doesKindBoxRequestExist),
// validation.By(v.hasPendingStatus),
// validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))),
validation.Field(&req.TypeID,
validation.Required,
validation.By(v.doesTypeExist)),
validation.Field(&req.DeliverReferDate, validation.Required, validation.By(v.isDateValid)),
validation.Field(&req.DeliverReferTimeID, validation.Required, validation.By(v.isReferTimeIDValid)),
validation.Field(&req.DeliverAddressID, validation.Required, validation.By(v.doesAddressExist(req.BenefactorID))),
); err != nil {
fieldErrors := make(map[string]string)

View File

@ -72,7 +72,7 @@ func (v Validator) doesBenefactorExist(value interface{}) error {
return nil
}
func (v Validator) doesTypeExist(value interface{}) error {
func (v Validator) doesKindBoxTypeExist(value interface{}) error {
typeID, ok := value.(entity.KindBoxType)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)