From 7a884e58568686e0a5e251e3f63e6eab414a8f41 Mon Sep 17 00:00:00 2001 From: lendral3n Date: Tue, 13 Feb 2024 21:00:08 +0700 Subject: [PATCH] update webh --- features/admin/entity.go | 1 + features/booking/data/model.go | 19 +++++-------------- features/booking/data/query.go | 19 +++++++++++++++---- features/booking/entity.go | 11 ++++++----- features/booking/handler/handler.go | 6 +++++- features/booking/service/logic.go | 12 +++++++----- 6 files changed, 39 insertions(+), 29 deletions(-) diff --git a/features/admin/entity.go b/features/admin/entity.go index 72c778c..c1abbf8 100644 --- a/features/admin/entity.go +++ b/features/admin/entity.go @@ -4,6 +4,7 @@ type DashboardData struct { TotalUser int TotalBooking int TotalKos int + TotalBookingPerMonth int } diff --git a/features/booking/data/model.go b/features/booking/data/model.go index 3f2fb65..323344e 100644 --- a/features/booking/data/model.go +++ b/features/booking/data/model.go @@ -33,6 +33,11 @@ type Payment struct { PaidAt *time.Time `gorm:"default:null;"` } +// type MonthCount struct { +// Month int +// Count int +// } + func CoreToModelBook(input booking.BookingCore) Booking { return Booking{ Code: input.Code, @@ -93,17 +98,3 @@ func WebhoocksCoreToModel(reqNotif booking.BookingCore) Booking { Status: reqNotif.Status, } } - -// func (mod *Booking) GenerateCode() (err error) { -// // mod.Code, err = strconv.Atoi(fmt.Sprintf("%d%d%d", mod.UserId, mod.BoardingHouseId, time.Now().Unix())) -// var bookCode int -// mod.Code, err = strconv.Atoi(fmt.Sprintf("%d%d%d", mod.UserId, mod.BoardingHouseId, time.Now().Unix())) -// if err != nil { -// return err -// } -// // var stringCode string -// stringCode := strconv.Itoa(bookCode) -// mod.Code = stringCode - -// return -// } diff --git a/features/booking/data/query.go b/features/booking/data/query.go index b931f19..66c4b2c 100644 --- a/features/booking/data/query.go +++ b/features/booking/data/query.go @@ -142,10 +142,6 @@ func (repo *bookQuery) GetRatingAndFacility(userId uint) ([]kos.Core, error) { for _, k := range kosData { result = append(result, k.ModelToCoreKos()) } - // for _, v := range kosData { - // fmt.Println(v.Ratings) - - // } return result, nil } @@ -157,3 +153,18 @@ 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() + if err != nil { + return nil, err + } + defer rows.Close() + for rows.Next() { + var count int + rows.Scan(&count) + counts = append(counts, count) + } + return counts, nil +} diff --git a/features/booking/entity.go b/features/booking/entity.go index a6a86a8..ad31382 100644 --- a/features/booking/entity.go +++ b/features/booking/entity.go @@ -22,11 +22,11 @@ type BookingCore struct { Payment PaymentCore } -// type WebhoocksRequesCore struct { -// Code string -// Status string -// Payment PaymentCore -// } +type MonthCount struct { + Month int + Count int +} + type PaymentCore struct { Method string @@ -47,6 +47,7 @@ type BookDataInterface interface { GetBooking(userId uint) ([]BookingCore, error) WebhoocksData(webhoocksReq BookingCore) error GetTotalBooking() (int, error) + GetTotalBookingPerYear(year int) ([]int, error) } // interface untuk Service Layer diff --git a/features/booking/handler/handler.go b/features/booking/handler/handler.go index f0d1026..fdcd460 100644 --- a/features/booking/handler/handler.go +++ b/features/booking/handler/handler.go @@ -38,7 +38,11 @@ func (handler *BookHandler) CreateBook(c echo.Context) error { return c.JSON(http.StatusInternalServerError, responses.WebResponse(errInsert.Error(), nil)) } - result := CoreToResponseBook(payment) + // result := CoreToResponseBook(payment) + result := BookingResponse{} + if payment != nil { + result = CoreToResponseBook(payment) + } return c.JSON(http.StatusOK, responses.WebResponse("success booking kos", result)) } diff --git a/features/booking/service/logic.go b/features/booking/service/logic.go index ee23f50..6bb626f 100644 --- a/features/booking/service/logic.go +++ b/features/booking/service/logic.go @@ -29,12 +29,14 @@ func (bs *bookService) Create(userIdLogin int, input booking.BookingCore) (*book return nil, errors.New("anda bukan renter") } - bookCore, err := bs.bookData.Insert(userIdLogin, input) - if err != nil { - return nil, err - } + // bookCore, err := bs.bookData.Insert(userIdLogin, input) + // if err != nil { + // return nil, err + // } + + - return bookCore, nil + return nil, nil } // CancelBooking implements booking.BookServiceInterface.