111 lines
2.9 KiB
Go
111 lines
2.9 KiB
Go
package ordersRepo
|
|
|
|
import (
|
|
"git.ma-al.com/goc_daniel/b2b/app/db"
|
|
"git.ma-al.com/goc_daniel/b2b/app/model"
|
|
constdata "git.ma-al.com/goc_daniel/b2b/app/utils/const_data"
|
|
"git.ma-al.com/goc_daniel/b2b/app/utils/query/filters"
|
|
"git.ma-al.com/goc_daniel/b2b/app/utils/query/find"
|
|
)
|
|
|
|
type UIOrdersRepo interface {
|
|
UserHasOrder(user_id uint, order_id uint) (bool, error)
|
|
Find(user_id uint, p find.Paging, filt *filters.FiltersList) (find.Found[model.CustomerOrder], error)
|
|
PlaceNewOrder(cart *model.CustomerCart, name string, country_id uint, address_info string) error
|
|
ChangeOrderAddress(order_id uint, country_id uint, address_info string) error
|
|
ChangeOrderStatus(order_id uint, status string) error
|
|
}
|
|
|
|
type OrdersRepo struct{}
|
|
|
|
func New() UIOrdersRepo {
|
|
return &OrdersRepo{}
|
|
}
|
|
|
|
func (repo *OrdersRepo) UserHasOrder(user_id uint, order_id uint) (bool, error) {
|
|
var amt uint
|
|
|
|
err := db.DB.
|
|
Table("b2b_customer_orders").
|
|
Select("COUNT(*) AS amt").
|
|
Where("user_id = ? AND order_id = ?", user_id, order_id).
|
|
Scan(&amt).
|
|
Error
|
|
|
|
return amt >= 1, err
|
|
}
|
|
|
|
func (repo *OrdersRepo) Find(user_id uint, p find.Paging, filt *filters.FiltersList) (find.Found[model.CustomerOrder], error) {
|
|
var list []model.CustomerOrder
|
|
var total int64
|
|
|
|
query := db.Get().
|
|
Model(&model.CustomerOrder{}).
|
|
Preload("Products").
|
|
Order("b2b_customer_orders.id DESC")
|
|
|
|
// Apply all filters
|
|
if filt != nil {
|
|
filt.ApplyAll(query)
|
|
}
|
|
|
|
// run counter first as query is without limit and offset
|
|
err := query.Count(&total).Error
|
|
if err != nil {
|
|
return find.Found[model.CustomerOrder]{}, err
|
|
}
|
|
|
|
err = query.
|
|
Limit(p.Limit()).
|
|
Offset(p.Offset()).
|
|
Find(&list).Error
|
|
if err != nil {
|
|
return find.Found[model.CustomerOrder]{}, err
|
|
}
|
|
|
|
return find.Found[model.CustomerOrder]{
|
|
Items: list,
|
|
Count: uint(total),
|
|
}, nil
|
|
}
|
|
|
|
func (repo *OrdersRepo) PlaceNewOrder(cart *model.CustomerCart, name string, country_id uint, address_info string) error {
|
|
order := model.CustomerOrder{
|
|
UserID: cart.UserID,
|
|
Name: name,
|
|
CountryID: country_id,
|
|
AddressJSON: address_info,
|
|
Status: constdata.NEW_ORDER_STATUS,
|
|
Products: make([]model.OrderProduct, 0, len(cart.Products)),
|
|
}
|
|
|
|
for _, product := range cart.Products {
|
|
order.Products = append(order.Products, model.OrderProduct{
|
|
ProductID: product.ProductID,
|
|
ProductAttributeID: product.ProductAttributeID,
|
|
Amount: product.Amount,
|
|
})
|
|
}
|
|
|
|
return db.DB.Create(&order).Error
|
|
}
|
|
|
|
func (repo *OrdersRepo) ChangeOrderAddress(order_id uint, country_id uint, address_info string) error {
|
|
return db.DB.
|
|
Table("b2b_customer_orders").
|
|
Where("order_id = ?", order_id).
|
|
Updates(map[string]interface{}{
|
|
"country_id": country_id,
|
|
"address_info": address_info,
|
|
}).
|
|
Error
|
|
}
|
|
|
|
func (repo *OrdersRepo) ChangeOrderStatus(order_id uint, status string) error {
|
|
return db.DB.
|
|
Table("b2b_customer_orders").
|
|
Where("order_id = ?", order_id).
|
|
Update("status", status).
|
|
Error
|
|
}
|