Skip to content

Commit

Permalink
Merge pull request #106 from Kos-Kita/totalbookingpermonth
Browse files Browse the repository at this point in the history
create total booking per bulan
  • Loading branch information
lendral3n authored Feb 13, 2024
2 parents d77e513 + a2ebd4b commit 94101ba
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 40 deletions.
4 changes: 2 additions & 2 deletions features/admin/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ type DashboardData struct {
TotalUser int
TotalBooking int
TotalKos int
TotalBookingPerMonth int
TotalBookingPerMonth []int
}


// interface untuk Service Layer
type AdminServiceInterface interface {
GetTotalData(userIdLogin int)(DashboardData, error)
GetTotalData(userIdLogin int, uear int)(DashboardData, error)
}
13 changes: 11 additions & 2 deletions features/admin/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"KosKita/utils/middlewares"
"KosKita/utils/responses"
"net/http"
"strconv"
"time"

"github.com/labstack/echo/v4"
)
Expand All @@ -25,7 +27,14 @@ func (handler *AdminHandler) GetAllData(c echo.Context) error {
return c.JSON(http.StatusUnauthorized, responses.WebResponse("Unauthorized user", nil))
}

dashboardData, errGet := handler.adminService.GetTotalData(userIdLogin)
// get the year from the query parameter, or use the current year as default
year, err := strconv.Atoi(c.QueryParam("year"))
if err != nil {
year = time.Now().Year()
}

// pass the year as the second argument to GetTotalData
dashboardData, errGet := handler.adminService.GetTotalData(userIdLogin, year)
if errGet != nil {
if errGet.Error() == "anda bukan admin" {
return c.JSON(http.StatusUnauthorized, responses.WebResponse(errGet.Error(), nil))
Expand All @@ -36,4 +45,4 @@ func (handler *AdminHandler) GetAllData(c echo.Context) error {
dashboardResult := CoreToResponseDashboard(&dashboardData)

return c.JSON(http.StatusOK, responses.WebResponse("success get dashboard data", dashboardResult))
}
}
20 changes: 14 additions & 6 deletions features/admin/handler/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@ package handler
import "KosKita/features/admin"

type DashboardData struct {
TotalUser int `json:"total_user"`
TotalBooking int `json:"total_booking"`
TotalKos int `json:"total_kos"`
TotalUser int `json:"total_user"`
TotalBooking int `json:"total_booking"`
TotalKos int `json:"total_kos"`
TotalBookingPerMonth map[string]int `json:"total_booking_per_month"`
}

func CoreToResponseDashboard(core *admin.DashboardData) DashboardData {
bulan := []string{"Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "November", "Desember"}
totalBookingPerMonth := make(map[string]int)
for i, jumlah := range core.TotalBookingPerMonth {
totalBookingPerMonth[bulan[i]] = jumlah
}

return DashboardData{
TotalUser: core.TotalUser,
TotalBooking: core.TotalBooking,
TotalKos: core.TotalKos,
TotalUser: core.TotalUser,
TotalBooking: core.TotalBooking,
TotalKos: core.TotalKos,
TotalBookingPerMonth: totalBookingPerMonth,
}
}
11 changes: 10 additions & 1 deletion features/admin/service/logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func New(repoUser user.UserDataInterface, repoKos kos.KosDataInterface, repoBook
}

// GetTotalData implements admin.AdminServiceInterface.
func (as *adminService) GetTotalData(userIdLogin int) (admin.DashboardData, error) {
func (as *adminService) GetTotalData(userIdLogin int, year int) (admin.DashboardData, error) {
user, err := as.userService.GetById(userIdLogin)
if err != nil {
return admin.DashboardData{}, err
Expand Down Expand Up @@ -56,5 +56,14 @@ func (as *adminService) GetTotalData(userIdLogin int) (admin.DashboardData, erro
}
dashboardData.TotalBooking = totalBooking

for month := 1; month <= 12; month++ {
totalBookingPerMonth, err := as.bookingData.GetTotalBookingPerMonth(year, month)
if err != nil {
return dashboardData, err
}
dashboardData.TotalBookingPerMonth = append(dashboardData.TotalBookingPerMonth, totalBookingPerMonth)
}


return dashboardData, nil
}
46 changes: 18 additions & 28 deletions features/booking/data/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"KosKita/features/booking"
"KosKita/features/kos"
"KosKita/features/kos/data"
kd "KosKita/features/kos/data"
"KosKita/utils/externalapi"
"errors"
"log"
Expand All @@ -26,30 +25,26 @@ func New(db *gorm.DB, mid externalapi.MidtransInterface) booking.BookDataInterfa

// Insert implements booking.BookDataInterface.
func (repo *bookQuery) Insert(userIdLogin int, input booking.BookingCore) (*booking.BookingCore, error) {
boardingHouse := kd.BoardingHouse{}
boardingHouse := data.BoardingHouse{}
if err := repo.db.First(&boardingHouse, input.BoardingHouseId).Error; err != nil {
return nil, err
}

input.Total = float64(boardingHouse.Price)

bookModel := CoreToModelBook(input)
bookModel.UserId = uint(userIdLogin)
bookModel.Total = input.Total

if err := repo.db.Create(&bookModel).Error; err != nil {
return nil, err
}

input.Code = bookModel.Code


log.Println("input book", input)
payment, errPay := repo.paymentMidtrans.NewOrderPayment(input)

log.Println("input payment", payment)
if errPay != nil {
return nil, errPay
}

log.Println("input payment", payment)

if err := repo.db.Create(&bookModel).Error; err != nil {
return nil, err
}

bookModel.Method = payment.Method
bookModel.Bank = payment.Bank
Expand All @@ -59,9 +54,9 @@ func (repo *bookQuery) Insert(userIdLogin int, input booking.BookingCore) (*book

log.Println("input bookmodel", bookModel)

if err := repo.db.Updates(&bookModel).Error; err != nil {
return nil, err
}
// if err := repo.db.Updates(&bookModel).Error; err != nil {
// return nil, err
// }

bookCore := ModelToCoreBook(bookModel)
// if payment != nil {
Expand Down Expand Up @@ -156,17 +151,12 @@ func (repo *bookQuery) GetTotalBooking() (int, error) {
return int(count), nil
}

func (repo *bookQuery) GetTotalBookingPerYear(year int) ([]int, error) {
var counts []int
rows, err := repo.db.Raw("SELECT COUNT(*) as count FROM bookings WHERE YEAR(created_at) = ? GROUP BY MONTH(created_at) ORDER BY MONTH(created_at)", year).Rows()
func (repo *bookQuery) GetTotalBookingPerMonth(year int, month int) (int, error) {
var count int
row := repo.db.Raw("SELECT COUNT(*) as count FROM bookings WHERE YEAR(created_at) = ? AND MONTH(created_at) = ?", year, month).Row()
err := row.Scan(&count)
if err != nil {
return nil, err
return 0, err
}
defer rows.Close()
for rows.Next() {
var count int
rows.Scan(&count)
counts = append(counts, count)
}
return counts, nil
}
return count, nil
}
2 changes: 1 addition & 1 deletion features/booking/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type BookDataInterface interface {
GetBooking(userId uint) ([]BookingCore, error)
WebhoocksData(webhoocksReq BookingCore) error
GetTotalBooking() (int, error)
GetTotalBookingPerYear(year int) ([]int, error)
GetTotalBookingPerMonth(year int, month int) (int, error)
}

// interface untuk Service Layer
Expand Down

0 comments on commit 94101ba

Please sign in to comment.