fix(niki): complete admin kind_box service

This commit is contained in:
imirazimi 2023-12-27 01:12:33 +03:30
parent 27634049bb
commit d49f1277cc
14 changed files with 104 additions and 23 deletions

View File

@ -3,6 +3,7 @@ package adminkindboxparam
import entity "git.gocasts.ir/ebhomengo/niki/entity"
type KindBoxAddRequest struct {
BenefactorID uint
KindBoxReqID uint
SenderID uint
SerialNumber string

View File

@ -1,7 +1,8 @@
package adminkindboxparam
type KindBoxDeleteRequest struct {
KindBoxID uint
BenefactorID uint
KindBoxID uint
}
type KindBoxDeleteResponse struct{}

View File

@ -3,7 +3,8 @@ package adminkindboxparam
import entity "git.gocasts.ir/ebhomengo/niki/entity"
type KindBoxGetRequest struct {
KindBoxID uint
BenefactorID uint
KindBoxID uint
}
type KindBoxGetResponse struct {

View File

@ -3,6 +3,8 @@ package adminkindboxparam
import entity "git.gocasts.ir/ebhomengo/niki/entity"
type KindBoxUpdateRequest struct {
BenefactorID uint
KindBoxID uint
TotalAmount uint
ReceiverID uint
SenderID uint

View File

@ -10,7 +10,18 @@ import (
func (s Service) Add(ctx context.Context, req param.KindBoxAddRequest) (param.KindBoxAddResponse, error) {
const op = "adminkindboxservice.Add"
// TODO: check validation
// exist, err := s.benefactorService.IsBenefactorExist(ctx, req.BenefactorID)
// if err != nil {
// return param.KindBoxAddResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
//}
//if !exist {
// return param.KindBoxAddResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotFound).WithKind(richerror.KindInvalid)
//}
kindBox, err := s.repo.AddKindBox(ctx, entity.KindBox{
BenefactorID: req.BenefactorID,
KindBoxReqID: req.KindBoxReqID,
SenderID: req.SenderID,
SerialNumber: req.SerialNumber,

View File

@ -3,10 +3,31 @@ package adminkindboxservice
import (
"context"
entity "git.gocasts.ir/ebhomengo/niki/entity"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) Delete(ctx context.Context, req param.KindBoxDeleteRequest) error {
// some code
panic("not implemented")
func (s Service) Delete(ctx context.Context, req param.KindBoxDeleteRequest) (param.KindBoxDeleteResponse, error) {
// TODO: Does business domain need to delete an kindbox ?
const op = "adminkindboxservice.Delete"
kb, gErr := s.repo.GetKindBox(ctx, req.KindBoxID)
if gErr != nil {
return param.KindBoxDeleteResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected)
}
if kb.Status != entity.KindBoxPendingSendStatus {
return param.KindBoxDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidStatus).WithKind(richerror.KindInvalid)
}
if kb.BenefactorID != req.BenefactorID {
return param.KindBoxDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden)
}
dErr := s.repo.DeleteKindBox(ctx, req.KindBoxID)
if dErr != nil {
return param.KindBoxDeleteResponse{}, richerror.New(op).WithErr(dErr).WithKind(richerror.KindUnexpected)
}
return param.KindBoxDeleteResponse{}, nil
}

View File

@ -3,15 +3,22 @@ package adminkindboxservice
import (
"context"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) Get(ctx context.Context, req param.KindBoxGetRequest) (param.KindBoxGetResponse, error) {
const op = "adminkindboxservice.Get"
kindBox, err := s.repo.GetKindBox(ctx, req.KindBoxID)
if err != nil {
return param.KindBoxGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
}
// TODO : ref to service.Update()
if kindBox.BenefactorID != req.BenefactorID {
return param.KindBoxGetResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden)
}
return param.KindBoxGetResponse{KindBox: kindBox}, nil
}

View File

@ -8,7 +8,7 @@ import (
type Repository interface {
AddKindBox(ctx context.Context, kindBox entity.KindBox) (entity.KindBox, error)
EditKindBox(ctx context.Context, kindBoxID uint, kindBoxInput entity.KindBox) (entity.KindBox, error)
UpdateKindBox(ctx context.Context, kindBoxID uint, kindBoxInput entity.KindBox) (entity.KindBox, error)
DeleteKindBox(ctx context.Context, kindBoxID uint) error
GetAllKindBox(ctx context.Context) ([]entity.KindBox, error)
GetKindBox(ctx context.Context, kindBox uint) (entity.KindBox, error)
@ -18,6 +18,11 @@ type Service struct {
repo Repository
}
// TODO: check validation.
// type BenefactorService interface {
// IsBenefactorExist(ctx context.Context, benefactorID uint) (bool, error)
// }
func New(repository Repository) Service {
return Service{
repo: repository,

View File

@ -3,10 +3,37 @@ package adminkindboxservice
import (
"context"
entity "git.gocasts.ir/ebhomengo/niki/entity"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
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.KindBoxUpdateRequest) (param.KindBoxUpdateResponse, error) {
// some code
panic("not implement")
// TODO: can benefactor update its Request ?
// TODO: Is Update Mothod Service Responsible to check which kindboxreqID belongs to benefactorID ?
// TODO: updating data(s) may have side-effect on other entities by masood-keshvary accepted -> rejected
const op = "adminkindboxservice.Update"
kb, gErr := s.repo.GetKindBox(ctx, req.KindBoxID)
if gErr != nil {
return param.KindBoxUpdateResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected)
}
if kb.BenefactorID != req.BenefactorID {
return param.KindBoxUpdateResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden)
}
kindBox, uErr := s.repo.UpdateKindBox(ctx, req.KindBoxID, entity.KindBox{
TotalAmount: req.TotalAmount,
ReceiverID: req.ReceiverID,
SenderID: req.SenderID,
SerialNumber: req.SerialNumber,
Status: req.Status,
})
if uErr != nil {
return param.KindBoxUpdateResponse{}, richerror.New(op).WithErr(uErr).WithKind(richerror.KindUnexpected)
}
return param.KindBoxUpdateResponse{KindBox: kindBox}, nil
}

View File

@ -5,7 +5,6 @@ import (
entity "git.gocasts.ir/ebhomengo/niki/entity"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
@ -13,13 +12,13 @@ func (s Service) Add(ctx context.Context, req param.KindBoxReqAddRequest) (param
const op = "adminkindboxreqservice.Add"
// TODO: check validation
kbr, err := s.repo.GetKindBoxReq(ctx, req.BenefactorID)
if err != nil {
return param.KindBoxReqAddResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
}
if kbr.BenefactorID != req.BenefactorID {
return param.KindBoxReqAddResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotFound).WithKind(richerror.KindInvalid)
}
// exist, err := s.benefactorService.IsBenefactorExist(ctx, req.BenefactorID)
// if err != nil {
// return param.KindBoxReqAddResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
//}
// if !exist {
// return param.KindBoxReqAddResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotFound).WithKind(richerror.KindInvalid)
//}
kindBoxReq, err := s.repo.AddKindBoxReq(ctx, entity.KindBoxReq{
BenefactorID: req.BenefactorID,

View File

@ -17,12 +17,12 @@ func (s Service) Delete(ctx context.Context, req param.KindBoxReqDeleteRequest)
if gErr != nil {
return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected)
}
if kbr.Status != entity.KindBoxReqPendingStatus {
return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidStatus).WithKind(richerror.KindInvalid)
}
if kbr.BenefactorID != req.BenefactorID {
return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden)
}
if kbr.Status != entity.KindBoxReqPendingStatus {
return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidStatus).WithKind(richerror.KindInvalid)
}
dErr := s.repo.DeleteKindBoxReq(ctx, req.KindBoxReqID)
if dErr != nil {

View File

@ -20,6 +20,9 @@ func (s Service) Update(ctx context.Context, req param.KindBoxReqUpdateRequest)
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)
}

View File

@ -17,12 +17,12 @@ func (s Service) Delete(ctx context.Context, req param.KindBoxReqDeleteRequest)
if gErr != nil {
return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected)
}
if kbr.Status != entity.KindBoxReqPendingStatus {
return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidStatus).WithKind(richerror.KindInvalid)
}
if kbr.BenefactorID != req.BenefactorID {
return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden)
}
if kbr.Status != entity.KindBoxReqPendingStatus {
return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidStatus).WithKind(richerror.KindInvalid)
}
dErr := s.repo.DeleteKindBoxReq(ctx, req.KindBoxReqID)
if dErr != nil {

View File

@ -18,6 +18,9 @@ func (s Service) Update(ctx context.Context, req param.KindBoxReqUpdateRequest)
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)
}