Skip to content

Commit

Permalink
ref: return pointer instead returning value
Browse files Browse the repository at this point in the history
  • Loading branch information
abbasfisal committed Dec 21, 2024
1 parent 6804e95 commit 2842d71
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 71 deletions.
6 changes: 3 additions & 3 deletions internal/modules/admin/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func (a *AdminHandler) StoreCategory(c *gin.Context) {
// end upload and save image
//---------------------------

newCategory, err := a.categorySrv.Create(context.TODO(), req)
newCategory, err := a.categorySrv.Create(context.TODO(), &req)
if err != nil || newCategory.ID <= 0 {
_ = os.Remove(pathToUpload)
fmt.Println("error in creating category : ", err)
Expand Down Expand Up @@ -428,8 +428,8 @@ func (a *AdminHandler) UpdateCategory(c *gin.Context) {
// end upload and save image
//---------------------------

categoryUpdateErr := a.categorySrv.Edit(c, catID, req)
fmt.Println("----- hanlder category edit : error ", categoryUpdateErr)
categoryUpdateErr := a.categorySrv.Edit(c, catID, &req)
fmt.Println("----- handler category edit : error ", categoryUpdateErr)
if categoryUpdateErr.Code > 0 {
fmt.Println("------- update category err : ", categoryUpdateErr.Error())
if categoryUpdateErr.Code == 404 {
Expand Down
30 changes: 15 additions & 15 deletions internal/modules/admin/repositories/category/category_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,46 @@ type CategoryRepository struct {
db *gorm.DB
}

func NewCategoryRepository(db *gorm.DB) CategoryRepository {
return CategoryRepository{db: db}
func NewCategoryRepository(db *gorm.DB) CategoryRepositoryInterface {
return &CategoryRepository{db: db}
}

func (cr CategoryRepository) GetAll(ctx context.Context) ([]entities.Category, error) {
var categories []entities.Category
func (cr *CategoryRepository) GetAll(ctx context.Context) ([]*entities.Category, error) {
var categories []*entities.Category
err := cr.db.WithContext(ctx).Find(&categories).Error
return categories, err
}

func (cr CategoryRepository) GetAllParent(ctx context.Context) ([]entities.Category, error) {
var categories []entities.Category
func (cr *CategoryRepository) GetAllParent(ctx context.Context) ([]*entities.Category, error) {
var categories []*entities.Category
err := cr.db.WithContext(ctx).Where("parent_id IS NULL").Find(&categories).Error
return categories, err
}

func (cr CategoryRepository) SelectBy(ctx context.Context, categoryID int) (entities.Category, error) {
func (cr *CategoryRepository) SelectBy(ctx context.Context, categoryID int) (*entities.Category, error) {
var category entities.Category
err := cr.db.WithContext(ctx).First(&category, "id = ?", categoryID).Error
return category, err
return &category, err
}

func (cr CategoryRepository) FindBy(ctx context.Context, columnName string, value any) (entities.Category, error) {
func (cr *CategoryRepository) FindBy(ctx context.Context, columnName string, value any) (*entities.Category, error) {
var category entities.Category
condition := fmt.Sprintf("%s = ?", columnName)
err := cr.db.WithContext(ctx).First(&category, condition, value).Error
return category, err
return &category, err
}

func (cr CategoryRepository) Store(ctx context.Context, category entities.Category) (entities.Category, error) {
func (cr *CategoryRepository) Store(ctx context.Context, category *entities.Category) (*entities.Category, error) {
err := cr.db.WithContext(ctx).Create(&category).Error
return category, err
}

func (cr CategoryRepository) Update(c *gin.Context, categoryID int, req requests.UpdateCategoryRequest) (entities.Category, error) {

func (cr *CategoryRepository) Update(c *gin.Context, categoryID int, req *requests.UpdateCategoryRequest) (*entities.Category, error) {
var category entities.Category

err := cr.db.WithContext(c).First(&category, categoryID).Error
if err != nil {
return category, err
return &category, err
}

err = cr.db.WithContext(c).Model(&category).
Expand Down Expand Up @@ -86,5 +86,5 @@ func (cr CategoryRepository) Update(c *gin.Context, categoryID int, req requests
return true
}()).Error

return category, err
return &category, err
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
)

type CategoryRepositoryInterface interface {
GetAll(ctx context.Context) ([]entities.Category, error)
GetAllParent(ctx context.Context) ([]entities.Category, error)
SelectBy(ctx context.Context, categoryID int) (entities.Category, error)
FindBy(ctx context.Context, columnName string, value any) (entities.Category, error)
Store(ctx context.Context, cat entities.Category) (entities.Category, error)
Update(c *gin.Context, categoryID int, req requests.UpdateCategoryRequest) (entities.Category, error)
GetAll(ctx context.Context) ([]*entities.Category, error)
GetAllParent(ctx context.Context) ([]*entities.Category, error)
SelectBy(ctx context.Context, categoryID int) (*entities.Category, error)
FindBy(ctx context.Context, columnName string, value any) (*entities.Category, error)
Store(ctx context.Context, cat *entities.Category) (*entities.Category, error)
Update(c *gin.Context, categoryID int, req *requests.UpdateCategoryRequest) (*entities.Category, error)
}
19 changes: 13 additions & 6 deletions internal/modules/admin/responses/category_resposne.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ type Categories struct {
Data []Category
}

func ToCategory(category entities.Category) Category {
return Category{
func ToCategory(category *entities.Category) *Category {
return &Category{
ID: category.ID,
ParentID: category.ParentID,
Priority: category.Priority,
Expand All @@ -27,12 +27,19 @@ func ToCategory(category entities.Category) Category {
}
}

func ToCategories(categories []entities.Category) Categories {
var response Categories
func ToCategories(categories []*entities.Category) *Categories {

if categories == nil {
return nil
}

response := Categories{
Data: make([]Category, 0, len(categories)),
}

for _, cat := range categories {
response.Data = append(response.Data, ToCategory(cat))
response.Data = append(response.Data, *ToCategory(cat))
}

return response
return &response
}
4 changes: 2 additions & 2 deletions internal/modules/admin/responses/product_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Product struct {
Discount uint

//relation
Category Category
Category *Category
Brand Brand
Images ImageProducts
ProductAttributes ProductAttributes
Expand Down Expand Up @@ -61,7 +61,7 @@ func ToProduct(p entities.Product) Product {
}(),

//relation
Category: ToCategory(p.Category),
Category: ToCategory(&p.Category),
Brand: ToBrand(p.Brand),
Images: ToImageProducts(p.ProductImages),
ProductAttributes: ToProductAttributes(p.ProductAttributes),
Expand Down
31 changes: 16 additions & 15 deletions internal/modules/admin/services/category/category_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,39 @@ type CategoryService struct {
repo category.CategoryRepositoryInterface
}

func NewCategoryService(categoryRepo category.CategoryRepositoryInterface) CategoryService {
return CategoryService{
func NewCategoryService(categoryRepo category.CategoryRepositoryInterface) CategoryServiceInterface {
return &CategoryService{
repo: categoryRepo,
}
}
func (cs CategoryService) Index(ctx context.Context) (responses.Categories, custom_error.CustomError) {

func (cs *CategoryService) Index(ctx context.Context) (*responses.Categories, custom_error.CustomError) {

categories, err := cs.repo.GetAll(ctx)
if err != nil {
return responses.Categories{}, custom_error.HandleError(err, custom_error.RecordNotFound)
return nil, custom_error.HandleError(err, custom_error.RecordNotFound)
}
return responses.ToCategories(categories), custom_error.CustomError{}
}

func (cs CategoryService) Show(ctx context.Context, categoryID int) (responses.Category, custom_error.CustomError) {
func (cs *CategoryService) Show(ctx context.Context, categoryID int) (*responses.Category, custom_error.CustomError) {

cat, err := cs.repo.SelectBy(ctx, categoryID)
if err != nil {
return responses.Category{}, custom_error.HandleError(err, custom_error.RecordNotFound)
return nil, custom_error.HandleError(err, custom_error.RecordNotFound)
}
return responses.ToCategory(cat), custom_error.CustomError{}
}

func (cs CategoryService) CheckSlugUniqueness(ctx context.Context, slug string) bool {
func (cs *CategoryService) CheckSlugUniqueness(ctx context.Context, slug string) bool {
cat, _ := cs.repo.FindBy(ctx, "slug", slug)
if cat.ID > 0 {
return true
}
return false
}

func (cs CategoryService) Create(ctx context.Context, req requests.CreateCategoryRequest) (responses.Category, error) {
func (cs *CategoryService) Create(ctx context.Context, req *requests.CreateCategoryRequest) (*responses.Category, error) {

var cat = entities.Category{
Title: req.Title,
Expand All @@ -65,32 +66,32 @@ func (cs CategoryService) Create(ctx context.Context, req requests.CreateCategor
if *req.Priority != 0 {
cat.Priority = req.Priority
}
newCategory, err := cs.repo.Store(ctx, cat)
newCategory, err := cs.repo.Store(ctx, &cat)
if err != nil {
return responses.Category{}, err
return nil, err
}
return responses.ToCategory(newCategory), nil
}

func (cs CategoryService) GetAllCategories(ctx context.Context) (responses.Categories, custom_error.CustomError) {
func (cs *CategoryService) GetAllCategories(ctx context.Context) (*responses.Categories, custom_error.CustomError) {

categories, err := cs.repo.GetAll(ctx)
if err != nil {
return responses.Categories{}, custom_error.HandleError(err, custom_error.RecordNotFound)
return nil, custom_error.HandleError(err, custom_error.RecordNotFound)
}
return responses.ToCategories(categories), custom_error.CustomError{}
}

func (cs CategoryService) GetAllParentCategory(ctx context.Context) (responses.Categories, custom_error.CustomError) {
func (cs *CategoryService) GetAllParentCategory(ctx context.Context) (*responses.Categories, custom_error.CustomError) {

categories, err := cs.repo.GetAllParent(ctx)
if err != nil {
return responses.Categories{}, custom_error.HandleError(err, custom_error.RecordNotFound)
return nil, custom_error.HandleError(err, custom_error.RecordNotFound)
}
return responses.ToCategories(categories), custom_error.CustomError{}
}

func (cs CategoryService) Edit(c *gin.Context, categoryID int, req requests.UpdateCategoryRequest) custom_error.CustomError {
func (cs *CategoryService) Edit(c *gin.Context, categoryID int, req *requests.UpdateCategoryRequest) custom_error.CustomError {
_, err := cs.repo.Update(c, categoryID, req)

if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
)

type CategoryServiceInterface interface {
Index(ctx context.Context) (responses.Categories, custom_error.CustomError)
GetAllCategories(ctx context.Context) (responses.Categories, custom_error.CustomError)
GetAllParentCategory(ctx context.Context) (responses.Categories, custom_error.CustomError)
Show(ctx context.Context, categoryID int) (responses.Category, custom_error.CustomError)
Index(ctx context.Context) (*responses.Categories, custom_error.CustomError)
GetAllCategories(ctx context.Context) (*responses.Categories, custom_error.CustomError)
GetAllParentCategory(ctx context.Context) (*responses.Categories, custom_error.CustomError)
Show(ctx context.Context, categoryID int) (*responses.Category, custom_error.CustomError)
CheckSlugUniqueness(ctx context.Context, slug string) bool
Create(ctx context.Context, req requests.CreateCategoryRequest) (responses.Category, error)
Edit(c *gin.Context, categoryID int, req requests.UpdateCategoryRequest) custom_error.CustomError
Create(ctx context.Context, req *requests.CreateCategoryRequest) (*responses.Category, error)
Edit(c *gin.Context, categoryID int, req *requests.UpdateCategoryRequest) custom_error.CustomError
}
12 changes: 6 additions & 6 deletions internal/modules/public/repositories/home/home_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func (h HomeRepository) GetLatestProducts(ctx context.Context, limit int) ([]ent
return products, err
}

func (h HomeRepository) GetCategories(ctx context.Context, limit int) ([]entities.Category, error) {
var categories []entities.Category
err := h.db.Limit(limit).Find(&categories, "status=?", true).Error
func (h HomeRepository) GetCategories(ctx context.Context, limit int) ([]*entities.Category, error) {
var categories []*entities.Category
err := h.db.WithContext(ctx).Limit(limit).Find(&categories, "status=?", true).Error

return categories, err
}
Expand Down Expand Up @@ -133,11 +133,11 @@ func (h HomeRepository) GetProductsBy(ctx context.Context, columnName string, va
return products, err
}

func (h HomeRepository) GetCategoryBy(ctx context.Context, columnName string, value any) (entities.Category, error) {
func (h HomeRepository) GetCategoryBy(ctx context.Context, columnName string, value any) (*entities.Category, error) {
var category entities.Category
err := h.db.Where(fmt.Sprintf("%s = ?", columnName), value).Find(&category).Error
err := h.db.WithContext(ctx).Where(fmt.Sprintf("%s = ?", columnName), value).Find(&category).Error

return category, err
return &category, err
}

func (h HomeRepository) NewOtp(ctx context.Context, mobile string) (entities.OTP, custom_error.CustomError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
type HomeRepositoryInterface interface {
GetRandomProducts(ctx context.Context, limit int) ([]entities.Product, error)
GetLatestProducts(ctx context.Context, limit int) ([]entities.Product, error)
GetCategories(ctx context.Context, limit int) ([]entities.Category, error)
GetCategories(ctx context.Context, limit int) ([]*entities.Category, error)
GetProduct(c *gin.Context, productSku, productSlug string) (map[string]interface{}, error)
GetProductsBy(ctx context.Context, columnName string, value any) ([]entities.Product, error)
GetCategoryBy(ctx context.Context, columnName string, value any) (entities.Category, error)
GetCategoryBy(ctx context.Context, columnName string, value any) (*entities.Category, error)
NewOtp(ctx context.Context, mobile string) (entities.OTP, custom_error.CustomError)
VerifyOtp(c *gin.Context, mobile string, req requests.CustomerVerifyRequest) (entities.OTP, error)
ProcessCustomerAuthenticate(c *gin.Context, mobile string) (entities.Session, error)
Expand Down
8 changes: 4 additions & 4 deletions internal/modules/public/services/home/home_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ func (h HomeService) GetProducts(ctx context.Context, limit int) (responses.Prod
return responses.ToProducts(products), custom_error.CustomError{}
}

func (h HomeService) GetCategories(ctx context.Context, limit int) (responses.Categories, custom_error.CustomError) {
func (h HomeService) GetCategories(ctx context.Context, limit int) (*responses.Categories, custom_error.CustomError) {

categories, err := h.repo.GetCategories(ctx, limit)
if err != nil {
return responses.Categories{}, custom_error.HandleError(err, custom_error.RecordNotFound)
return nil, custom_error.HandleError(err, custom_error.RecordNotFound)
}
return responses.ToCategories(categories), custom_error.CustomError{}
}
Expand All @@ -66,11 +66,11 @@ func (h HomeService) ListProductByCategorySlug(c *gin.Context, slug string) (pag

}

func (h HomeService) ShowCategory(ctx context.Context, columnName string, value any) (responses.Category, custom_error.CustomError) {
func (h HomeService) ShowCategory(ctx context.Context, columnName string, value any) (*responses.Category, custom_error.CustomError) {

category, err := h.repo.GetCategoryBy(ctx, columnName, value)
if err != nil {
return responses.Category{}, custom_error.HandleError(err, custom_error.RecordNotFound)
return nil, custom_error.HandleError(err, custom_error.RecordNotFound)
}
return responses.ToCategory(category), custom_error.CustomError{}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,14 @@ import (

type HomeServiceInterface interface {
GetProducts(ctx context.Context, limit int) (responses.Products, custom_error.CustomError)
GetCategories(ctx context.Context, limit int) (responses.Categories, custom_error.CustomError)
ShowCategory(ctx context.Context, columnName string, value any) (responses.Category, custom_error.CustomError)
GetCategories(ctx context.Context, limit int) (*responses.Categories, custom_error.CustomError)
ShowCategory(ctx context.Context, columnName string, value any) (*responses.Category, custom_error.CustomError)

ListProductByCategorySlug(c *gin.Context, slug string) (pagination.Pagination, error)

// GetMenu fetch categories to show in menu
GetMenu(c context.Context) ([]CustomerResp.CategoryResponse, error)

// otp

SendOtp(ctx context.Context, Mobile string) (entities.OTP, custom_error.CustomError)

VerifyOtp(c *gin.Context, mobile string, req requests.CustomerVerifyRequest) custom_error.CustomError
ProcessCustomerAuthentication(c *gin.Context, mobile string) (CustomerResp.CustomerSession, custom_error.CustomError)
LogOut(c *gin.Context) bool
Expand Down

0 comments on commit 2842d71

Please sign in to comment.