Browse Source

Product new fields

Vova Tkach 5 years ago
parent
commit
f8ac2a7b6c

+ 33 - 2
engine/fetdata/shop.go

@@ -53,6 +53,9 @@ func (this *Shop) load() {
 			shop_products.price,
 			shop_products.name,
 			shop_products.alias,
+			shop_products.vendor,
+			shop_products.quantity,
+			shop_products.category,
 			shop_products.briefly,
 			shop_products.content,
 			UNIX_TIMESTAMP(shop_products.datetime) as datetime,
@@ -67,11 +70,18 @@ func (this *Shop) load() {
 			shop_currencies.name,
 			shop_currencies.coefficient,
 			shop_currencies.code,
-			shop_currencies.symbol
+			shop_currencies.symbol,
+			shop_cats.id,
+			shop_cats.user,
+			shop_cats.name,
+			shop_cats.alias,
+			shop_cats.lft,
+			shop_cats.rgt
 		FROM
 			shop_products
 			LEFT JOIN users ON users.id = shop_products.user
 			LEFT JOIN shop_currencies ON shop_currencies.id = shop_products.currency
+			LEFT JOIN shop_cats ON shop_cats.id = shop_products.category
 		WHERE
 			shop_products.active = 1
 		ORDER BY
@@ -133,6 +143,9 @@ func (this *Shop) load() {
 				shop_products.price,
 				shop_products.name,
 				shop_products.alias,
+				shop_products.vendor,
+				shop_products.quantity,
+				shop_products.category,
 				shop_products.briefly,
 				shop_products.content,
 				UNIX_TIMESTAMP(shop_products.datetime) AS datetime,
@@ -147,12 +160,19 @@ func (this *Shop) load() {
 				shop_currencies.name,
 				shop_currencies.coefficient,
 				shop_currencies.code,
-				shop_currencies.symbol
+				shop_currencies.symbol,
+				shop_cats.id,
+				shop_cats.user,
+				shop_cats.name,
+				shop_cats.alias,
+				shop_cats.lft,
+				shop_cats.rgt
 			FROM
 				shop_products
 				LEFT JOIN shop_cat_product_rel ON shop_cat_product_rel.product_id = shop_products.id
 				LEFT JOIN users ON users.id = shop_products.user
 				LEFT JOIN shop_currencies ON shop_currencies.id = shop_products.currency
+				LEFT JOIN shop_cats ON shop_cats.id = shop_products.category
 			WHERE
 				shop_products.active = 1 AND
 				shop_cat_product_rel.category_id IN (` + strings.Join(cat_ids, ", ") + `)
@@ -179,6 +199,7 @@ func (this *Shop) load() {
 				rp := utils.MySql_shop_product{}
 				ru := utils.MySql_user{}
 				rc := utils.MySql_shop_currency{}
+				ro := utils.MySql_shop_category{}
 				if err := rows.Scan(
 					&rp.A_id,
 					&rp.A_user,
@@ -186,6 +207,9 @@ func (this *Shop) load() {
 					&rp.A_price,
 					&rp.A_name,
 					&rp.A_alias,
+					&rp.A_vendor,
+					&rp.A_quantity,
+					&rp.A_category,
 					&rp.A_briefly,
 					&rp.A_content,
 					&rp.A_datetime,
@@ -201,12 +225,19 @@ func (this *Shop) load() {
 					&rc.A_coefficient,
 					&rc.A_code,
 					&rc.A_symbol,
+					&ro.A_id,
+					&ro.A_user,
+					&ro.A_name,
+					&ro.A_alias,
+					&ro.A_lft,
+					&ro.A_rgt,
 				); err == nil {
 					this.products = append(this.products, &ShopProduct{
 						wrap:     this.wrap,
 						object:   &rp,
 						user:     &User{wrap: this.wrap, object: &ru},
 						currency: &Currency{wrap: this.wrap, object: &rc},
+						category: &ShopCategory{wrap: this.wrap, object: &ro},
 					})
 				}
 			}

+ 34 - 0
engine/fetdata/shop_category.go

@@ -13,6 +13,40 @@ type ShopCategory struct {
 	user *User
 }
 
+func (this *ShopCategory) load(id int) {
+	if this == nil {
+		return
+	}
+	if this.object != nil {
+		return
+	}
+	this.object = &utils.MySql_shop_category{}
+	if err := this.wrap.DB.QueryRow(`
+		SELECT
+			id,
+			user,
+			name,
+			alias,
+			lft,
+			rgt
+		FROM
+			users
+		WHERE
+			id = ?
+		LIMIT 1;`,
+		id,
+	).Scan(
+		&this.object.A_id,
+		&this.object.A_user,
+		&this.object.A_name,
+		&this.object.A_alias,
+		&this.object.A_lft,
+		&this.object.A_rgt,
+	); err != nil {
+		return
+	}
+}
+
 func (this *ShopCategory) Id() int {
 	if this == nil {
 		return 0

+ 27 - 0
engine/fetdata/shop_product.go

@@ -14,6 +14,7 @@ type ShopProduct struct {
 
 	user     *User
 	currency *Currency
+	category *ShopCategory
 }
 
 func (this *ShopProduct) Id() int {
@@ -75,6 +76,32 @@ func (this *ShopProduct) Alias() string {
 	return this.object.A_alias
 }
 
+func (this *ShopProduct) Vendor() string {
+	if this == nil {
+		return ""
+	}
+	return this.object.A_vendor
+}
+
+func (this *ShopProduct) Quantity() int {
+	if this == nil {
+		return 0
+	}
+	return this.object.A_quantity
+}
+
+func (this *ShopProduct) Category() *ShopCategory {
+	if this == nil {
+		return nil
+	}
+	if this.category != nil {
+		return this.category
+	}
+	this.category = &ShopCategory{wrap: this.wrap}
+	this.category.load(this.object.A_category)
+	return this.category
+}
+
 func (this *ShopProduct) Briefly() template.HTML {
 	if this == nil {
 		return template.HTML("")

+ 23 - 1
modules/module_index_act_mysql_setup.go

@@ -258,6 +258,9 @@ func (this *Modules) RegisterAction_IndexMysqlSetup() *Action {
 				price float(8,2) NOT NULL COMMENT 'Product price',
 				name varchar(255) NOT NULL COMMENT 'Product name',
 				alias varchar(255) NOT NULL COMMENT 'Product alias',
+				vendor varchar(255) NOT NULL,
+				quantity int(11) NOT NULL,
+				category int(11) NOT NULL,
 				briefly text NOT NULL COMMENT 'Product brief content',
 				content text NOT NULL COMMENT 'Product content',
 				datetime datetime NOT NULL COMMENT 'Creation date/time',
@@ -455,7 +458,7 @@ func (this *Modules) RegisterAction_IndexMysqlSetup() *Action {
 			return
 		}
 		if _, err = tx.Exec(
-			`INSERT INTO settings (name, value) VALUES ('database_version', '000000006');`,
+			`INSERT INTO settings (name, value) VALUES ('database_version', '000000007');`,
 		); err != nil {
 			tx.Rollback()
 			wrap.MsgError(err.Error())
@@ -541,6 +544,9 @@ func (this *Modules) RegisterAction_IndexMysqlSetup() *Action {
 				price = ?,
 				name = ?,
 				alias = ?,
+				vendor = ?,
+				quantity = ?,
+				category = ?,
 				briefly = ?,
 				content = ?,
 				datetime = ?,
@@ -552,6 +558,9 @@ func (this *Modules) RegisterAction_IndexMysqlSetup() *Action {
 			1000.00,
 			"Samsung Galaxy S10",
 			"samsung-galaxy-s10",
+			"Samsung",
+			"1",
+			"3",
 			"<p>Arcu ac tortor dignissim convallis aenean et tortor. Vitae auctor eu augue ut lectus arcu. Ac turpis egestas integer eget aliquet nibh praesent.</p>",
 			"<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Feugiat in ante metus dictum at tempor commodo ullamcorper a. Et malesuada fames ac turpis egestas sed tempus urna et. Euismod elementum nisi quis eleifend. Nisi porta lorem mollis aliquam ut porttitor. Ac turpis egestas maecenas pharetra convallis posuere. Nunc non blandit massa enim nec dui. Commodo elit at imperdiet dui accumsan sit amet nulla. Viverra accumsan in nisl nisi scelerisque. Dui nunc mattis enim ut tellus. Molestie ac feugiat sed lectus vestibulum mattis ullamcorper. Faucibus ornare suspendisse sed nisi lacus. Nulla facilisi morbi tempus iaculis. Ut eu sem integer vitae justo eget magna fermentum iaculis. Ullamcorper sit amet risus nullam eget felis eget nunc. Volutpat sed cras ornare arcu dui vivamus. Eget magna fermentum iaculis eu non diam.</p><p>Arcu ac tortor dignissim convallis aenean et tortor. Vitae auctor eu augue ut lectus arcu. Ac turpis egestas integer eget aliquet nibh praesent. Interdum velit euismod in pellentesque massa placerat duis. Vestibulum rhoncus est pellentesque elit ullamcorper dignissim cras tincidunt. Nisl rhoncus mattis rhoncus urna neque viverra justo. Odio ut enim blandit volutpat. Ac auctor augue mauris augue neque gravida. Ut lectus arcu bibendum at varius vel. Porttitor leo a diam sollicitudin tempor id eu nisl nunc. Dolor sit amet consectetur adipiscing elit duis tristique. Semper quis lectus nulla at volutpat diam ut. Sapien eget mi proin sed.</p>",
 			utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
@@ -715,6 +724,11 @@ func (this *Modules) RegisterAction_IndexMysqlSetup() *Action {
 			wrap.MsgError(err.Error())
 			return
 		}
+		if _, err = tx.Exec(`ALTER TABLE shop_products ADD KEY FK_shop_products_category (category);`); err != nil {
+			tx.Rollback()
+			wrap.MsgError(err.Error())
+			return
+		}
 		if _, err = tx.Exec(`ALTER TABLE users ADD UNIQUE KEY email (email);`); err != nil {
 			tx.Rollback()
 			wrap.MsgError(err.Error())
@@ -834,6 +848,14 @@ func (this *Modules) RegisterAction_IndexMysqlSetup() *Action {
 			wrap.MsgError(err.Error())
 			return
 		}
+		if _, err = tx.Exec(`
+			ALTER TABLE shop_products ADD CONSTRAINT FK_shop_products_category
+			FOREIGN KEY (category) REFERENCES shop_cats (id) ON DELETE RESTRICT;
+		`); err != nil {
+			tx.Rollback()
+			wrap.MsgError(err.Error())
+			return
+		}
 
 		// Commit all changes
 		err = tx.Commit()

+ 70 - 1
modules/module_shop.go

@@ -331,6 +331,9 @@ func (this *Modules) RegisterModule_Shop() *Module {
 					price,
 					name,
 					alias,
+					vendor,
+					quantity,
+					category,
 					briefly,
 					content,
 					UNIX_TIMESTAMP(datetime) as datetime,
@@ -349,6 +352,9 @@ func (this *Modules) RegisterModule_Shop() *Module {
 				&row.A_price,
 				&row.A_name,
 				&row.A_alias,
+				&row.A_vendor,
+				&row.A_quantity,
+				&row.A_category,
 				&row.A_briefly,
 				&row.A_content,
 				&row.A_datetime,
@@ -684,8 +690,14 @@ func (this *Modules) RegisterModule_Shop() *Module {
 			data := utils.MySql_shop_product{
 				A_id:       0,
 				A_user:     0,
+				A_currency: 0,
+				A_price:    0,
 				A_name:     "",
 				A_alias:    "",
+				A_vendor:   "",
+				A_quantity: 0,
+				A_category: 0,
+				A_briefly:  "",
 				A_content:  "",
 				A_datetime: 0,
 				A_active:   0,
@@ -706,6 +718,9 @@ func (this *Modules) RegisterModule_Shop() *Module {
 						price,
 						name,
 						alias,
+						vendor,
+						quantity,
+						category,
 						briefly,
 						content,
 						active
@@ -722,6 +737,9 @@ func (this *Modules) RegisterModule_Shop() *Module {
 					&data.A_price,
 					&data.A_name,
 					&data.A_alias,
+					&data.A_vendor,
+					&data.A_quantity,
+					&data.A_category,
 					&data.A_briefly,
 					&data.A_content,
 					&data.A_active,
@@ -814,6 +832,57 @@ func (this *Modules) RegisterModule_Shop() *Module {
 					Hint:    "Example: mobile-phone",
 					Max:     "255",
 				},
+				{
+					Kind:    builder.DFKText,
+					Caption: "Vendor/Count",
+					Name:    "vendor",
+					Value:   "0",
+					CallBack: func(field *builder.DataFormField) string {
+						return `<div class="form-group n3">` +
+							`<div class="row">` +
+							`<div class="col-md-3">` +
+							`<label for="lbl_vendor">Vendor/Count</label>` +
+							`</div>` +
+							`<div class="col-md-9">` +
+							`<div>` +
+							`<div class="row">` +
+							`<div class="col-md-8">` +
+							`<div><input class="form-control" type="text" id="lbl_vendor" name="vendor" value="` + html.EscapeString(data.A_vendor) + `" placeholder="" autocomplete="off"></div>` +
+							`<div class="d-md-none mb-3"></div>` +
+							`</div>` +
+							`<div class="col-md-4">` +
+							`<input class="form-control" type="number" step="1" id="lbl_quantity" name="quantity" value="` + utils.IntToStr(data.A_quantity) + `" placeholder="" autocomplete="off">` +
+							`</div>` +
+							`</div>` +
+							`</div>` +
+							`</div>` +
+							`</div>` +
+							`</div>`
+					},
+				},
+				{
+					Kind:    builder.DFKText,
+					Caption: "Category",
+					Name:    "category",
+					Value:   "0",
+					CallBack: func(field *builder.DataFormField) string {
+						return `<div class="form-group n2">` +
+							`<div class="row">` +
+							`<div class="col-md-3">` +
+							`<label for="lbl_category">Category</label>` +
+							`</div>` +
+							`<div class="col-md-9">` +
+							`<div>` +
+							`<select class="selectpicker form-control" id="lbl_category" name="category" data-live-search="true">` +
+							`<option title="Nothing selected" value="0">&mdash;</option>` +
+							this.shop_GetCategorySelectOptions(wrap, 0, data.A_category, []int{}) +
+							`</select>` +
+							`</div>` +
+							`</div>` +
+							`</div>` +
+							`</div>`
+					},
+				},
 				{
 					Kind:    builder.DFKText,
 					Caption: "Categories",
@@ -823,7 +892,7 @@ func (this *Modules) RegisterModule_Shop() *Module {
 						return `<div class="form-group n5">` +
 							`<div class="row">` +
 							`<div class="col-md-3">` +
-							`<label for="lbl_parent">Categories</label>` +
+							`<label for="lbl_cats">Categories</label>` +
 							`</div>` +
 							`<div class="col-md-9">` +
 							`<div>` +

+ 30 - 0
modules/module_shop_act_modify.go

@@ -19,6 +19,9 @@ func (this *Modules) RegisterAction_ShopModify() *Action {
 		pf_price := wrap.R.FormValue("price")
 		pf_currency := wrap.R.FormValue("currency")
 		pf_alias := wrap.R.FormValue("alias")
+		pf_vendor := wrap.R.FormValue("vendor")
+		pf_quantity := wrap.R.FormValue("quantity")
+		pf_category := wrap.R.FormValue("category")
 		pf_briefly := wrap.R.FormValue("briefly")
 		pf_content := wrap.R.FormValue("content")
 		pf_active := wrap.R.FormValue("active")
@@ -42,6 +45,16 @@ func (this *Modules) RegisterAction_ShopModify() *Action {
 			return
 		}
 
+		if !utils.IsNumeric(pf_quantity) {
+			wrap.MsgError(`Inner system error`)
+			return
+		}
+
+		if !utils.IsNumeric(pf_category) {
+			wrap.MsgError(`Inner system error`)
+			return
+		}
+
 		if pf_name == "" {
 			wrap.MsgError(`Please specify product name`)
 			return
@@ -56,6 +69,11 @@ func (this *Modules) RegisterAction_ShopModify() *Action {
 			return
 		}
 
+		// Default is ROOT
+		if pf_category == "0" {
+			pf_category = "1"
+		}
+
 		// Collect fields and data for filter values
 		filter_values := map[int]int{}
 		for key, values := range wrap.R.PostForm {
@@ -79,6 +97,9 @@ func (this *Modules) RegisterAction_ShopModify() *Action {
 						price = ?,
 						name = ?,
 						alias = ?,
+						vendor = ?,
+						quantity = ?,
+						category = ?,
 						briefly = ?,
 						content = ?,
 						datetime = ?,
@@ -89,6 +110,9 @@ func (this *Modules) RegisterAction_ShopModify() *Action {
 					utils.StrToFloat64(pf_price),
 					pf_name,
 					pf_alias,
+					pf_vendor,
+					utils.StrToInt(pf_quantity),
+					utils.StrToInt(pf_category),
 					pf_briefly,
 					pf_content,
 					utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
@@ -183,6 +207,9 @@ func (this *Modules) RegisterAction_ShopModify() *Action {
 						price = ?,
 						name = ?,
 						alias = ?,
+						vendor = ?,
+						quantity = ?,
+						category = ?,
 						briefly = ?,
 						content = ?,
 						active = ?
@@ -193,6 +220,9 @@ func (this *Modules) RegisterAction_ShopModify() *Action {
 					utils.StrToFloat64(pf_price),
 					pf_name,
 					pf_alias,
+					pf_vendor,
+					utils.StrToInt(pf_quantity),
+					utils.StrToInt(pf_category),
 					pf_briefly,
 					pf_content,
 					pf_active,

+ 1 - 0
support/migrate/000000001.go

@@ -12,4 +12,5 @@ var Migrations = map[string]func(*sqlw.DB, string) error{
 	"000000004": Migrate_000000004,
 	"000000005": Migrate_000000005,
 	"000000006": Migrate_000000006,
+	"000000007": Migrate_000000007,
 }

+ 44 - 0
support/migrate/000000007.go

@@ -0,0 +1,44 @@
+package migrate
+
+import (
+	"golang-fave/engine/sqlw"
+)
+
+func Migrate_000000007(db *sqlw.DB, host string) error {
+	// Changes
+	if _, err := db.Exec(`ALTER TABLE shop_products ADD COLUMN vendor VARCHAR(255) NOT NULL DEFAULT '' AFTER alias;`); err != nil {
+		return err
+	}
+	if _, err := db.Exec(`ALTER TABLE shop_products ADD COLUMN quantity INT(11) NOT NULL DEFAULT 0 AFTER vendor;`); err != nil {
+		return err
+	}
+	if _, err := db.Exec(`ALTER TABLE shop_products ADD COLUMN category INT(11) NOT NULL DEFAULT 1 AFTER quantity;`); err != nil {
+		return err
+	}
+
+	// Indexes
+	if _, err := db.Exec(`ALTER TABLE shop_products ADD KEY FK_shop_products_category (category);`); err != nil {
+		return err
+	}
+
+	// References
+	if _, err := db.Exec(`
+		ALTER TABLE shop_products ADD CONSTRAINT FK_shop_products_category
+		FOREIGN KEY (category) REFERENCES shop_cats (id) ON DELETE RESTRICT;
+	`); err != nil {
+		return err
+	}
+
+	// Remove default
+	if _, err := db.Exec(`ALTER TABLE shop_products ALTER vendor DROP DEFAULT;`); err != nil {
+		return err
+	}
+	if _, err := db.Exec(`ALTER TABLE shop_products ALTER quantity DROP DEFAULT;`); err != nil {
+		return err
+	}
+	if _, err := db.Exec(`ALTER TABLE shop_products ALTER category DROP DEFAULT;`); err != nil {
+		return err
+	}
+
+	return nil
+}

+ 5 - 0
support/schema.sql

@@ -88,6 +88,9 @@ CREATE TABLE shop_products (
 	price float(8,2) NOT NULL COMMENT 'Product price',
 	name varchar(255) NOT NULL COMMENT 'Product name',
 	alias varchar(255) NOT NULL COMMENT 'Product alias',
+	vendor varchar(255) NOT NULL,
+	quantity int(11) NOT NULL,
+	category int(11) NOT NULL,
 	briefly text NOT NULL COMMENT 'Product brief content',
 	content text NOT NULL COMMENT 'Product content',
 	datetime datetime NOT NULL COMMENT 'Creation date/time',
@@ -135,6 +138,7 @@ ALTER TABLE shop_product_images ADD KEY FK_shop_product_images_product_id (produ
 ALTER TABLE shop_products ADD UNIQUE KEY alias (alias);
 ALTER TABLE shop_products ADD KEY FK_shop_products_user (user);
 ALTER TABLE shop_products ADD KEY FK_shop_products_currency (currency);
+ALTER TABLE shop_products ADD KEY FK_shop_products_category (category);
 ALTER TABLE users ADD UNIQUE KEY email (email);
 
 # References
@@ -152,3 +156,4 @@ ALTER TABLE shop_filters_values ADD CONSTRAINT FK_shop_filters_values_filter_id
 ALTER TABLE shop_product_images ADD CONSTRAINT FK_shop_product_images_product_id FOREIGN KEY (product_id) REFERENCES shop_products (id) ON DELETE RESTRICT;
 ALTER TABLE shop_products ADD CONSTRAINT FK_shop_products_user FOREIGN KEY (user) REFERENCES users (id) ON DELETE RESTRICT;
 ALTER TABLE shop_products ADD CONSTRAINT FK_shop_products_currency FOREIGN KEY (currency) REFERENCES shop_currencies (id) ON DELETE RESTRICT;
+ALTER TABLE shop_products ADD CONSTRAINT FK_shop_products_category FOREIGN KEY (category) REFERENCES shop_cats (id) ON DELETE RESTRICT;

+ 3 - 0
utils/mysql_struct_shop_product.go

@@ -7,6 +7,9 @@ type MySql_shop_product struct {
 	A_price    float64
 	A_name     string
 	A_alias    string
+	A_vendor   string
+	A_quantity int
+	A_category int
 	A_briefly  string
 	A_content  string
 	A_datetime int