forked from ebhomengo/niki
1
0
Fork 0
niki/service/benefactor/kind_box_req/update.go

39 lines
1.5 KiB
Go

package userkindboxreqservice
import (
"context"
entity "git.gocasts.ir/ebhomengo/niki/entity"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) Update(ctx context.Context, req param.KindBoxReqUpdateRequest) (param.KindBoxReqUpdateResponse, error) {
// TODO: can benefactor update its request ?
// TODO: Is Update Mothod Service Responsible to check which kindboxreqID belongs to benefactorID ?
const op = "userkindboxreqservice.Update"
kbr, gErr := s.repo.GetKindBoxReq(ctx, req.KindBoxReqID)
if gErr != nil {
return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected)
}
if kbr.BenefactorID != req.BenefactorID {
return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden)
}
if kbr.Status != entity.KindBoxReqPendingStatus {
return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidStatus).WithKind(richerror.KindInvalid)
}
kindBoxReq, uErr := s.repo.UpdateKindBoxReq(ctx, req.KindBoxReqID, entity.KindBoxReq{
BenefactorID: req.BenefactorID,
TypeID: req.TypeID,
CountRequested: req.CountRequested,
})
if uErr != nil {
return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithErr(uErr).WithKind(richerror.KindUnexpected)
}
return param.KindBoxReqUpdateResponse{KindBoxReq: kindBoxReq}, nil
}