stock request table created
This commit is contained in:
56
repositories/stockrequest.go
Normal file
56
repositories/stockrequest.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"nearle/models"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type StockRequestRepository interface {
|
||||
CreateStockRequest(req *models.StockRequest) error
|
||||
GetStockRequests(tenantID int, locationID int, status string, pageNo int, pageSize int) ([]models.StockRequest, error)
|
||||
GetStockRequestByID(requestID int) (*models.StockRequest, error)
|
||||
UpdateStockRequest(requestID int, status string) error
|
||||
}
|
||||
|
||||
type stockRequestRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewStockRequestRepository(db *gorm.DB) StockRequestRepository {
|
||||
return &stockRequestRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *stockRequestRepository) CreateStockRequest(req *models.StockRequest) error {
|
||||
return r.db.Create(req).Error
|
||||
}
|
||||
|
||||
func (r *stockRequestRepository) GetStockRequests(tenantID int, locationID int, status string, pageNo int, pageSize int) ([]models.StockRequest, error) {
|
||||
var requests []models.StockRequest
|
||||
query := r.db.Table("stockrequests").
|
||||
Select("stockrequests.*, products.productname, products.productimage").
|
||||
Joins("left join products on products.productid = stockrequests.productid").
|
||||
Where("stockrequests.tenantid = ?", tenantID)
|
||||
|
||||
if locationID > 0 {
|
||||
query = query.Where("stockrequests.locationid = ?", locationID)
|
||||
}
|
||||
|
||||
if status != "" {
|
||||
query = query.Where("stockrequests.status = ?", status)
|
||||
}
|
||||
|
||||
offset := (pageNo - 1) * pageSize
|
||||
err := query.Order("stockrequests.created DESC").Offset(offset).Limit(pageSize).Find(&requests).Error
|
||||
return requests, err
|
||||
}
|
||||
|
||||
func (r *stockRequestRepository) GetStockRequestByID(requestID int) (*models.StockRequest, error) {
|
||||
var req models.StockRequest
|
||||
err := r.db.Table("stockrequests").Where("requestid = ?", requestID).First(&req).Error
|
||||
return &req, err
|
||||
}
|
||||
|
||||
func (r *stockRequestRepository) UpdateStockRequest(requestID int, status string) error {
|
||||
return r.db.Model(&models.StockRequest{}).Where("requestid = ?", requestID).Update("status", status).Error
|
||||
}
|
||||
Reference in New Issue
Block a user