diff options
Diffstat (limited to 'db.go')
| -rw-r--r-- | db.go | 60 |
1 files changed, 58 insertions, 2 deletions
@@ -1,8 +1,64 @@ package main import ( - "github.com/jmoiron/sqlx" + // "github.com/jmoiron/sqlx" + "database/sql" ) -func save_offer(W []Angebot) { +func (app *App) createTables() error { + query1 := `CREATE TABLE IF NOT EXISTS angebot ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + shop text, + name text, + url text, + original_price int, + discounted_price int, + valid_until text, + image_url text, + spirit_type text, + created_at text + )` + _, err := app.DB.Exec(query1) + if err != nil { + return err + } + + query2 := `CREATE TABLE IF NOT EXISTS shop( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name text, + url text, + logo_url text, + shipping_costs text, + free_shipping text + )` + _, err = app.DB.Exec(query2) + return err +} + +func (app *App) save_offer(W []Angebot) error { + + query := `INSERT INTO angebot (shop, name, url, original_price, discounted_price, valid_until, image_url, spirit_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?)` + + stmt, err := app.DB.Prepare(query) + if err != nil { + return err + } + + for _, o := range W { + + var found int + + err := app.DB.QueryRow("SELECT 1 FROM angebot WHERE shop = ? AND name = ? AND url = ? AND original_price = ? AND discounted_price = ? AND image_url = ? AND spirit_type = ?", o.Shop, o.Name, o.Url, o.Original_price, o.Discounted_price, o.Image_url, o.Spirit_type).Scan(&found) + + if err == sql.ErrNoRows { + + _, err = stmt.Exec(o.Shop, o.Name, o.Url, o.Original_price, o.Discounted_price, o.Valid_until, o.Image_url, o.Spirit_type) + if err != nil { + return err + } + + } + } + + return nil } |
