164 lines
5.4 KiB
Go
164 lines
5.4 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
dbgen "github.com/SamuelTariku/FortuneBet-Backend/gen/db"
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/domain"
|
|
)
|
|
|
|
func convertCreateBranch(branch domain.CreateBranch) dbgen.CreateBranchParams {
|
|
return dbgen.CreateBranchParams{
|
|
Name: branch.Name,
|
|
Location: branch.Location,
|
|
WalletID: branch.WalletID,
|
|
BranchManagerID: branch.BranchManagerID,
|
|
CompanyID: branch.CompanyID,
|
|
IsSelfOwned: branch.IsSelfOwned,
|
|
}
|
|
}
|
|
|
|
func convertDBBranchDetail(dbBranch dbgen.BranchDetail) domain.BranchDetail {
|
|
return domain.BranchDetail{
|
|
ID: dbBranch.ID,
|
|
Name: dbBranch.Name,
|
|
Location: dbBranch.Location,
|
|
WalletID: dbBranch.WalletID,
|
|
BranchManagerID: dbBranch.BranchManagerID,
|
|
CompanyID: dbBranch.CompanyID,
|
|
IsSelfOwned: dbBranch.IsSelfOwned,
|
|
ManagerName: dbBranch.ManagerName.(string),
|
|
ManagerPhoneNumber: dbBranch.ManagerPhoneNumber.String,
|
|
}
|
|
}
|
|
|
|
func convertDBBranch(dbBranch dbgen.Branch) domain.Branch {
|
|
return domain.Branch{
|
|
ID: dbBranch.ID,
|
|
Name: dbBranch.Name,
|
|
Location: dbBranch.Location,
|
|
WalletID: dbBranch.WalletID,
|
|
BranchManagerID: dbBranch.BranchManagerID,
|
|
CompanyID: dbBranch.CompanyID,
|
|
IsSelfOwned: dbBranch.IsSelfOwned,
|
|
}
|
|
}
|
|
|
|
func (s *Store) CreateBranch(ctx context.Context, branch domain.CreateBranch) (domain.Branch, error) {
|
|
|
|
dbBranch, err := s.queries.CreateBranch(ctx, convertCreateBranch(branch))
|
|
|
|
if err != nil {
|
|
return domain.Branch{}, err
|
|
}
|
|
return convertDBBranch(dbBranch), nil
|
|
}
|
|
|
|
func (s *Store) CreateSupportedOperation(ctx context.Context, supportedOperation domain.CreateSupportedOperation) (domain.SupportedOperation, error) {
|
|
dbSupportedOperation, err := s.queries.CreateSupportedOperation(ctx, dbgen.CreateSupportedOperationParams{
|
|
Name: supportedOperation.Name,
|
|
Description: supportedOperation.Description,
|
|
})
|
|
if err != nil {
|
|
return domain.SupportedOperation{}, err
|
|
}
|
|
return domain.SupportedOperation{
|
|
ID: dbSupportedOperation.ID,
|
|
Name: dbSupportedOperation.Name,
|
|
Description: dbSupportedOperation.Description,
|
|
}, nil
|
|
}
|
|
|
|
func (s *Store) CreateBranchOperation(ctx context.Context, branchOperation domain.CreateBranchOperation) error {
|
|
_, err := s.queries.CreateBranchOperation(ctx, dbgen.CreateBranchOperationParams{
|
|
BranchID: branchOperation.BranchID,
|
|
OperationID: branchOperation.OperationID,
|
|
})
|
|
return err
|
|
}
|
|
|
|
func (s *Store) GetBranchByID(ctx context.Context, id int64) (domain.BranchDetail, error) {
|
|
dbBranch, err := s.queries.GetBranchByID(ctx, id)
|
|
if err != nil {
|
|
return domain.BranchDetail{}, err
|
|
}
|
|
return convertDBBranchDetail(dbBranch), nil
|
|
}
|
|
|
|
func (s *Store) GetBranchByManagerID(ctx context.Context, branchManagerID int64) ([]domain.BranchDetail, error) {
|
|
dbBranches, err := s.queries.GetBranchByManagerID(ctx, branchManagerID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var branches []domain.BranchDetail = make([]domain.BranchDetail, 0, len(dbBranches))
|
|
for _, dbBranch := range dbBranches {
|
|
branches = append(branches, convertDBBranchDetail(dbBranch))
|
|
}
|
|
return branches, nil
|
|
}
|
|
func (s *Store) GetBranchByCompanyID(ctx context.Context, companyID int64) ([]domain.BranchDetail, error) {
|
|
dbBranches, err := s.queries.GetBranchByCompanyID(ctx, companyID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var branches []domain.BranchDetail = make([]domain.BranchDetail, 0, len(dbBranches))
|
|
for _, dbBranch := range dbBranches {
|
|
branches = append(branches, convertDBBranchDetail(dbBranch))
|
|
}
|
|
return branches, nil
|
|
}
|
|
|
|
func (s *Store) GetBranchOperations(ctx context.Context, branchID int64) ([]domain.BranchOperation, error) {
|
|
dbBranchOperations, err := s.queries.GetBranchOperations(ctx, branchID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var branchOperations []domain.BranchOperation = make([]domain.BranchOperation, 0, len(dbBranchOperations))
|
|
for _, dbBranchOperation := range dbBranchOperations {
|
|
branchOperations = append(branchOperations, domain.BranchOperation{
|
|
ID: dbBranchOperation.ID,
|
|
OperationName: dbBranchOperation.Name,
|
|
OperationDescription: dbBranchOperation.Description,
|
|
})
|
|
}
|
|
return branchOperations, nil
|
|
}
|
|
|
|
func (s *Store) GetAllBranches(ctx context.Context) ([]domain.BranchDetail, error) {
|
|
dbBranches, err := s.queries.GetAllBranches(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var branches []domain.BranchDetail = make([]domain.BranchDetail, 0, len(dbBranches))
|
|
for _, dbBranch := range dbBranches {
|
|
branches = append(branches, convertDBBranchDetail(dbBranch))
|
|
}
|
|
return branches, nil
|
|
}
|
|
|
|
func (s *Store) UpdateBranch(ctx context.Context, id int64, branch domain.UpdateBranch) (domain.Branch, error) {
|
|
dbBranch, err := s.queries.UpdateBranch(ctx, dbgen.UpdateBranchParams{
|
|
ID: id,
|
|
Name: branch.Name,
|
|
Location: branch.Location,
|
|
BranchManagerID: branch.BranchManagerID,
|
|
IsSelfOwned: branch.IsSelfOwned,
|
|
})
|
|
if err != nil {
|
|
return domain.Branch{}, err
|
|
}
|
|
return convertDBBranch(dbBranch), nil
|
|
}
|
|
|
|
func (s *Store) DeleteBranch(ctx context.Context, id int64) error {
|
|
return s.queries.DeleteBranch(ctx, id)
|
|
}
|
|
|
|
func (s *Store) DeleteBranchOperation(ctx context.Context, branchID int64, operationID int64) error {
|
|
err := s.queries.DeleteBranchOperation(ctx, dbgen.DeleteBranchOperationParams{
|
|
BranchID: branchID,
|
|
OperationID: operationID,
|
|
})
|
|
return err
|
|
}
|