259 lines
7.7 KiB
Go
259 lines
7.7 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
dbgen "github.com/SamuelTariku/FortuneBet-Backend/gen/db"
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/domain"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
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 convertUpdateBranch(updateBranch domain.UpdateBranch) dbgen.UpdateBranchParams {
|
|
|
|
var newUpdateBranch dbgen.UpdateBranchParams
|
|
|
|
newUpdateBranch.ID = updateBranch.ID
|
|
|
|
if updateBranch.Name != nil {
|
|
newUpdateBranch.Name = pgtype.Text{
|
|
String: *updateBranch.Name,
|
|
Valid: true,
|
|
}
|
|
}
|
|
if updateBranch.Location != nil {
|
|
newUpdateBranch.Location = pgtype.Text{
|
|
String: *updateBranch.Location,
|
|
Valid: true,
|
|
}
|
|
}
|
|
if updateBranch.BranchManagerID != nil {
|
|
newUpdateBranch.BranchManagerID = pgtype.Int8{
|
|
Int64: *updateBranch.BranchManagerID,
|
|
Valid: true,
|
|
}
|
|
}
|
|
if updateBranch.CompanyID != nil {
|
|
newUpdateBranch.CompanyID = pgtype.Int8{
|
|
Int64: *updateBranch.CompanyID,
|
|
Valid: true,
|
|
}
|
|
}
|
|
if updateBranch.IsSelfOwned != nil {
|
|
newUpdateBranch.IsSelfOwned = pgtype.Bool{
|
|
Bool: *updateBranch.IsSelfOwned,
|
|
Valid: true,
|
|
}
|
|
}
|
|
|
|
return newUpdateBranch
|
|
}
|
|
|
|
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) 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) 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) SearchBranchByName(ctx context.Context, name string) ([]domain.BranchDetail, error) {
|
|
dbBranches, err := s.queries.SearchBranchByName(ctx, pgtype.Text{String: name, Valid: true})
|
|
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, branch domain.UpdateBranch) (domain.Branch, error) {
|
|
|
|
dbBranch, err := s.queries.UpdateBranch(ctx, convertUpdateBranch(branch))
|
|
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)
|
|
}
|
|
|
|
// Branch Operations
|
|
|
|
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) 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) CreateBranchCashier(ctx context.Context, branchID int64, userID int64) error {
|
|
_, err := s.queries.CreateBranchCashier(ctx, dbgen.CreateBranchCashierParams{
|
|
UserID: userID,
|
|
BranchID: branchID,
|
|
})
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Store) GetAllSupportedOperations(ctx context.Context) ([]domain.SupportedOperation, error) {
|
|
dbOperations, err := s.queries.GetAllSupportedOperations(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var operations []domain.SupportedOperation = make([]domain.SupportedOperation, 0, len(dbOperations))
|
|
for _, dbOperation := range dbOperations {
|
|
operations = append(operations, domain.SupportedOperation{
|
|
ID: dbOperation.ID,
|
|
Name: dbOperation.Name,
|
|
Description: dbOperation.Description,
|
|
})
|
|
}
|
|
return operations, 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) GetBranchByCashier(ctx context.Context, userID int64) (domain.Branch, error) {
|
|
branch, err := s.queries.GetBranchByCashier(ctx, userID)
|
|
if err != nil {
|
|
return domain.Branch{}, err
|
|
}
|
|
|
|
return convertDBBranch(branch), err
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func (s *Store) DeleteBranchCashier(ctx context.Context, userID int64) error {
|
|
return s.queries.DeleteBranchCashier(ctx, userID)
|
|
|
|
}
|