aboutsummaryrefslogtreecommitdiff
path: root/dataimport/importer.go
blob: 0d121e5d8c0bd023af011e23d8f6476f8eee31e1 (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
package dataimport

import (
	"log"

	"fagott.pw/grilist/models"
)

func logErr(err error) {
	if err != nil {
		log.Printf("IMPORT ERROR: %v\n", err)
	}
}

func AssignGril(foreign int, id int, dataSource models.DataSource) error {
	_, err := database.Exec("INSERT INTO grilist.grils_id_mappings (gril_id, source, source_id) VALUES ($1, $2, $3);", id, models.DataSourceAnilist, foreign)
	if err != nil {
		return err
	}
	return nil
}

func InsertGril(g *ImporterGril, dataSource models.DataSource) (int, error) {
	var dbID int
	if err := database.QueryRow("INSERT INTO grilist.grils (age) VALUES (NULL) RETURNING id;").Scan(&dbID); err != nil {
		return 0, err
	}
	if err := AssignGril(g.ID, dbID, dataSource); err != nil {
		return 0, err
	}
	if g.Gril.KanjiName != "" {
		_, err := database.Exec(`INSERT INTO
		grilist.gril_names (gril_id, name, name_type)
		VALUES ($1, $2, $3);`, dbID, g.KanjiName,
			0)
		logErr(err)
	}
	if g.Gril.RomajiName != "" {
		_, err := database.Exec(`INSERT INTO
		grilist.gril_names (gril_id, name, name_type)
		VALUES ($1, $2, $3);`, dbID, g.RomajiName,
			1)
		logErr(err)
	}
	for _, v := range g.OtherNames {
		_, err := database.Exec(`INSERT INTO
		grilist.gril_names (gril_id, name, name_type)
		VALUES ($1, $2, $3);`, dbID, v, 2)
		logErr(err)
	}
	return dbID, nil
}