// Save creates a new Customer on database, or updates if it already exists. func (c *Customer) Save(db *sql.DB) error { if c.Exists() { err := utils.ExecSQL(db, customerUpdateSQL, c.ID, c.Name) if err != nil { return err } } else { err := utils.ExecSQL(db, customerInsertSQL, c.ID, c.Name) if err != nil { return err } } return nil }
// Save creates a new OrderItem on database, or updates if it already exists. func (o *OrderItem) Save(db *sql.DB) error { if o.Exists() { err := utils.ExecSQL(db, oitemUpdateSQL, o.ItemOrder.ID, o.ItemProduct.ID, o.Quantity, o.UnitPrice) if err != nil { return err } } else { err := utils.ExecSQL(db, oitemInsertSQL, o.ItemOrder.ID, o.ItemProduct.ID, o.Quantity, o.UnitPrice) if err != nil { return err } } return nil }
// Save creates a new Product on database, or updates if it already exists. func (p *Product) Save(db *sql.DB) error { if p.Exists() { err := utils.ExecSQL(db, productUpdateSQL, p.ID, p.Name, p.Description, p.Price) if err != nil { return err } } else { err := utils.ExecSQL(db, productInsertSQL, p.ID, p.Name, p.Description, p.Price) if err != nil { return err } } return nil }
// Delete removes an OrderItem from database. func (o *OrderItem) Delete(db *sql.DB) error { err := utils.ExecSQL(db, oitemDeleteSQL, o.ItemOrder.ID, o.ItemProduct.ID) if err != nil { return err } return nil }
// Delete removes an Order from database. func (o *Order) Delete(db *sql.DB) error { err := utils.ExecSQL(db, orderDeleteSQL, o.ID) if err != nil { return err } return nil }
// Delete removes a Customer from database. func (c *Customer) Delete(db *sql.DB) error { err := utils.ExecSQL(db, customerDeleteSQL, c.ID) if err != nil { return err } return nil }
// Delete removes a Product from database. func (p *Product) Delete(db *sql.DB) error { err := utils.ExecSQL(db, productDeleteSQL, p.ID) if err != nil { return err } return nil }
// Save creates a new Order on database, or updates if it already exists. // Note that the Customer must be already in the database. // FIXME: Not using transactions! func (o *Order) Save(db *sql.DB) error { exists := o.Exists() if !exists { err := utils.ExecSQL(db, orderInsertSQL, o.ID, o.OrderCustomer.ID) if err != nil { return err } } else { return fmt.Errorf("Order %s already exists.", o.ID) } for _, i := range o.Items { err := i.Save(db) if err != nil { return err } } return nil }