From 91c901a764dbf2d600366ed1d8ee19c813d3047d Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 5 Feb 2018 02:54:46 +0100 Subject: Adds some database code. --- db.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) (limited to 'db.go') diff --git a/db.go b/db.go index f2f819d..8ea7165 100644 --- a/db.go +++ b/db.go @@ -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 } -- cgit v1.2.3