update config
This commit is contained in:
@@ -116,6 +116,7 @@ func (r *memberRepository) toRegistrationDBModel(registration *entity.MemberRegi
|
||||
UpdatedAt: registration.UpdatedAt,
|
||||
BranchID: registration.BranchID,
|
||||
CashierID: registration.CashierID,
|
||||
Password: registration.Password,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ type MemberRegistrationDB struct {
|
||||
UpdatedAt time.Time `gorm:"column:updated_at"`
|
||||
BranchID int64 `gorm:"column:branch_id"`
|
||||
CashierID int64 `gorm:"column:cashier_id"`
|
||||
Password string `gorm:"column:password"`
|
||||
}
|
||||
|
||||
func (MemberRegistrationDB) TableName() string {
|
||||
|
||||
@@ -38,6 +38,8 @@ type OrderRepository interface {
|
||||
req entity.PopularProductsRequest,
|
||||
) ([]entity.PopularProductItem, error)
|
||||
FindByIDAndPartnerID(ctx mycontext.Context, id int64, partnerID int64) (*entity.Order, error)
|
||||
GetOrderHistoryByUserID(ctx mycontext.Context, userID int64, req entity.SearchRequest) ([]*entity.Order, int64, error)
|
||||
FindByIDAndCustomerID(ctx mycontext.Context, id int64, customerID int64) (*entity.Order, error)
|
||||
}
|
||||
|
||||
type orderRepository struct {
|
||||
@@ -299,6 +301,10 @@ func (r *orderRepository) toDomainOrderItemModel(dbModel *models.OrderItemDB) *e
|
||||
CreatedBy: dbModel.CreatedBy,
|
||||
CreatedAt: dbModel.CreatedAt,
|
||||
ItemName: dbModel.ItemName,
|
||||
Product: &entity.Product{
|
||||
ID: dbModel.ItemID,
|
||||
Name: dbModel.ItemName,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -416,6 +422,60 @@ func (r *orderRepository) GetOrderHistoryByPartnerID(ctx mycontext.Context, part
|
||||
return orders, totalCount, nil
|
||||
}
|
||||
|
||||
func (r *orderRepository) GetOrderHistoryByUserID(ctx mycontext.Context, userID int64, req entity.SearchRequest) ([]*entity.Order, int64, error) {
|
||||
var ordersDB []models.OrderDB
|
||||
var totalCount int64
|
||||
|
||||
baseQuery := r.db.Model(&models.OrderDB{}).Where("customer_id = ?", userID)
|
||||
|
||||
if req.Status != "" {
|
||||
baseQuery = baseQuery.Where("status = ?", req.Status)
|
||||
}
|
||||
|
||||
if !req.Start.IsZero() {
|
||||
baseQuery = baseQuery.Where("created_at >= ?", req.Start)
|
||||
}
|
||||
|
||||
if !req.End.IsZero() {
|
||||
baseQuery = baseQuery.Where("created_at <= ?", req.End)
|
||||
}
|
||||
|
||||
if err := baseQuery.Count(&totalCount).Error; err != nil {
|
||||
return nil, 0, errors.Wrap(err, "failed to count total orders")
|
||||
}
|
||||
|
||||
query := baseQuery.Session(&gorm.Session{})
|
||||
|
||||
query = query.Order("created_at DESC")
|
||||
|
||||
if req.Limit > 0 {
|
||||
query = query.Limit(req.Limit)
|
||||
}
|
||||
|
||||
if req.Offset > 0 {
|
||||
query = query.Offset(req.Offset)
|
||||
}
|
||||
|
||||
if err := query.Preload("OrderItems").Find(&ordersDB).Error; err != nil {
|
||||
return nil, 0, errors.Wrap(err, "failed to find order history by partner ID")
|
||||
}
|
||||
|
||||
orders := make([]*entity.Order, 0, len(ordersDB))
|
||||
for _, orderDB := range ordersDB {
|
||||
order := r.toDomainOrderModel(&orderDB)
|
||||
order.OrderItems = make([]entity.OrderItem, 0, len(orderDB.OrderItems))
|
||||
|
||||
for _, itemDB := range orderDB.OrderItems {
|
||||
item := r.toDomainOrderItemModel(&itemDB)
|
||||
order.OrderItems = append(order.OrderItems, *item)
|
||||
}
|
||||
|
||||
orders = append(orders, order)
|
||||
}
|
||||
|
||||
return orders, totalCount, nil
|
||||
}
|
||||
|
||||
func (r *orderRepository) CreateOrUpdate(ctx mycontext.Context, order *entity.Order) (*entity.Order, error) {
|
||||
isUpdate := order.ID != 0
|
||||
|
||||
@@ -856,3 +916,23 @@ func (r *orderRepository) FindByIDAndPartnerID(ctx mycontext.Context, id int64,
|
||||
|
||||
return order, nil
|
||||
}
|
||||
|
||||
func (r *orderRepository) FindByIDAndCustomerID(ctx mycontext.Context, id int64, customerID int64) (*entity.Order, error) {
|
||||
var orderDB models.OrderDB
|
||||
|
||||
if err := r.db.Preload("OrderItems").Where("id = ? AND customer_id = ?", id, customerID).First(&orderDB).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errors.New("order not found")
|
||||
}
|
||||
return nil, errors.Wrap(err, "failed to find order")
|
||||
}
|
||||
|
||||
order := r.toDomainOrderModel(&orderDB)
|
||||
|
||||
for _, itemDB := range orderDB.OrderItems {
|
||||
item := r.toDomainOrderItemModel(&itemDB)
|
||||
order.OrderItems = append(order.OrderItems, *item)
|
||||
}
|
||||
|
||||
return order, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user