summaryrefslogtreecommitdiff
path: root/db.go
blob: 8ea716570345ce926d0c201de410090586d0db9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main

import (
	//	"github.com/jmoiron/sqlx"
	"database/sql"
)

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
}