Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion pkg/controller/category/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ type Controller struct {
Validator *validator.Validate
Logger *utility.Logger
ExtReq request.ExternalRequest
CategoryService category.CategoryService
}

func (base *Controller) GetCategoryNames(c *gin.Context) {
categories, code, err := category.GetCategoryNames(base.Db.Postgresql.DB(), c)

categories, code, err := base.CategoryService.GetCategoryNames(c)
if err != nil {
rd := utility.BuildErrorResponse(http.StatusBadRequest, "error", err.Error(), err, nil)
c.JSON(http.StatusBadRequest, rd)
Expand Down
4 changes: 3 additions & 1 deletion pkg/router/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (
"github.com/hngprojects/hng_boilerplate_golang_web/pkg/controller/category"
"github.com/hngprojects/hng_boilerplate_golang_web/pkg/middleware"
"github.com/hngprojects/hng_boilerplate_golang_web/pkg/repository/storage"
categoryServices "github.com/hngprojects/hng_boilerplate_golang_web/services/category"
"github.com/hngprojects/hng_boilerplate_golang_web/utility"
)

func Category(r *gin.Engine, ApiVersion string, validator *validator.Validate, db *storage.Database, logger *utility.Logger) *gin.Engine {
extReq := request.ExternalRequest{Logger: logger, Test: false}
category := category.Controller{Db: db, Validator: validator, Logger: logger, ExtReq: extReq}
categoryService := categoryServices.NewCategoryService(db.Postgresql.DB())
category := category.Controller{Db: db, Validator: validator, Logger: logger, ExtReq: extReq, CategoryService: categoryService}

categoryUrl := r.Group(fmt.Sprintf("%v", ApiVersion), middleware.Authorize(db.Postgresql.DB()))
{
Expand Down
22 changes: 19 additions & 3 deletions services/category/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,26 @@ type PaginatedResponse struct {
PageSize int `json:"pageSize"`
}

func GetCategoryNames(db *gorm.DB, ctx *gin.Context) (*PaginatedResponse, int, error) {
// CategoryService defines the interface for category operations
type CategoryService interface {
GetCategoryNames(ctx *gin.Context) (*PaginatedResponse, int, error)
}

// categoryService implements CategoryService
type categoryService struct {
db *gorm.DB
}

// NewCategoryService initializes a new CategoryService instance
func NewCategoryService(db *gorm.DB) CategoryService {
return &categoryService{db: db}
}

// GetCategoryNames fetches paginated category names
func (s *categoryService) GetCategoryNames(ctx *gin.Context) (*PaginatedResponse, int, error) {
ownerID, _ := middleware.GetIdFromToken(ctx)
if ownerID == "" {
return nil, http.StatusUnauthorized, errors.New("Unauthorized access")
return nil, http.StatusUnauthorized, errors.New("unauthorized access")
}

page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
Expand All @@ -40,7 +56,7 @@ func GetCategoryNames(db *gorm.DB, ctx *gin.Context) (*PaginatedResponse, int, e
var categories []Category
var totalCount int64

tx := db.Begin()
tx := s.db.Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
Expand Down
4 changes: 3 additions & 1 deletion tests/test_categories/category_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/hngprojects/hng_boilerplate_golang_web/pkg/controller/category"
"github.com/hngprojects/hng_boilerplate_golang_web/pkg/middleware"
"github.com/hngprojects/hng_boilerplate_golang_web/pkg/repository/storage"
categoryServices "github.com/hngprojects/hng_boilerplate_golang_web/services/category"
tst "github.com/hngprojects/hng_boilerplate_golang_web/tests"
"github.com/hngprojects/hng_boilerplate_golang_web/utility"
)
Expand Down Expand Up @@ -45,7 +46,8 @@ func TestGetCategoryNames(t *testing.T) {
tst.SignupUser(t, r, auth, userSignUpData, false)

token := tst.GetLoginToken(t, r, auth, loginData)
category := category.Controller{Db: db, Validator: validatorRef, Logger: logger}
categoryService := categoryServices.NewCategoryService(db.Postgresql.DB())
category := category.Controller{Db: db, Validator: validatorRef, Logger: logger, CategoryService: categoryService}

r = gin.Default()

Expand Down
Loading