Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: interfaced the billing service #386

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions pkg/controller/billing/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ func (base *Controller) CreateBilling(c *gin.Context) {
userClaims := claims.(jwt.MapClaims)
userId := userClaims["user_id"].(string)

respData, err := billing.CreateBilling(billingReq, base.Db.Postgresql.DB(), userId)
billingService := billing.NewBillingService(base.Db.Postgresql.DB())
respData, err := billingService.CreateBilling(billingReq, userId)

if err != nil {
rd := utility.BuildErrorResponse(http.StatusBadRequest, "error", err.Error(), err, nil)
Expand Down Expand Up @@ -84,7 +85,8 @@ func (base *Controller) DeleteBilling(c *gin.Context) {
userClaims := claims.(jwt.MapClaims)
userId := userClaims["user_id"].(string)

if err := billing.DeleteBilling(billingID, userId, base.Db.Postgresql.DB()); err != nil {
billingService := billing.NewBillingService(base.Db.Postgresql.DB())
if err := billingService.DeleteBilling(billingID, userId); err != nil {
rd := utility.BuildErrorResponse(http.StatusNotFound, "error", "billing not found", err.Error(), nil)
c.JSON(http.StatusNotFound, rd)
return
Expand All @@ -97,7 +99,8 @@ func (base *Controller) DeleteBilling(c *gin.Context) {
}

func (base *Controller) GetBillings(c *gin.Context) {
billings_len, paginationResponse, err := billing.GetBillings(base.Db.Postgresql.DB(), c)
billingService := billing.NewBillingService(base.Db.Postgresql.DB())
billings_len, paginationResponse, err := billingService.GetBillings(c)
if err != nil {
rd := utility.BuildErrorResponse(http.StatusNotFound, "error", "failed to fetch billings", err, nil)
c.JSON(http.StatusNotFound, rd)
Expand Down Expand Up @@ -125,7 +128,8 @@ func (base *Controller) GetBillingById(c *gin.Context) {
return
}

billing, err := billing.GetBillingById(billingID, base.Db.Postgresql.DB())
billingService := billing.NewBillingService(base.Db.Postgresql.DB())
billing, err := billingService.GetBillingById(billingID)

if err != nil {
rd := utility.BuildErrorResponse(http.StatusNotFound, "error", "billing not found", err.Error(), nil)
Expand Down Expand Up @@ -167,7 +171,8 @@ func (base *Controller) UpdateBillingById(c *gin.Context) {
}
userId := userID.(string)

billing, err := billing.UpdateBillingById(billingID, userId, req, base.Db.Postgresql.DB())
billingService := billing.NewBillingService(base.Db.Postgresql.DB())
billing, err := billingService.UpdateBillingById(billingID, userId, req)

if err != nil {
rd := utility.BuildErrorResponse(http.StatusNotFound, "error", "billing not found", err.Error(), nil)
Expand Down
96 changes: 45 additions & 51 deletions services/billing/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,43 @@ import (
"github.com/hngprojects/hng_boilerplate_golang_web/utility"
)

func CreateBilling(req models.CreateBillingRequest, db *gorm.DB, userId string) (models.BillingResponse, error) {
// instance of Postgresql db
pdb := inst.InitDB(db)
// BillingService defines the interface for billing operations.
type BillingService interface {
CreateBilling(req models.CreateBillingRequest, userId string) (models.BillingResponse, error)
DeleteBilling(BillingId string, userId string) error
GetBillings(c *gin.Context) (int, database.PaginationResponse, error)
GetBillingById(BillingId string) (models.Billing, error)
UpdateBillingById(BillingId string, userId string, req models.UpdateBillingRequest) (models.Billing, error)
}

// BillingServiceImpl implements BillingService using GORM.
type BillingServiceImpl struct {
db database.DatabaseManager
}

// NewBillingService creates a new instance of BillingService.
func NewBillingService(db *gorm.DB) BillingService {
return &BillingServiceImpl{db: inst.InitDB(db)}
}

// CreateBilling creates a new billing record.
func (s *BillingServiceImpl) CreateBilling(req models.CreateBillingRequest, userId string) (models.BillingResponse, error) {
var (
user models.User
billingResp models.BillingResponse
)

Billing := models.Billing{
ID: utility.GenerateUUID(),
Name: req.Name,
Price: req.Price,
}

err := Billing.Create(pdb)

if err != nil {
if err := Billing.Create(s.db); err != nil {
return billingResp, err
}

user, err = user.GetUserByID(pdb, userId)

user, err := user.GetUserByID(s.db, userId)
if err != nil {
return billingResp, err
}
Expand All @@ -42,84 +58,62 @@ func CreateBilling(req models.CreateBillingRequest, db *gorm.DB, userId string)
Name: Billing.Name,
Price: Billing.Price,
CreatedAt: Billing.CreatedAt,
UpdatedAt: billingResp.UpdatedAt,
UpdatedAt: Billing.UpdatedAt,
}

return response, nil
}

func DeleteBilling(BillingId string, userId string, db *gorm.DB) error {
// instance of Postgresql db
pdb := inst.InitDB(db)

// DeleteBilling deletes a billing record by ID.
func (s *BillingServiceImpl) DeleteBilling(BillingId string, userId string) error {
var Billing models.Billing

Billing, err := Billing.CheckBillingExists(BillingId, pdb)

Billing, err := Billing.CheckBillingExists(BillingId, s.db)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return errors.New("Billing not found")
return errors.New("billing not found")
}
return err
}

return Billing.Delete(pdb)
return Billing.Delete(s.db)
}

func GetBillings(db *gorm.DB, c *gin.Context) (int, database.PaginationResponse, error) {
// instance of Postgresql db
pdb := inst.InitDB(db)

var (
Billing models.Billing
)
Billings, paginationResponse, err := Billing.GetAllBillings(pdb, c)

// GetBillings retrieves paginated billing records.
func (s *BillingServiceImpl) GetBillings(c *gin.Context) (int, database.PaginationResponse, error) {
var Billing models.Billing
Billings, paginationResponse, err := Billing.GetAllBillings(s.db, c)
if err != nil {
return 0, paginationResponse, err
}

total_billings := len(Billings)

return total_billings, paginationResponse, nil
totalBillings := len(Billings)
return totalBillings, paginationResponse, nil
}

func GetBillingById(BillingId string, db *gorm.DB) (models.Billing, error) {
// instance of Postgresql db
pdb := inst.InitDB(db)

var (
resp models.Billing
)

resp, err := resp.GetBillingById(pdb, BillingId)

// GetBillingById fetches a billing record by ID.
func (s *BillingServiceImpl) GetBillingById(BillingId string) (models.Billing, error) {
var resp models.Billing
resp, err := resp.GetBillingById(s.db, BillingId)
if err != nil {
return resp, err
}

return resp, nil
}

func UpdateBillingById(BillingId string, userId string, req models.UpdateBillingRequest, db *gorm.DB) (models.Billing, error) {
// instance of Postgresql db
pdb := inst.InitDB(db)

var (
resp models.Billing
)

resp, err := resp.CheckBillingExists(BillingId, pdb)
// UpdateBillingById updates a billing record by ID.
func (s *BillingServiceImpl) UpdateBillingById(BillingId string, userId string, req models.UpdateBillingRequest) (models.Billing, error) {
var resp models.Billing

resp, err := resp.CheckBillingExists(BillingId, s.db)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp, errors.New("billing not found")
}
return resp, err
}

_, err = resp.UpdateBillingById(pdb, req, BillingId)

_, err = resp.UpdateBillingById(s.db, req, BillingId)
if err != nil {
return resp, err
}
Expand Down
Loading