feat(service): complete add | getByID | getAll for kind_box and kind_box_req services

This commit is contained in:
imirazimi 2023-12-23 00:55:16 +03:30
parent 1c96539af2
commit 7d76ea2d50
19 changed files with 199 additions and 44 deletions

View File

@ -4,10 +4,11 @@ import "time"
type KindBox struct { type KindBox struct {
ID uint ID uint
KindBoxReqID uint
TotalAmount uint TotalAmount uint
ReceiverId uint ReceiverID uint
SenderID uint SenderID uint
SerialNumber string SerialNumber string
Status uint Status KindBoxStatus
StatusChangedAt *time.Time StatusChangedAt *time.Time
} }

View File

@ -8,7 +8,7 @@ type KindBoxReq struct {
CountRequested uint CountRequested uint
CountAccepted uint CountAccepted uint
BenefactorID uint BenefactorID uint
Status uint Status KindBoxReqStatus
Description string Description string
StatusChangedAt *time.Time StatusChangedAt *time.Time
} }

View File

@ -0,0 +1,41 @@
package entity
type KindBoxReqStatus uint
const (
KindBoxReqPendingStatus KindBoxReqStatus = iota + 1
KindBoxReqAcceptedStatus
KindBoxReqRejectedStatus
)
const (
kindBoxReqPendingStatusStr = "pending"
kindBoxReqAcceptedStatusStr = "accepted"
kindBoxReqRejectedStatusStr = "rejected"
)
func (s KindBoxReqStatus) String() string {
switch s {
case KindBoxReqPendingStatus:
return kindBoxReqPendingStatusStr
case KindBoxReqAcceptedStatus:
return kindBoxReqAcceptedStatusStr
case KindBoxReqRejectedStatus:
return kindBoxReqRejectedStatusStr
}
return ""
}
func MapToKindBoxReqStatus(statusStr string) KindBoxReqStatus {
switch statusStr {
case kindBoxReqPendingStatusStr:
return KindBoxReqPendingStatus
case kindBoxReqAcceptedStatusStr:
return KindBoxReqAcceptedStatus
case kindBoxReqRejectedStatusStr:
return KindBoxReqRejectedStatus
}
return KindBoxReqStatus(0)
}

53
entity/kind_box_status.go Normal file
View File

@ -0,0 +1,53 @@
package entity
type KindBoxStatus uint
const (
KindBoxPendingSendStatus KindBoxStatus = iota + 1
KindBoxSentStatus
KindBoxPendingReceivedStatus
KindBoxRecievedStatus
KindBoxEnumeratedStatus
)
const (
kindBoxPendingSendStatus = "pending-send"
kindBoxSentStatus = "sent"
kindBoxPendingReceivedStatus = "pending-received"
kindBoxRecievedStatus = "received"
kindBoxEnumeratedStatus = "enumerated"
)
func (s KindBoxStatus) String() string {
switch s {
case KindBoxPendingSendStatus:
return kindBoxPendingSendStatus
case KindBoxSentStatus:
return kindBoxSentStatus
case KindBoxPendingReceivedStatus:
return kindBoxPendingReceivedStatus
case KindBoxRecievedStatus:
return kindBoxRecievedStatus
case KindBoxEnumeratedStatus:
return kindBoxEnumeratedStatus
}
return ""
}
func MapToKindBoxStatus(statusStr string) KindBoxStatus {
switch statusStr {
case kindBoxPendingSendStatus:
return KindBoxPendingSendStatus
case kindBoxSentStatus:
return KindBoxSentStatus
case kindBoxPendingReceivedStatus:
return KindBoxPendingReceivedStatus
case kindBoxRecievedStatus:
return KindBoxRecievedStatus
case kindBoxEnumeratedStatus:
return KindBoxEnumeratedStatus
}
return KindBoxStatus(0)
}

View File

@ -3,7 +3,9 @@ package param
import "git.gocasts.ir/ebhomengo/niki/entity" import "git.gocasts.ir/ebhomengo/niki/entity"
type KindBoxAddRequest struct { type KindBoxAddRequest struct {
KindBox entity.KindBox KindBoxReqID uint
SenderID uint
SerialNumber string
} }
type KindBoxAddResponse struct { type KindBoxAddResponse struct {

View File

@ -3,7 +3,9 @@ package param
import "git.gocasts.ir/ebhomengo/niki/entity" import "git.gocasts.ir/ebhomengo/niki/entity"
type KindBoxReqAddRequest struct { type KindBoxReqAddRequest struct {
KindBoxReq entity.KindBoxReq CountRequested uint
BenefactorID uint
TypeID uint
} }
type KindBoxReqAddResponse struct { type KindBoxReqAddResponse struct {

View File

@ -1,10 +1,21 @@
package kindboxservice package kindboxservice
import ( import (
entity "git.gocasts.ir/ebhomengo/niki/entity"
param "git.gocasts.ir/ebhomengo/niki/param/kind_box" param "git.gocasts.ir/ebhomengo/niki/param/kind_box"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
) )
func (s Service) Add(newKindBoxReq param.KindBoxAddRequest) (param.KindBoxAddResponse, error) { func (s Service) Add(req param.KindBoxAddRequest) (param.KindBoxAddResponse, error) {
// some code const op = "kindboxservice.Add"
panic("not implemented") kindBox, err := s.repo.AddKindBox(entity.KindBox{
KindBoxReqID: req.KindBoxReqID,
SenderID: req.SenderID,
SerialNumber: req.SerialNumber,
Status: entity.KindBoxPendingSendStatus,
})
if err != nil {
return param.KindBoxAddResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
}
return param.KindBoxAddResponse{KindBox: kindBox}, nil
} }

View File

@ -1,8 +1,15 @@
package kindboxservice package kindboxservice
import param "git.gocasts.ir/ebhomengo/niki/param/kind_box" import (
param "git.gocasts.ir/ebhomengo/niki/param/kind_box"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) GetAll() (param.KindBoxGetAllResponse, error) { func (s Service) GetAll() (param.KindBoxGetAllResponse, error) {
// some code const op = "kindboxservice.GetAll"
panic("not implement") allKindBox, err := s.repo.GetAllKindBox()
if err != nil {
return param.KindBoxGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
}
return param.KindBoxGetAllResponse{AllKindBox: allKindBox}, nil
} }

View File

@ -1,8 +1,15 @@
package kindboxservice package kindboxservice
import param "git.gocasts.ir/ebhomengo/niki/param/kind_box" import (
param "git.gocasts.ir/ebhomengo/niki/param/kind_box"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) GetByID(request param.KindBoxGetByIDRequest) (param.KindBoxGetByIDResponse, error) { func (s Service) GetByID(req param.KindBoxGetByIDRequest) (param.KindBoxGetByIDResponse, error) {
// some code const op = "kindboxservice.GetByID"
panic("not implement") kindBox, err := s.repo.GetKindBoxByID(req.KindBoxID)
if err != nil {
return param.KindBoxGetByIDResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
}
return param.KindBoxGetByIDResponse{KindBox: kindBox}, nil
} }

View File

@ -1,10 +1,21 @@
package kindboxreqservice package kindboxreqservice
import ( import (
entity "git.gocasts.ir/ebhomengo/niki/entity"
param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
) )
func (s Service) Add(newKindBoxReq param.KindBoxReqAddRequest) (param.KindBoxReqAddResponse, error) { func (s Service) Add(req param.KindBoxReqAddRequest) (param.KindBoxReqAddResponse, error) {
// some code const op = "kindboxreqservice.Add"
panic("not implemented") kindBoxReq, err := s.repo.AddKindBoxReq(entity.KindBoxReq{
TypeID: req.TypeID,
CountRequested: req.CountRequested,
BenefactorID: req.BenefactorID,
Status: entity.KindBoxReqPendingStatus,
})
if err != nil {
return param.KindBoxReqAddResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
}
return param.KindBoxReqAddResponse{KindBoxReq: kindBoxReq}, nil
} }

View File

@ -1,8 +1,15 @@
package kindboxreqservice package kindboxreqservice
import param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req" import (
param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) GetAll() (param.KindBoxReqGetAllResponse, error) { func (s Service) GetAll() (param.KindBoxReqGetAllResponse, error) {
// some code const op = "kindboxreqservice.GetAll"
panic("not implement") allKindBoxReq, err := s.repo.GetAllKindBoxReq()
if err != nil {
return param.KindBoxReqGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
}
return param.KindBoxReqGetAllResponse{AllKindBoxReq: allKindBoxReq}, nil
} }

View File

@ -1,8 +1,15 @@
package kindboxreqservice package kindboxreqservice
import param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req" import (
param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) GetByID(request param.KindBoxReqGetByIDRequest) (param.KindBoxReqGetByIDResponse, error) { func (s Service) GetByID(req param.KindBoxReqGetByIDRequest) (param.KindBoxReqGetByIDResponse, error) {
// some code const op = "kindboxreqservice.GetByID"
panic("not implement") kindBoxReq, err := s.repo.GetKindBoxReqByID(req.KindBoxReqID)
if err != nil {
return param.KindBoxReqGetByIDResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
}
return param.KindBoxReqGetByIDResponse{KindBoxReq: kindBoxReq}, nil
} }

View File

@ -2,9 +2,15 @@ package kindboxreqservice
import ( import (
param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req"
// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error".
) )
func (s Service) Update(updatedKindBoxReq param.KindBoxReqUpdateRequest) (param.KindBoxReqUpdateResponse, error) { func (s Service) Update(req param.KindBoxReqUpdateRequest) (param.KindBoxReqUpdateResponse, error) {
// some code // const op = "kindboxreqservice.Update"
panic("not implement") // kindBoxReq, err := s.repo.EditKindBoxReq(req.KindBoxReq.ID, req.KindBoxReq)
// if err != nil {
// return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
// }
// return param.KindBoxReqUpdateResponse{KindBoxReq: kindBoxReq}, nil
panic("not implemented")
} }

View File

@ -7,8 +7,8 @@ import (
validation "github.com/go-ozzo/ozzo-validation/v4" validation "github.com/go-ozzo/ozzo-validation/v4"
) )
func (v Validator) ValidateGetByIdRequest(req param.KindBoxGetByIDRequest) (map[string]string, error) { func (v Validator) ValidateGetByIDRequest(req param.KindBoxGetByIDRequest) (map[string]string, error) {
const op = "kindbox.ValidateGetByIdRequest" const op = "kindbox.ValidateGetByIDRequest"
if err := validation.Validate(&req.KindBoxID, validation.Required); err != nil { if err := validation.Validate(&req.KindBoxID, validation.Required); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -13,7 +13,7 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxUpdateRequest) (map[st
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.KindBox.SerialNumber, validation.Required), validation.Field(&req.KindBox.SerialNumber, validation.Required),
validation.Field(&req.KindBox.ReceiverId, validation.Field(&req.KindBox.ReceiverID,
validation.Required, validation.Required,
validation.By(v.doesReceiverUserExist)), validation.By(v.doesReceiverUserExist)),

View File

@ -20,8 +20,8 @@ func New(repo Repository) Validator {
} }
func (v Validator) doesReceiverUserExist(value interface{}) error { func (v Validator) doesReceiverUserExist(value interface{}) error {
receiverId := value.(int) receiverID := value.(int)
_, err := v.repo.ReceiverUserExist(receiverId) _, err := v.repo.ReceiverUserExist(receiverID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
} }
@ -30,8 +30,8 @@ func (v Validator) doesReceiverUserExist(value interface{}) error {
} }
func (v Validator) doesSenderUserExist(value interface{}) error { func (v Validator) doesSenderUserExist(value interface{}) error {
senderId := value.(int) senderID := value.(int)
_, err := v.repo.SenderUserExist(senderId) _, err := v.repo.SenderUserExist(senderID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
} }

View File

@ -12,13 +12,13 @@ func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[strin
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.KindBoxReq.CountRequested, validation.Required, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)), validation.Field(&req.CountRequested, validation.Required, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)),
validation.Field(&req.KindBoxReq.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBeneFactorExist)), validation.By(v.doesBeneFactorExist)),
validation.Field(&req.KindBoxReq.TypeID, validation.Field(&req.TypeID,
validation.Required, validation.Required,
validation.By(v.doesTypeExist)), validation.By(v.doesTypeExist)),
); err != nil { ); err != nil {

View File

@ -7,8 +7,8 @@ import (
validation "github.com/go-ozzo/ozzo-validation/v4" validation "github.com/go-ozzo/ozzo-validation/v4"
) )
func (v Validator) ValidateGetByIdRequest(req param.KindBoxReqGetByIDRequest) (map[string]string, error) { func (v Validator) ValidateGetByIDRequest(req param.KindBoxReqGetByIDRequest) (map[string]string, error) {
const op = "kindboxreq.ValidateGetByIdRequest" const op = "kindboxreq.ValidateGetByIDRequest"
if err := validation.Validate(req.KindBoxReqID, validation.Required); err != nil { if err := validation.Validate(req.KindBoxReqID, validation.Required); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -25,8 +25,8 @@ func New(repo Repository) Validator {
} }
func (v Validator) doesBeneFactorExist(value interface{}) error { func (v Validator) doesBeneFactorExist(value interface{}) error {
benefactorId := value.(int) benefactorID := value.(int)
_, err := v.repo.BeneFactorExist(benefactorId) _, err := v.repo.BeneFactorExist(benefactorID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
} }
@ -35,8 +35,8 @@ func (v Validator) doesBeneFactorExist(value interface{}) error {
} }
func (v Validator) doesTypeExist(value interface{}) error { func (v Validator) doesTypeExist(value interface{}) error {
typeId := value.(int) typeID := value.(int)
_, err := v.repo.TypeExist(typeId) _, err := v.repo.TypeExist(typeID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
} }