forked from ebhomengo/niki
add kind box migration and service
This commit is contained in:
parent
1823191c4e
commit
dc9d931227
|
@ -13,6 +13,7 @@ func (h Handler) SetRoutes(e *echo.Echo) {
|
||||||
//r.GET("/", h.GetAll).Name = "admin-getallkindboxreq"
|
//r.GET("/", h.GetAll).Name = "admin-getallkindboxreq"
|
||||||
//nolint:gocritic
|
//nolint:gocritic
|
||||||
//r.PATCH("/:id", h.Update).Name = "admin-updatekindboxreq"
|
//r.PATCH("/:id", h.Update).Name = "admin-updatekindboxreq"
|
||||||
|
// todo - add acl
|
||||||
r.PATCH("/accept-kind-box-req/:id", h.Accept)
|
r.PATCH("/accept-kind-box-req/:id", h.Accept)
|
||||||
r.PATCH("/reject-kind-box-req/:id", h.Reject)
|
r.PATCH("/reject-kind-box-req/:id", h.Reject)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,12 +4,13 @@ import "time"
|
||||||
|
|
||||||
type KindBox struct {
|
type KindBox struct {
|
||||||
ID uint
|
ID uint
|
||||||
KindBoxReqID uint // TODO like database model
|
KindBoxReqID uint
|
||||||
BenefactorID uint // TODO need in business entity
|
BenefactorID uint
|
||||||
|
Type KindBoxType
|
||||||
TotalAmount uint
|
TotalAmount uint
|
||||||
ReceiverID uint
|
|
||||||
SenderID uint
|
|
||||||
SerialNumber string
|
SerialNumber string
|
||||||
Status KindBoxStatus
|
Status KindBoxStatus
|
||||||
|
SenderID uint
|
||||||
|
ReceiverID uint
|
||||||
StatusChangedAt time.Time
|
StatusChangedAt time.Time
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,8 +5,6 @@ import entity "git.gocasts.ir/ebhomengo/niki/entity"
|
||||||
type KindBoxAddRequest struct {
|
type KindBoxAddRequest struct {
|
||||||
BenefactorID uint
|
BenefactorID uint
|
||||||
KindBoxReqID uint
|
KindBoxReqID uint
|
||||||
SenderID uint
|
|
||||||
SerialNumber string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type KindBoxAddResponse struct {
|
type KindBoxAddResponse struct {
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
package adminkindboxreqparam
|
package adminkindboxreqparam
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.gocasts.ir/ebhomengo/niki/entity"
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type KindBoxReqAcceptRequest struct {
|
type KindBoxReqAcceptRequest struct {
|
||||||
|
@ -12,13 +11,16 @@ type KindBoxReqAcceptRequest struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type KindBoxReqAcceptResponse struct {
|
type KindBoxReqAcceptResponse struct {
|
||||||
ID uint `json:"id"`
|
KindBoxReqID uint `json:"kind_box_req_id"`
|
||||||
KindBoxType entity.KindBoxType `json:"kind_box_type"`
|
KindBoxReqStatus entity.KindBoxReqStatus `json:"kind_box_req_status"`
|
||||||
CountRequested uint `json:"count_requested"`
|
CountRequested uint `json:"count_requested"`
|
||||||
CountAccepted uint `json:"count_accepted"`
|
CountAccepted uint `json:"count_accepted"`
|
||||||
BenefactorID uint `json:"benefactor_id"`
|
ReferDate time.Time `json:"refer_date"`
|
||||||
Status entity.KindBoxReqStatus `json:"status"`
|
AddressID uint `json:"address_id"`
|
||||||
Description string `json:"description"`
|
|
||||||
ReferDate time.Time `json:"refer_date"`
|
KindBoxID uint `json:"kind_box_id"`
|
||||||
AddressID uint `json:"address_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"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1,10 @@
|
||||||
package mysqlkindbox
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -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`;
|
|
@ -14,8 +14,6 @@ func (s Service) Add(ctx context.Context, req param.KindBoxAddRequest) (param.Ki
|
||||||
kindBox, err := s.repo.AddKindBox(ctx, entity.KindBox{
|
kindBox, err := s.repo.AddKindBox(ctx, entity.KindBox{
|
||||||
BenefactorID: req.BenefactorID,
|
BenefactorID: req.BenefactorID,
|
||||||
KindBoxReqID: req.KindBoxReqID,
|
KindBoxReqID: req.KindBoxReqID,
|
||||||
SenderID: req.SenderID,
|
|
||||||
SerialNumber: req.SerialNumber,
|
|
||||||
Status: entity.KindBoxPendingSendStatus,
|
Status: entity.KindBoxPendingSendStatus,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -1,20 +1,20 @@
|
||||||
package adminkindboxservice
|
package adminkindboxservice
|
||||||
|
|
||||||
import (
|
//import (
|
||||||
"context"
|
// "context"
|
||||||
|
//
|
||||||
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
|
// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
|
||||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||||
)
|
//)
|
||||||
|
//
|
||||||
func (s Service) Delete(ctx context.Context, req param.KindBoxDeleteRequest) (param.KindBoxDeleteResponse, error) {
|
//func (s Service) Delete(ctx context.Context, req param.KindBoxDeleteRequest) (param.KindBoxDeleteResponse, error) {
|
||||||
// TODO: Does business domain need to delete an kindbox ?
|
// // TODO: Does business domain need to delete an kindbox ?
|
||||||
const op = "adminkindboxservice.Delete"
|
// const op = "adminkindboxservice.Delete"
|
||||||
|
//
|
||||||
dErr := s.repo.DeleteKindBox(ctx, req.KindBoxID)
|
// dErr := s.repo.DeleteKindBox(ctx, req.KindBoxID)
|
||||||
if dErr != nil {
|
// if dErr != nil {
|
||||||
return param.KindBoxDeleteResponse{}, richerror.New(op).WithErr(dErr).WithKind(richerror.KindUnexpected)
|
// return param.KindBoxDeleteResponse{}, richerror.New(op).WithErr(dErr).WithKind(richerror.KindUnexpected)
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
return param.KindBoxDeleteResponse{}, nil
|
// return param.KindBoxDeleteResponse{}, nil
|
||||||
}
|
//}
|
||||||
|
|
|
@ -1,19 +1,19 @@
|
||||||
package adminkindboxservice
|
package adminkindboxservice
|
||||||
|
|
||||||
import (
|
//import (
|
||||||
"context"
|
// "context"
|
||||||
|
//
|
||||||
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
|
// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
|
||||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||||
)
|
//)
|
||||||
|
//
|
||||||
func (s Service) Get(ctx context.Context, req param.KindBoxGetRequest) (param.KindBoxGetResponse, error) {
|
//func (s Service) Get(ctx context.Context, req param.KindBoxGetRequest) (param.KindBoxGetResponse, error) {
|
||||||
const op = "adminkindboxservice.Get"
|
// const op = "adminkindboxservice.Get"
|
||||||
|
//
|
||||||
kindBox, err := s.repo.GetKindBox(ctx, req.KindBoxID)
|
// kindBox, err := s.repo.GetKindBox(ctx, req.KindBoxID)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return param.KindBoxGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
|
// return param.KindBoxGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
return param.KindBoxGetResponse{KindBox: kindBox}, nil
|
// return param.KindBoxGetResponse{KindBox: kindBox}, nil
|
||||||
}
|
//}
|
||||||
|
|
|
@ -1,18 +1,18 @@
|
||||||
package adminkindboxservice
|
package adminkindboxservice
|
||||||
|
|
||||||
import (
|
//import (
|
||||||
"context"
|
// "context"
|
||||||
|
//
|
||||||
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
|
// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
|
||||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||||
)
|
//)
|
||||||
|
//
|
||||||
func (s Service) GetAll(ctx context.Context, _ param.KindBoxGetAllRequest) (param.KindBoxGetAllResponse, error) {
|
//func (s Service) GetAll(ctx context.Context, _ param.KindBoxGetAllRequest) (param.KindBoxGetAllResponse, error) {
|
||||||
const op = "adminkindboxservice.GetAll"
|
// const op = "adminkindboxservice.GetAll"
|
||||||
allKindBox, err := s.repo.GetAllKindBox(ctx)
|
// allKindBox, err := s.repo.GetAllKindBox(ctx)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return param.KindBoxGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
|
// return param.KindBoxGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
return param.KindBoxGetAllResponse{AllKindBox: allKindBox}, nil
|
// return param.KindBoxGetAllResponse{AllKindBox: allKindBox}, nil
|
||||||
}
|
//}
|
||||||
|
|
|
@ -8,10 +8,10 @@ import (
|
||||||
|
|
||||||
type Repository interface {
|
type Repository interface {
|
||||||
AddKindBox(ctx context.Context, kindBox entity.KindBox) (entity.KindBox, error)
|
AddKindBox(ctx context.Context, kindBox entity.KindBox) (entity.KindBox, error)
|
||||||
UpdateKindBox(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
|
//DeleteKindBox(ctx context.Context, kindBoxID uint) error
|
||||||
GetAllKindBox(ctx context.Context) ([]entity.KindBox, error)
|
//GetAllKindBox(ctx context.Context) ([]entity.KindBox, error)
|
||||||
GetKindBox(ctx context.Context, kindBox uint) (entity.KindBox, error)
|
//GetKindBox(ctx context.Context, kindBox uint) (entity.KindBox, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
|
|
|
@ -1,29 +1,29 @@
|
||||||
package adminkindboxservice
|
package adminkindboxservice
|
||||||
|
|
||||||
import (
|
//import (
|
||||||
"context"
|
// "context"
|
||||||
|
//
|
||||||
entity "git.gocasts.ir/ebhomengo/niki/entity"
|
// entity "git.gocasts.ir/ebhomengo/niki/entity"
|
||||||
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
|
// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
|
||||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||||
)
|
//)
|
||||||
|
//
|
||||||
func (s Service) Update(ctx context.Context, req param.KindBoxUpdateRequest) (param.KindBoxUpdateResponse, error) {
|
//func (s Service) Update(ctx context.Context, req param.KindBoxUpdateRequest) (param.KindBoxUpdateResponse, error) {
|
||||||
// TODO: can benefactor update its Request ?
|
// // TODO: can benefactor update its Request ?
|
||||||
// TODO: Is Update Mothod Service Responsible to check which kindboxreqID belongs to benefactorID ?
|
// // 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
|
// // TODO: updating data(s) may have side-effect on other entities by masood-keshvary accepted -> rejected
|
||||||
const op = "adminkindboxservice.Update"
|
// const op = "adminkindboxservice.Update"
|
||||||
|
//
|
||||||
kindBox, uErr := s.repo.UpdateKindBox(ctx, req.KindBoxID, entity.KindBox{
|
// kindBox, uErr := s.repo.UpdateKindBox(ctx, req.KindBoxID, entity.KindBox{
|
||||||
TotalAmount: req.TotalAmount,
|
// TotalAmount: req.TotalAmount,
|
||||||
ReceiverID: req.ReceiverID,
|
// ReceiverID: req.ReceiverID,
|
||||||
SenderID: req.SenderID,
|
// SenderID: req.SenderID,
|
||||||
SerialNumber: req.SerialNumber,
|
// SerialNumber: req.SerialNumber,
|
||||||
Status: req.Status,
|
// Status: req.Status,
|
||||||
})
|
// })
|
||||||
if uErr != nil {
|
// if uErr != nil {
|
||||||
return param.KindBoxUpdateResponse{}, richerror.New(op).WithErr(uErr).WithKind(richerror.KindUnexpected)
|
// return param.KindBoxUpdateResponse{}, richerror.New(op).WithErr(uErr).WithKind(richerror.KindUnexpected)
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
return param.KindBoxUpdateResponse{KindBox: kindBox}, nil
|
// return param.KindBoxUpdateResponse{KindBox: kindBox}, nil
|
||||||
}
|
//}
|
||||||
|
|
|
@ -2,7 +2,7 @@ package adminkindboxreqservice
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
adminkindboxparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
|
||||||
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
|
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
|
||||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
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.
|
// fire new event to create a kind-box.
|
||||||
// get kind box req
|
|
||||||
|
|
||||||
kindBoxReq, gErr := s.repo.GetByID(ctx, req.ID)
|
kindBoxReq, gErr := s.repo.GetByID(ctx, req.ID)
|
||||||
if gErr != nil {
|
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{
|
return param.KindBoxReqAcceptResponse{
|
||||||
ID: kindBoxReq.ID,
|
KindBoxReqID: kindBoxReq.ID,
|
||||||
KindBoxType: kindBoxReq.KindBoxType,
|
KindBoxReqStatus: kindBoxReq.Status,
|
||||||
CountRequested: kindBoxReq.CountRequested,
|
CountRequested: kindBoxReq.CountRequested,
|
||||||
CountAccepted: kindBoxReq.CountAccepted,
|
CountAccepted: kindBoxReq.CountAccepted,
|
||||||
BenefactorID: kindBoxReq.BenefactorID,
|
ReferDate: kindBoxReq.ReferDate,
|
||||||
Status: kindBoxReq.Status,
|
AddressID: kindBoxReq.AddressID,
|
||||||
Description: kindBoxReq.Description,
|
KindBoxID: res.KindBox.ID,
|
||||||
ReferDate: kindBoxReq.ReferDate,
|
KindBoxType: kindBoxReq.KindBoxType,
|
||||||
AddressID: kindBoxReq.AddressID,
|
BenefactorID: kindBoxReq.BenefactorID,
|
||||||
|
KindBoxStatus: res.KindBox.Status,
|
||||||
|
KindBoxSerialNumber: res.KindBox.SerialNumber,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package adminkindboxreqservice
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
adminkindboxservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box"
|
||||||
|
|
||||||
"git.gocasts.ir/ebhomengo/niki/entity"
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
||||||
)
|
)
|
||||||
|
@ -24,7 +25,8 @@ type Repository interface {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
repo Repository
|
repo Repository
|
||||||
|
kindBoxSvc adminkindboxservice.Service
|
||||||
// benefactorService BenefactorService
|
// benefactorService BenefactorService
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue