forked from ebhomengo/niki
1
0
Fork 0
niki/service/benefactor/address/service.go

77 lines
2.5 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)
2024-01-17 20:17:06 +00:00
GetAllProvinces(ctx context.Context) ([]entity.Province, error)
2024-01-18 10:16:36 +00:00
GetAllCities(ctx context.Context) ([]entity.City, 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
}
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
}
2024-01-18 10:16:36 +00:00
func (s Service) GetAllCities(ctx context.Context, _ param.GetAllCitiesRequest) (param.GetAllCitiesResponse, error) {
const op = "benefactoraddressservice.GetAllCities"
Cities, err := s.repo.GetAllCities(ctx)
if err != nil {
return param.GetAllCitiesResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
}
return param.GetAllCitiesResponse{Cities: Cities}, nil
}