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 {
ID uint
KindBoxReqID uint
TotalAmount uint
ReceiverId uint
ReceiverID uint
SenderID uint
SerialNumber string
Status uint
Status KindBoxStatus
StatusChangedAt *time.Time
}

View File

@ -8,7 +8,7 @@ type KindBoxReq struct {
CountRequested uint
CountAccepted uint
BenefactorID uint
Status uint
Status KindBoxReqStatus
Description string
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"
type KindBoxAddRequest struct {
KindBox entity.KindBox
KindBoxReqID uint
SenderID uint
SerialNumber string
}
type KindBoxAddResponse struct {

View File

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

View File

@ -1,10 +1,21 @@
package kindboxservice
import (
entity "git.gocasts.ir/ebhomengo/niki/entity"
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) {
// some code
panic("not implemented")
func (s Service) Add(req param.KindBoxAddRequest) (param.KindBoxAddResponse, error) {
const op = "kindboxservice.Add"
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
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) {
// some code
panic("not implement")
const op = "kindboxservice.GetAll"
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
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) {
// some code
panic("not implement")
func (s Service) GetByID(req param.KindBoxGetByIDRequest) (param.KindBoxGetByIDResponse, error) {
const op = "kindboxservice.GetByID"
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
import (
entity "git.gocasts.ir/ebhomengo/niki/entity"
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) {
// some code
panic("not implemented")
func (s Service) Add(req param.KindBoxReqAddRequest) (param.KindBoxReqAddResponse, error) {
const op = "kindboxreqservice.Add"
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
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) {
// some code
panic("not implement")
const op = "kindboxreqservice.GetAll"
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
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) {
// some code
panic("not implement")
func (s Service) GetByID(req param.KindBoxReqGetByIDRequest) (param.KindBoxReqGetByIDResponse, error) {
const op = "kindboxreqservice.GetByID"
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 (
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) {
// some code
panic("not implement")
func (s Service) Update(req param.KindBoxReqUpdateRequest) (param.KindBoxReqUpdateResponse, error) {
// const op = "kindboxreqservice.Update"
// 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"
)
func (v Validator) ValidateGetByIdRequest(req param.KindBoxGetByIDRequest) (map[string]string, error) {
const op = "kindbox.ValidateGetByIdRequest"
func (v Validator) ValidateGetByIDRequest(req param.KindBoxGetByIDRequest) (map[string]string, error) {
const op = "kindbox.ValidateGetByIDRequest"
if err := validation.Validate(&req.KindBoxID, validation.Required); err != nil {
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,
validation.Field(&req.KindBox.SerialNumber, validation.Required),
validation.Field(&req.KindBox.ReceiverId,
validation.Field(&req.KindBox.ReceiverID,
validation.Required,
validation.By(v.doesReceiverUserExist)),

View File

@ -20,8 +20,8 @@ func New(repo Repository) Validator {
}
func (v Validator) doesReceiverUserExist(value interface{}) error {
receiverId := value.(int)
_, err := v.repo.ReceiverUserExist(receiverId)
receiverID := value.(int)
_, err := v.repo.ReceiverUserExist(receiverID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
@ -30,8 +30,8 @@ func (v Validator) doesReceiverUserExist(value interface{}) error {
}
func (v Validator) doesSenderUserExist(value interface{}) error {
senderId := value.(int)
_, err := v.repo.SenderUserExist(senderId)
senderID := value.(int)
_, err := v.repo.SenderUserExist(senderID)
if err != nil {
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,
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.By(v.doesBeneFactorExist)),
validation.Field(&req.KindBoxReq.TypeID,
validation.Field(&req.TypeID,
validation.Required,
validation.By(v.doesTypeExist)),
); err != nil {

View File

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

View File

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