Skip to content

Commit

Permalink
Merge pull request #96 from Kos-Kita/updateid
Browse files Browse the repository at this point in the history
update book
  • Loading branch information
lendral3n authored Feb 13, 2024
2 parents 7df0b22 + 414cb01 commit 9465a74
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 53 deletions.
78 changes: 34 additions & 44 deletions features/booking/data/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,28 +23,42 @@ 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 {
return 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,
}
}
Expand Down Expand Up @@ -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
// }
Expand All @@ -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)
// }
// }
5 changes: 0 additions & 5 deletions features/booking/data/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
7 changes: 5 additions & 2 deletions features/booking/handler/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package handler

import (
"KosKita/features/booking"

"github.com/google/uuid"
)

type BookRequest struct {
Expand All @@ -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{
Expand All @@ -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,
}
}
}
3 changes: 1 addition & 2 deletions features/kos/data/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 9465a74

Please sign in to comment.