2018-12-16 16:27:33 +01:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
2019-01-13 21:09:49 +01:00
|
|
|
"sort"
|
|
|
|
"time"
|
2018-12-16 16:27:33 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Product struct {
|
2019-01-13 21:09:49 +01:00
|
|
|
ID uint `gorm:"primary_key"`
|
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
2018-12-16 16:27:33 +01:00
|
|
|
Name string
|
2018-12-20 20:50:01 +01:00
|
|
|
Company Company `json:",omitempty"`
|
2018-12-16 16:27:33 +01:00
|
|
|
CompanyID uint
|
|
|
|
Code string `gorm:"unique; unique_index"`
|
2018-12-20 20:50:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetProducts() ([]*Product) {
|
|
|
|
|
|
|
|
products := make([]*Product, 0)
|
|
|
|
GetDB().Find(&products)
|
|
|
|
|
2019-01-13 21:09:49 +01:00
|
|
|
sort.Slice(products, func(i, j int) bool {
|
|
|
|
return products[i].Name < products[j].Name
|
|
|
|
})
|
|
|
|
|
2018-12-20 20:50:01 +01:00
|
|
|
return products
|
2019-01-05 16:19:24 +01:00
|
|
|
}
|
|
|
|
|
2019-01-13 21:09:49 +01:00
|
|
|
func DeleteProduct(id uint) (err error) {
|
|
|
|
|
|
|
|
product := &Product{}
|
|
|
|
product.ID = id
|
|
|
|
return GetDB().Delete(&product).Error
|
|
|
|
}
|
|
|
|
|
2019-01-05 16:19:24 +01:00
|
|
|
func GetProduct(barcode string) (*Product) {
|
|
|
|
|
|
|
|
product := &Product{}
|
|
|
|
if GetDB().Where("code = ?", barcode).First(product).RecordNotFound() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return product
|
|
|
|
}
|
|
|
|
|
|
|
|
func (product *Product) CreateOrUpdate() (*Product) {
|
|
|
|
|
|
|
|
GetDB().Save(product)
|
|
|
|
|
|
|
|
return product;
|
2018-12-16 16:27:33 +01:00
|
|
|
}
|