niki/service/benefactor/address/service.go

53 lines
1.6 KiB
Go
Raw Normal View History

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)
}
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
}