Skip to content

Commit

Permalink
bugfix: fix bug response card dashboard (#75)
Browse files Browse the repository at this point in the history
bugfix:fix bug response card dashboard
  • Loading branch information
masnann authored Dec 3, 2023
2 parents 5b86bc8 + 14a407c commit 70eb4ec
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
16 changes: 13 additions & 3 deletions module/feature/dashboard/handler/handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handler

import (
"errors"
"fmt"
"github.com/capstone-kelompok-7/backend-disappear/module/entities"
"github.com/capstone-kelompok-7/backend-disappear/module/feature/dashboard"
Expand Down Expand Up @@ -28,10 +29,19 @@ func (h *DashboardHandler) GetCardDashboard() echo.HandlerFunc {
}
productCount, userCount, orderCount, incomeCount, err := h.service.GetCardDashboard()
if err != nil {
c.Logger().Error("handler: failed to fetch all products:", err.Error())
return response.SendBadRequestResponse(c, "Gagal mendapatkan data : "+err.Error())
c.Logger().Error("handler: failed to fetch dashboard data:", err.Error())
if errors.Is(err, errors.New("gagal menghitung total produk")) {
productCount = 0
} else if errors.Is(err, errors.New("gagal menghitung total pelanggan")) {
userCount = 0
} else if errors.Is(err, errors.New("gagal menghitung total pesanan")) {
orderCount = 0
} else if errors.Is(err, errors.New("gagal menghitung total pendapatan")) {
incomeCount = 0
} else {
return response.SendBadRequestResponse(c, "Gagal mendapatkan data: "+err.Error())
}
}

return response.SendStatusOkWithDataResponse(c, "Berhasil mendapatkan data cards dasboard", dto.FormatCardResponse(productCount, userCount, orderCount, incomeCount))
}
}
Expand Down
14 changes: 9 additions & 5 deletions module/feature/dashboard/repository/repository.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package repository

import (
"database/sql"
"errors"
"github.com/capstone-kelompok-7/backend-disappear/module/entities"
"github.com/capstone-kelompok-7/backend-disappear/module/feature/dashboard"
"gorm.io/gorm"
Expand Down Expand Up @@ -45,18 +47,21 @@ func (r *DashboardRepository) CountOrder() (int64, error) {
}

func (r *DashboardRepository) CountIncome() (float64, error) {
var totalAmount float64
var totalAmount sql.NullFloat64

firstDay := time.Now().AddDate(0, 0, -time.Now().Day()+1).Format("2006-01-02")
lastDay := time.Now().AddDate(0, 1, -time.Now().Day()).Format("2006-01-02")
if err := r.db.Model(&entities.OrderModels{}).
Where("order_status = ? AND payment_status = ? AND created_at BETWEEN ? AND ?",
"proses", "konfirmasi", firstDay, lastDay).
Select("SUM(total_amount_paid)").
Scan(&totalAmount).Error; err != nil {
return 0, err
Row().Scan(&totalAmount); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return 0.0, nil
}
return 0.0, err
}
return totalAmount, nil
return totalAmount.Float64, nil
}

func (r *DashboardRepository) CountTotalGram() (int64, error) {
Expand Down Expand Up @@ -84,7 +89,6 @@ func (r *DashboardRepository) GetProductWithMaxReviews() ([]*entities.ProductMod
return nil, err
}
return products, nil

}

func (r *DashboardRepository) GetGramPlasticStat(startOfWeek, endOfWeek time.Time) (uint64, error) {
Expand Down
1 change: 0 additions & 1 deletion module/feature/dashboard/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func (s *DashboardService) GetCardDashboard() (int64, int64, int64, float64, err
}

return productCount, userCount, orderCount, inComeCount, nil

}

func (s *DashboardService) GetLandingPage() (int64, int64, int64, error) {
Expand Down

0 comments on commit 70eb4ec

Please sign in to comment.