2024-01-16 16:13:06 +00:00
|
|
|
package benefactoraddressservice
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
|
|
|
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/address"
|
|
|
|
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Repository interface {
|
|
|
|
CreateBenefactorAddress(ctx context.Context, address entity.Address) (entity.Address, error)
|
|
|
|
GetAddressByID(ctx context.Context, id uint) (*entity.Address, error)
|
2024-01-17 20:17:06 +00:00
|
|
|
GetAllProvinces(ctx context.Context) ([]entity.Province, error)
|
2024-01-16 16:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Service struct {
|
|
|
|
repo Repository
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(repo Repository) Service {
|
|
|
|
return Service{repo: repo}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Service) Add(ctx context.Context, req param.BenefactorAddAddressRequest) (param.BenefactorAddAddressResponse, error) {
|
|
|
|
const op = "benefactoraddressservice.Add"
|
|
|
|
|
|
|
|
address, err := s.repo.CreateBenefactorAddress(ctx, entity.Address{
|
|
|
|
PostalCode: req.PostalCode,
|
|
|
|
Address: req.Address,
|
|
|
|
Lat: req.Lat,
|
|
|
|
Lon: req.Lon,
|
|
|
|
CityID: req.CityID,
|
|
|
|
ProvinceID: req.ProvinceID,
|
|
|
|
BenefactorID: req.BenefactorID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return param.BenefactorAddAddressResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
return param.BenefactorAddAddressResponse{Address: address}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Service) AddressExistByID(ctx context.Context, req param.GetAddressByIDRequest) (param.GetAddressByIDResponse, error) {
|
|
|
|
const op = "benefactorservice.BenefactorExistByID"
|
|
|
|
|
|
|
|
address, err := s.repo.GetAddressByID(ctx, req.ID)
|
|
|
|
if err != nil {
|
|
|
|
return param.GetAddressByIDResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
return param.GetAddressByIDResponse{Address: address}, nil
|
|
|
|
}
|
2024-01-17 20:17:06 +00:00
|
|
|
|
|
|
|
func (s Service) GetAllProvinces(ctx context.Context, _ param.GetAllProvincesRequest) (param.GetAllProvincesResponse, error) {
|
|
|
|
const op = "benefactoraddressservice.GetAllProvinces"
|
|
|
|
|
|
|
|
provinces, err := s.repo.GetAllProvinces(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return param.GetAllProvincesResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
return param.GetAllProvincesResponse{Provinces: provinces}, nil
|
|
|
|
}
|