add kind box migration and service

This commit is contained in:
Abolfazl Nourzad 2024-01-23 11:09:58 +03:30
parent 1823191c4e
commit dc9d931227
Signed by: abolfazl
GPG Key ID: 183534166EB62E0B
14 changed files with 158 additions and 114 deletions

View File

@ -13,6 +13,7 @@ func (h Handler) SetRoutes(e *echo.Echo) {
//r.GET("/", h.GetAll).Name = "admin-getallkindboxreq"
//nolint:gocritic
//r.PATCH("/:id", h.Update).Name = "admin-updatekindboxreq"
// todo - add acl
r.PATCH("/accept-kind-box-req/:id", h.Accept)
r.PATCH("/reject-kind-box-req/:id", h.Reject)
}

View File

@ -4,12 +4,13 @@ import "time"
type KindBox struct {
ID uint
KindBoxReqID uint // TODO like database model
BenefactorID uint // TODO need in business entity
KindBoxReqID uint
BenefactorID uint
Type KindBoxType
TotalAmount uint
ReceiverID uint
SenderID uint
SerialNumber string
Status KindBoxStatus
SenderID uint
ReceiverID uint
StatusChangedAt time.Time
}

View File

@ -5,8 +5,6 @@ import entity "git.gocasts.ir/ebhomengo/niki/entity"
type KindBoxAddRequest struct {
BenefactorID uint
KindBoxReqID uint
SenderID uint
SerialNumber string
}
type KindBoxAddResponse struct {

View File

@ -1,9 +1,8 @@
package adminkindboxreqparam
import (
"time"
"git.gocasts.ir/ebhomengo/niki/entity"
"time"
)
type KindBoxReqAcceptRequest struct {
@ -12,13 +11,16 @@ type KindBoxReqAcceptRequest struct {
}
type KindBoxReqAcceptResponse struct {
ID uint `json:"id"`
KindBoxType entity.KindBoxType `json:"kind_box_type"`
CountRequested uint `json:"count_requested"`
CountAccepted uint `json:"count_accepted"`
BenefactorID uint `json:"benefactor_id"`
Status entity.KindBoxReqStatus `json:"status"`
Description string `json:"description"`
ReferDate time.Time `json:"refer_date"`
AddressID uint `json:"address_id"`
KindBoxReqID uint `json:"kind_box_req_id"`
KindBoxReqStatus entity.KindBoxReqStatus `json:"kind_box_req_status"`
CountRequested uint `json:"count_requested"`
CountAccepted uint `json:"count_accepted"`
ReferDate time.Time `json:"refer_date"`
AddressID uint `json:"address_id"`
KindBoxID uint `json:"kind_box_id"`
KindBoxType entity.KindBoxType `json:"kind_box_type"`
BenefactorID uint `json:"benefactor_id"`
KindBoxStatus entity.KindBoxStatus `json:"kind_box_status"`
KindBoxSerialNumber string `json:"kind_box_serial_number"`
}

View File

@ -1 +1,10 @@
package mysqlkindbox
import (
"context"
"git.gocasts.ir/ebhomengo/niki/entity"
)
func (d DB) AddKindBox(ctx context.Context, kindBox entity.KindBox) (entity.KindBox, error) {
return entity.KindBox{}, nil
}

View File

@ -0,0 +1,24 @@
-- +migrate Up
CREATE TABLE `kind_boxes`
(
`id` INT PRIMARY KEY AUTO_INCREMENT,
`kind_box_req_id` INT NOT NULL,
`benefactor_id` INT NOT NULL,
`type` ENUM ('on-table','cylindrical','stand-up') NOT NULL,
`total_amount` INT UNSIGNED NULL NULL,
`serial_number` varchar(255),
`status` varchar(255),
`sender_id` INT NULL,
`receiver_id` INT NULL,
`status_changed_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`kind_box_req_id`) REFERENCES `kind_box_reqs` (`id`),
FOREIGN KEY (`benefactor_id`) REFERENCES `benefactors` (`id`),
FOREIGN KEY (`sender_id`) REFERENCES `admins` (`id`),
FOREIGN KEY (`receiver_id`) REFERENCES `admins` (`id`),
index `index_serial_number` (`serial_number`)
);
-- +migrate Down
DROP TABLE `kind_boxes`;

View File

@ -14,8 +14,6 @@ func (s Service) Add(ctx context.Context, req param.KindBoxAddRequest) (param.Ki
kindBox, err := s.repo.AddKindBox(ctx, entity.KindBox{
BenefactorID: req.BenefactorID,
KindBoxReqID: req.KindBoxReqID,
SenderID: req.SenderID,
SerialNumber: req.SerialNumber,
Status: entity.KindBoxPendingSendStatus,
})
if err != nil {

View File

@ -1,20 +1,20 @@
package adminkindboxservice
import (
"context"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
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"
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
}
//import (
// "context"
//
// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
//)
//
//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"
//
// 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

@ -1,19 +1,19 @@
package adminkindboxservice
import (
"context"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
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)
}
return param.KindBoxGetResponse{KindBox: kindBox}, nil
}
//import (
// "context"
//
// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
// 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)
// }
//
// return param.KindBoxGetResponse{KindBox: kindBox}, nil
//}

View File

@ -1,18 +1,18 @@
package adminkindboxservice
import (
"context"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) GetAll(ctx context.Context, _ param.KindBoxGetAllRequest) (param.KindBoxGetAllResponse, error) {
const op = "adminkindboxservice.GetAll"
allKindBox, err := s.repo.GetAllKindBox(ctx)
if err != nil {
return param.KindBoxGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
}
return param.KindBoxGetAllResponse{AllKindBox: allKindBox}, nil
}
//import (
// "context"
//
// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
//)
//
//func (s Service) GetAll(ctx context.Context, _ param.KindBoxGetAllRequest) (param.KindBoxGetAllResponse, error) {
// const op = "adminkindboxservice.GetAll"
// allKindBox, err := s.repo.GetAllKindBox(ctx)
// if err != nil {
// return param.KindBoxGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
// }
//
// return param.KindBoxGetAllResponse{AllKindBox: allKindBox}, nil
//}

View File

@ -8,10 +8,10 @@ import (
type Repository interface {
AddKindBox(ctx context.Context, kindBox 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)
//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)
}
type Service struct {

View File

@ -1,29 +1,29 @@
package adminkindboxservice
import (
"context"
entity "git.gocasts.ir/ebhomengo/niki/entity"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) Update(ctx context.Context, req param.KindBoxUpdateRequest) (param.KindBoxUpdateResponse, error) {
// 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"
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
}
//import (
// "context"
//
// entity "git.gocasts.ir/ebhomengo/niki/entity"
// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
//)
//
//func (s Service) Update(ctx context.Context, req param.KindBoxUpdateRequest) (param.KindBoxUpdateResponse, error) {
// // 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"
//
// 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

@ -2,7 +2,7 @@ package adminkindboxreqservice
import (
"context"
adminkindboxparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
@ -15,22 +15,31 @@ func (s Service) Accept(ctx context.Context, req param.KindBoxReqAcceptRequest)
}
// fire new event to create a kind-box.
// get kind box req
kindBoxReq, gErr := s.repo.GetByID(ctx, req.ID)
if gErr != nil {
return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(err)
return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(gErr)
}
res, kErr := s.kindBoxSvc.Add(ctx, adminkindboxparam.KindBoxAddRequest{
BenefactorID: kindBoxReq.BenefactorID,
KindBoxReqID: kindBoxReq.ID,
})
if kErr != nil {
return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(kErr)
}
return param.KindBoxReqAcceptResponse{
ID: kindBoxReq.ID,
KindBoxType: kindBoxReq.KindBoxType,
CountRequested: kindBoxReq.CountRequested,
CountAccepted: kindBoxReq.CountAccepted,
BenefactorID: kindBoxReq.BenefactorID,
Status: kindBoxReq.Status,
Description: kindBoxReq.Description,
ReferDate: kindBoxReq.ReferDate,
AddressID: kindBoxReq.AddressID,
KindBoxReqID: kindBoxReq.ID,
KindBoxReqStatus: kindBoxReq.Status,
CountRequested: kindBoxReq.CountRequested,
CountAccepted: kindBoxReq.CountAccepted,
ReferDate: kindBoxReq.ReferDate,
AddressID: kindBoxReq.AddressID,
KindBoxID: res.KindBox.ID,
KindBoxType: kindBoxReq.KindBoxType,
BenefactorID: kindBoxReq.BenefactorID,
KindBoxStatus: res.KindBox.Status,
KindBoxSerialNumber: res.KindBox.SerialNumber,
}, nil
}

View File

@ -2,6 +2,7 @@ package adminkindboxreqservice
import (
"context"
adminkindboxservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box"
"git.gocasts.ir/ebhomengo/niki/entity"
)
@ -24,7 +25,8 @@ type Repository interface {
// }
type Service struct {
repo Repository
repo Repository
kindBoxSvc adminkindboxservice.Service
// benefactorService BenefactorService
}