diff --git a/features/booking/data/model.go b/features/booking/data/model.go index 0fb634b..3f2fb65 100644 --- a/features/booking/data/model.go +++ b/features/booking/data/model.go @@ -4,14 +4,13 @@ import ( "KosKita/features/booking" kd "KosKita/features/kos/data" ud "KosKita/features/user/data" - "fmt" "time" "gorm.io/gorm" ) type Booking struct { - Code string `gorm:"column:code; primaryKey;"` + Code string `gorm:"column:code; type:varchar(36);primary_key" json:"id"` Total float64 `gorm:"column:total;"` UserId uint BoardingHouseId uint @@ -24,15 +23,14 @@ type Booking struct { } type Payment struct { - Method string `gorm:"column:method; type:varchar(20);"` - Bank string `gorm:"column:bank; type:varchar(20);"` - VirtualNumber string `gorm:"column:virtual_number; type:varchar(50);"` - BillKey string `gorm:"column:bill_key; type:varchar(50);"` - BillCode string `gorm:"column:bill_code; type:varchar(50);"` - // Status string `gorm:"column:status; type:varchar(50);"` - CreatedAt time.Time `gorm:"index"` - ExpiredAt *time.Time `gorm:"nullable"` - PaidAt *time.Time `gorm:"default:null;"` + Method string `gorm:"column:method; type:varchar(20);"` + Bank string `gorm:"column:bank; type:varchar(20);"` + VirtualNumber string `gorm:"column:virtual_number; type:varchar(50);"` + BillKey string `gorm:"column:bill_key; type:varchar(50);"` + BillCode string `gorm:"column:bill_code; type:varchar(50);"` + CreatedAt time.Time `gorm:"index"` + ExpiredAt *time.Time `gorm:"nullable"` + PaidAt *time.Time `gorm:"default:null;"` } func CoreToModelBook(input booking.BookingCore) Booking { @@ -40,12 +38,27 @@ func CoreToModelBook(input booking.BookingCore) Booking { Code: input.Code, UserId: input.UserId, BoardingHouseId: input.BoardingHouseId, + Total: input.Total, + BookedAt: input.BookedAt, + Status: input.Status, + Payment: CoreToModelPay(input.Payment), + } +} + +func CoreToModelPay(input booking.PaymentCore) Payment { + return Payment{ + Method: input.Method, + Bank: input.Bank, + VirtualNumber: input.VirtualNumber, + BillKey: input.BillKey, + BillCode: input.BillCode, + ExpiredAt: &input.ExpiredAt, + PaidAt: &input.PaidAt, } } func CoreToModelBookCancel(input booking.BookingCore) Booking { return Booking{ - Status: input.Status, } } @@ -74,9 +87,17 @@ func PaymentModelToCore(model Payment) booking.PaymentCore { } } +func WebhoocksCoreToModel(reqNotif booking.BookingCore) Booking { + return Booking{ + Code: reqNotif.Code, + 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 -// bookCode, err = strconv.Atoi(fmt.Sprintf("%d%d%d", mod.UserId, mod.BoardingHouseId, time.Now().Unix())) +// mod.Code, err = strconv.Atoi(fmt.Sprintf("%d%d%d", mod.UserId, mod.BoardingHouseId, time.Now().Unix())) // if err != nil { // return err // } @@ -86,34 +107,3 @@ func PaymentModelToCore(model Payment) booking.PaymentCore { // return // } - -func (mod *Booking) GenerateCode() (err error) { - mod.Code = fmt.Sprintf("%d%d%d", mod.UserId, mod.BoardingHouseId, time.Now().Unix()) - return -} - -func WebhoocksCoreToModel(reqNotif booking.BookingCore) Booking { - return Booking{ - Code: reqNotif.Code, - Status: reqNotif.Status, - } -} - -// func WebhoocksCoreToModel(reqNotif booking.BookingCore) (Booking, error) { - -// return Booking{ -// Code: reqNotif.Code, -// Payment: payment, -// }, nil -// } - -// func Notif(reqNotif booking.PaymentCore) (Payment, error) { -// switch reqNotif.Status { -// case "pending", "success", "failed", "expired": -// return Payment{ -// Status: reqNotif.Status, -// }, nil -// default: -// return Payment{}, fmt.Errorf("invalid payment status: %s", reqNotif.Status) -// } -// } diff --git a/features/booking/data/query.go b/features/booking/data/query.go index 011016a..b931f19 100644 --- a/features/booking/data/query.go +++ b/features/booking/data/query.go @@ -36,10 +36,6 @@ func (repo *bookQuery) Insert(userIdLogin int, input booking.BookingCore) (*book bookModel.UserId = uint(userIdLogin) bookModel.Total = input.Total - if err := bookModel.GenerateCode(); err != nil { - return nil, err - } - if err := repo.db.Create(&bookModel).Error; err != nil { return nil, err } @@ -57,7 +53,6 @@ func (repo *bookQuery) Insert(userIdLogin int, input booking.BookingCore) (*book bookModel.Payment.VirtualNumber = payment.VirtualNumber bookModel.Payment.BillKey = payment.BillKey bookModel.Payment.BillCode = payment.BillCode - bookModel.Status = bookModel.Status bookModel.Payment.ExpiredAt = &payment.ExpiredAt bookModel.Payment.PaidAt = &payment.PaidAt bookModel.Payment.PaidAt = &payment.PaidAt diff --git a/features/booking/handler/request.go b/features/booking/handler/request.go index bb19fb8..7797666 100644 --- a/features/booking/handler/request.go +++ b/features/booking/handler/request.go @@ -2,6 +2,8 @@ package handler import ( "KosKita/features/booking" + + "github.com/google/uuid" ) type BookRequest struct { @@ -16,6 +18,7 @@ type CancelBookingRequest struct { func RequestToCoreBook(input BookRequest, userIdLogin uint) booking.BookingCore { return booking.BookingCore{ + Code: uuid.New().String(), UserId: userIdLogin, BoardingHouseId: input.BoardingHouseId, Payment: booking.PaymentCore{ @@ -39,7 +42,7 @@ type WebhoocksRequest struct { func WebhoocksRequestToCore(input WebhoocksRequest) booking.BookingCore { return booking.BookingCore{ - Code: input.Code, + Code: input.Code, Status: input.Status, } -} \ No newline at end of file +} diff --git a/features/kos/data/query.go b/features/kos/data/query.go index d77737b..d374627 100644 --- a/features/kos/data/query.go +++ b/features/kos/data/query.go @@ -116,14 +116,13 @@ func (repo *kosQuery) Update(userIdLogin int, input kos.CoreInput) error { return nil } - func (repo *kosQuery) CekRating(userId, kosId int) (*kos.RatingCore, error) { var ratingData Rating tx := repo.db.Where("user_id = ? AND boarding_house_id = ?", userId, kosId).First(&ratingData) if tx.Error != nil { - return nil, tx.Error + return nil, nil } ratingCore := ratingData.ModelToCoreRating()