From cc9933f675a315ce5854c04e8a44ba03afb791c6 Mon Sep 17 00:00:00 2001 From: "aditya.siregar" Date: Wed, 28 Aug 2024 00:36:26 +0700 Subject: [PATCH] Remove unused var --- internal/entity/branch.go | 83 ----------------------- internal/handlers/request/branch.go | 36 ---------- internal/repository/branches/branch.go | 91 -------------------------- internal/repository/repository.go | 11 ---- internal/services/branch/branch.go | 89 ------------------------- internal/services/service.go | 15 +---- internal/services/users/users.go | 8 +-- 7 files changed, 5 insertions(+), 328 deletions(-) delete mode 100644 internal/entity/branch.go delete mode 100644 internal/handlers/request/branch.go delete mode 100644 internal/repository/branches/branch.go delete mode 100644 internal/services/branch/branch.go diff --git a/internal/entity/branch.go b/internal/entity/branch.go deleted file mode 100644 index 4a56c3c..0000000 --- a/internal/entity/branch.go +++ /dev/null @@ -1,83 +0,0 @@ -package entity - -import ( - "furtuna-be/internal/constants/branch" - "time" -) - -type Branch struct { - ID int64 - Name string - Status branch.BranchStatus - Location string - CreatedAt time.Time - UpdatedAt time.Time - DeletedAt *time.Time - CreatedBy int64 - UpdatedBy int64 -} - -type BranchSearch struct { - Search string - Name string - Limit int - Offset int -} - -type BranchList []*BranchDB - -type BranchDB struct { - Branch -} - -func (b *Branch) ToBranchDB() *BranchDB { - return &BranchDB{ - Branch: *b, - } -} - -func (BranchDB) TableName() string { - return "branches" -} - -func (e *BranchDB) ToBranch() *Branch { - return &Branch{ - ID: e.ID, - Name: e.Name, - Status: e.Status, - Location: e.Location, - CreatedAt: e.CreatedAt, - UpdatedAt: e.UpdatedAt, - CreatedBy: e.CreatedBy, - } -} - -func (b *BranchList) ToBranchList() []*Branch { - var branches []*Branch - for _, branch := range *b { - branches = append(branches, branch.ToBranch()) - } - return branches -} - -func (o *BranchDB) ToUpdatedBranch(updatedby int64, req Branch) { - o.UpdatedBy = updatedby - - if req.Name != "" { - o.Name = req.Name - } - - if req.Status != "" { - o.Status = req.Status - } - - if req.Location != "" { - o.Location = req.Location - } -} - -func (o *BranchDB) SetDeleted(updatedby int64) { - currentTime := time.Now() - o.DeletedAt = ¤tTime - o.UpdatedBy = updatedby -} diff --git a/internal/handlers/request/branch.go b/internal/handlers/request/branch.go deleted file mode 100644 index 43fb688..0000000 --- a/internal/handlers/request/branch.go +++ /dev/null @@ -1,36 +0,0 @@ -package request - -import ( - "furtuna-be/internal/constants/branch" - "furtuna-be/internal/entity" -) - -type BranchParam struct { - Search string `form:"search" json:"search" example:"Ketua Umum"` - Name string `form:"name" json:"name" example:"Ketua Umum"` - Limit int `form:"limit" json:"limit" example:"10"` - Offset int `form:"offset" json:"offset" example:"0"` -} - -func (p *BranchParam) ToEntity() entity.BranchSearch { - return entity.BranchSearch{ - Search: p.Search, - Name: p.Name, - Limit: p.Limit, - Offset: p.Offset, - } -} - -type Branch struct { - Name string `json:"name" validate:"required"` - Location string `json:"location" validate:"required"` - Status branch.BranchStatus `json:"status"` -} - -func (e *Branch) ToEntity() *entity.Branch { - return &entity.Branch{ - Name: e.Name, - Location: e.Location, - Status: e.Status, - } -} diff --git a/internal/repository/branches/branch.go b/internal/repository/branches/branch.go deleted file mode 100644 index d3064ab..0000000 --- a/internal/repository/branches/branch.go +++ /dev/null @@ -1,91 +0,0 @@ -package branches - -import ( - "context" - "furtuna-be/internal/common/logger" - "furtuna-be/internal/entity" - - "go.uber.org/zap" - "gorm.io/gorm" -) - -type BranchRepository struct { - db *gorm.DB -} - -func NewBranchRepository(db *gorm.DB) *BranchRepository { - return &BranchRepository{ - db: db, - } -} - -func (b *BranchRepository) CreateBranch(ctx context.Context, branch *entity.BranchDB) (*entity.BranchDB, error) { - err := b.db.Create(branch).Error - if err != nil { - logger.ContextLogger(ctx).Error("error when create branch", zap.Error(err)) - return nil, err - } - return branch, nil -} - -func (b *BranchRepository) UpdateBranch(ctx context.Context, branch *entity.BranchDB) (*entity.BranchDB, error) { - if err := b.db.Save(branch).Error; err != nil { - logger.ContextLogger(ctx).Error("error when update branch", zap.Error(err)) - return nil, err - } - return branch, nil -} - -func (b *BranchRepository) GetBranchByID(ctx context.Context, id int64) (*entity.BranchDB, error) { - branch := new(entity.BranchDB) - if err := b.db.First(branch, id).Error; err != nil { - logger.ContextLogger(ctx).Error("error when get by id branch", zap.Error(err)) - return nil, err - } - return branch, nil -} - -func (b *BranchRepository) GetAllBranches(ctx context.Context, req entity.BranchSearch) (entity.BranchList, int, error) { - var branches []*entity.BranchDB - var total int64 - - query := b.db - query = query.Where("deleted_at is null") - - if req.Search != "" { - query = query.Where("name ILIKE ?", "%"+req.Search+"%") - } - - if req.Name != "" { - query = query.Where("name ILIKE ?", "%"+req.Name+"%") - } - - if req.Limit > 0 { - query = query.Limit(req.Limit) - } - - if req.Offset > 0 { - query = query.Offset(req.Offset) - } - - if err := query.Find(&branches).Error; err != nil { - logger.ContextLogger(ctx).Error("error when get all branches", zap.Error(err)) - return nil, 0, err - } - - if err := b.db.Model(&entity.BranchDB{}).Where(query).Count(&total).Error; err != nil { - logger.ContextLogger(ctx).Error("error when count branches", zap.Error(err)) - return nil, 0, err - } - - return branches, int(total), nil -} - -func (b *BranchRepository) DeleteBranch(ctx context.Context, id int64) error { - branch := new(entity.BranchDB) - branch.ID = id - if err := b.db.Delete(branch).Error; err != nil { - return err - } - return nil -} diff --git a/internal/repository/repository.go b/internal/repository/repository.go index e82293e..72f2804 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -4,7 +4,6 @@ import ( "context" "database/sql" "furtuna-be/internal/common/mycontext" - "furtuna-be/internal/repository/branches" "furtuna-be/internal/repository/brevo" "furtuna-be/internal/repository/license" mdtrns "furtuna-be/internal/repository/midtrans" @@ -35,7 +34,6 @@ type RepoManagerImpl struct { Auth Auth Event Event User User - Branch Branch Studio Studio Product Product Order Order @@ -57,7 +55,6 @@ func NewRepoManagerImpl(db *gorm.DB, cfg *config.Config) *RepoManagerImpl { Auth: auth.NewAuthRepository(db), Event: event.NewEventRepo(db), User: users.NewUserRepository(db), - Branch: branches.NewBranchRepository(db), Studio: studios.NewStudioRepository(db), Product: products.NewProductRepository(db), Order: orders.NewOrderRepository(db), @@ -114,14 +111,6 @@ type User interface { CountUsersByRoleAndSiteOrPartner(ctx context.Context, roleID int, siteID *int64) (int, error) } -type Branch interface { - CreateBranch(ctx context.Context, branch *entity.BranchDB) (*entity.BranchDB, error) - UpdateBranch(ctx context.Context, branch *entity.BranchDB) (*entity.BranchDB, error) - GetBranchByID(ctx context.Context, id int64) (*entity.BranchDB, error) - GetAllBranches(ctx context.Context, req entity.BranchSearch) (entity.BranchList, int, error) - DeleteBranch(ctx context.Context, id int64) error -} - type Studio interface { CreateStudio(ctx context.Context, studio *entity.StudioDB) (*entity.StudioDB, error) UpdateStudio(ctx context.Context, studio *entity.StudioDB) (*entity.StudioDB, error) diff --git a/internal/services/branch/branch.go b/internal/services/branch/branch.go deleted file mode 100644 index 9f7183b..0000000 --- a/internal/services/branch/branch.go +++ /dev/null @@ -1,89 +0,0 @@ -package branch - -import ( - "context" - "furtuna-be/internal/common/logger" - "furtuna-be/internal/common/mycontext" - "furtuna-be/internal/entity" - "furtuna-be/internal/repository" - - "go.uber.org/zap" -) - -type BranchService struct { - repo repository.Branch -} - -func NewBranchService(repo repository.Branch) *BranchService { - return &BranchService{ - repo: repo, - } -} - -func (s *BranchService) Create(ctx mycontext.Context, branchReq *entity.Branch) (*entity.Branch, error) { - branchDB := branchReq.ToBranchDB() - branchDB.CreatedBy = ctx.RequestedBy() - - branchDB, err := s.repo.CreateBranch(ctx, branchDB) - if err != nil { - logger.ContextLogger(ctx).Error("error when create branch", zap.Error(err)) - return nil, err - } - - return branchDB.ToBranch(), nil -} - -func (s *BranchService) Update(ctx mycontext.Context, id int64, branchReq *entity.Branch) (*entity.Branch, error) { - existingBranch, err := s.repo.GetBranchByID(ctx, id) - if err != nil { - return nil, err - } - - existingBranch.ToUpdatedBranch(ctx.RequestedBy(), *branchReq) - - updatedBranchDB, err := s.repo.UpdateBranch(ctx, existingBranch.ToBranchDB()) - if err != nil { - logger.ContextLogger(ctx).Error("error when update branch", zap.Error(err)) - return nil, err - } - - return updatedBranchDB.ToBranch(), nil -} - -func (s *BranchService) GetByID(ctx context.Context, id int64) (*entity.Branch, error) { - branchDB, err := s.repo.GetBranchByID(ctx, id) - if err != nil { - logger.ContextLogger(ctx).Error("error when get branch by id", zap.Error(err)) - return nil, err - } - - return branchDB.ToBranch(), nil -} - -func (s *BranchService) GetAll(ctx context.Context, search entity.BranchSearch) ([]*entity.Branch, int, error) { - branches, total, err := s.repo.GetAllBranches(ctx, search) - if err != nil { - logger.ContextLogger(ctx).Error("error when get all branches", zap.Error(err)) - return nil, 0, err - } - - return branches.ToBranchList(), total, nil -} - -func (s *BranchService) Delete(ctx mycontext.Context, id int64) error { - branchDB, err := s.repo.GetBranchByID(ctx, id) - if err != nil { - logger.ContextLogger(ctx).Error("error when get branch by id", zap.Error(err)) - return err - } - - branchDB.SetDeleted(ctx.RequestedBy()) - - _, err = s.repo.UpdateBranch(ctx, branchDB) - if err != nil { - logger.ContextLogger(ctx).Error("error when update branch", zap.Error(err)) - return err - } - - return nil -} diff --git a/internal/services/service.go b/internal/services/service.go index 3bf017b..7d3ce9d 100644 --- a/internal/services/service.go +++ b/internal/services/service.go @@ -4,7 +4,6 @@ import ( "context" "furtuna-be/internal/common/mycontext" "furtuna-be/internal/services/balance" - "furtuna-be/internal/services/branch" "furtuna-be/internal/services/discovery" service "furtuna-be/internal/services/license" "furtuna-be/internal/services/order" @@ -29,7 +28,6 @@ type ServiceManagerImpl struct { AuthSvc Auth EventSvc Event UserSvc User - BranchSvc Branch StudioSvc Studio ProductSvc Product OrderSvc Order @@ -46,14 +44,13 @@ func NewServiceManagerImpl(cfg *config.Config, repo *repository.RepoManagerImpl) return &ServiceManagerImpl{ AuthSvc: auth.New(repo.Auth, repo.Crypto, repo.User, repo.EmailService, cfg.Email, repo.Trx, repo.License), EventSvc: event.NewEventService(repo.Event), - UserSvc: users.NewUserService(repo.User, repo.Branch), - BranchSvc: branch.NewBranchService(repo.Branch), + UserSvc: users.NewUserService(repo.User), StudioSvc: studio.NewStudioService(repo.Studio), ProductSvc: product.NewProductService(repo.Product), OrderSvc: order.NewOrderService(repo.Order, repo.Product, repo.Crypto, repo.Midtrans, repo.Payment, repo.Trx, repo.Wallet, &cfg.Order, repo.Transaction), OSSSvc: oss.NewOSSService(repo.OSS), PartnerSvc: partner.NewPartnerService( - repo.Partner, users.NewUserService(repo.User, repo.Branch), repo.Trx, repo.Wallet, repo.User), + repo.Partner, users.NewUserService(repo.User), repo.Trx, repo.Wallet, repo.User), SiteSvc: site.NewSiteService(repo.Site, repo.User), LicenseSvc: service.NewLicenseService(repo.License), Transaction: transaction.New(repo.Transaction, repo.Wallet, repo.Trx), @@ -87,14 +84,6 @@ type User interface { Delete(ctx mycontext.Context, id int64) error } -type Branch interface { - Create(ctx mycontext.Context, branchReq *entity.Branch) (*entity.Branch, error) - Update(ctx mycontext.Context, id int64, branchReq *entity.Branch) (*entity.Branch, error) - GetByID(ctx context.Context, id int64) (*entity.Branch, error) - GetAll(ctx context.Context, search entity.BranchSearch) ([]*entity.Branch, int, error) - Delete(ctx mycontext.Context, id int64) error -} - type Studio interface { Create(ctx mycontext.Context, studioReq *entity.Studio) (*entity.Studio, error) Update(ctx mycontext.Context, id int64, studioReq *entity.Studio) (*entity.Studio, error) diff --git a/internal/services/users/users.go b/internal/services/users/users.go index a50765b..9baaddf 100644 --- a/internal/services/users/users.go +++ b/internal/services/users/users.go @@ -14,14 +14,12 @@ import ( ) type UserService struct { - repo repository.User - branchRepo repository.Branch + repo repository.User } -func NewUserService(repo repository.User, branchRepo repository.Branch) *UserService { +func NewUserService(repo repository.User) *UserService { return &UserService{ - repo: repo, - branchRepo: branchRepo, + repo: repo, } }