This repository was archived by the owner on Apr 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.go
More file actions
80 lines (70 loc) · 2.15 KB
/
db.go
File metadata and controls
80 lines (70 loc) · 2.15 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"fmt"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
var db, _ = gorm.Open(sqlite.Open("db/ark-en.sqlite"), &gorm.Config{})
// Fetches all the known product IDs and names from the resource DB that matches the search term
func getProductIDs(searchTerm string) []rawProduct {
if len(searchTerm) == 0 {
return make([]rawProduct, 0)
}
// var products []rawProduct
products := make([]rawProduct, 0)
query := `SELECT
Product.ARKID AS ArkID,
Product.pkProductID AS ID,
Resource.Value AS Title
FROM Resource
LEFT JOIN Product
ON Product.fkProductNameID = Resource.fkResourceID
WHERE Resource.Value LIKE "%` + searchTerm + `%"
AND Resource.Value NOT LIKE "%Tray%"
AND Resource.Value NOT LIKE "%China%"
AND Resource.VALUE NOT LIKE "%Boxed%"
AND Product.pkProductID IS NOT NULL;
`
db.Raw(query).Scan(&products)
return products
}
// Fetches a string from the resource DB by ID
func convertIDToValue(resourceID int) string {
var result string
query := fmt.Sprintf("SELECT Value FROM Resource WHERE fkResourceID = %d", resourceID)
db.Raw(query).Scan(&result)
return result
}
// Converts a rawProduct to a populatedProduct
// Fetches all the spec rows for the product
func getProductSpecs(product rawProduct) populatedProduct {
var rawSpecFields []rawSpecField
query := fmt.Sprintf(`
SELECT DISTINCT
Specification.fkSectionID AS SectionID,
Specification.fkNameID AS NameID,
Specification.fkValueID AS ValueID
FROM Specification
LEFT JOIN ProductSpecificationLink
ON ProductSpecificationLink.fkProductID = %d
WHERE Specification.pkSpecificationID = ProductSpecificationLink.fkSpecificationID;
`, product.ID)
db.Raw(query).Scan(&rawSpecFields)
populatedSpecFields := make([][]string, 0)
for i := 0; i < len(rawSpecFields); i++ {
rawSpecRow := rawSpecFields[i]
specRow := make([]string, 3)
specRow[0] = convertIDToValue(rawSpecRow.SectionID)
specRow[1] = convertIDToValue(rawSpecRow.NameID)
specRow[2] = convertIDToValue(rawSpecRow.ValueID)
populatedSpecFields = append(
populatedSpecFields,
specRow,
)
}
return populatedProduct{
ArkID: product.ArkID,
Title: product.Title,
Specs: populatedSpecFields,
}
}