package model import "trading-go/common" type Category struct { Id uint `db:"id"` Name string `db:"name"` Pic string `db:"pic"` } type CategoryJson struct { Name string `json:"name"` Pic string `json:"pic"` } type CategoryOfGoods struct { Id uint `db:"id"` GoodsId uint `db:"goods_id"` CategoryId uint `db:"category_id"` } func (j CategoryJson) Change() Category { return Category{ Name: j.Name, Pic: j.Pic, } } func (c Category) GetAll() (categories []Category, err error) { db := common.DB sqlStr := "SELECT * FROM categories" err = db.Select(&categories, sqlStr) return } func (c Category) Create() error { db := common.DB sqlStr := "INSERT INTO categories(name, pic) VALUES(:name, :pic)" _, err := db.NamedExec(sqlStr, c) return err } func (c Category) Delete() error { db := common.DB sqlStr := "DELETE FROM categories WHERE id = ?" _, err := db.Exec(sqlStr, c.Id) return err } func (g CategoryOfGoods) Save(id uint, categories []uint) error { db := common.DB sqlStr := "INSERT INTO category_of_goods(goods_id, category_id) VALUES (?, ?)" for _, category := range categories { _, err := db.Exec(sqlStr, id, category) if err != nil { return err } } return nil }