diff --git a/.gitignore b/.gitignore index fd2d067..a47f9f7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ *.db3 *.db3-* +*.db3.* *.iml .idea pcinv diff --git a/data.go b/data.go index ad08e7c..1ff9338 100644 --- a/data.go +++ b/data.go @@ -1,25 +1,14 @@ package main -import "gitea.seeseepuff.be/seeseemelk/mysqlite" +import ( + "fmt" + "gitea.seeseepuff.be/seeseemelk/mysqlite" +) -func (a *App) GetAllBrands() ([]string, error) { - var brands []string - var err error - for row := range a.db.Query("SELECT brand FROM assets GROUP BY brand ORDER BY brand ASC").Range(&err) { - var name string - err := row.Scan(&name) - if err != nil { - return nil, err - } - brands = append(brands, name) - } - return brands, err -} - -func (a *App) GetAllRAMTypes() ([]string, error) { +func (a *App) getAllTypes(column, table string) ([]string, error) { var types []string var err error - for row := range a.db.Query("SELECT type FROM info_ram GROUP BY type ORDER BY type ASC").Range(&err) { + for row := range a.db.Query(fmt.Sprintf("SELECT %s FROM %s GROUP BY %s ORDER BY %s ASC", column, table, column, column)).Range(&err) { var name string err := row.Scan(&name) if err != nil { @@ -30,6 +19,64 @@ func (a *App) GetAllRAMTypes() ([]string, error) { return types, err } +func (a *App) GetAllBrands() ([]string, error) { + return a.getAllTypes("brand", "assets") +} + +func (a *App) GetAllRamTypes() ([]string, error) { + return a.getAllTypes("type", "info_ram") +} + +func (a *App) GetAllHddTypes() ([]string, error) { + return a.getAllTypes("type", "info_hdd") +} + +func (a *App) GetAllHddFormFactors() ([]string, error) { + return a.getAllTypes("form_factor", "info_hdd") +} + +func (a *App) GetAllHddConnections() ([]string, error) { + return a.getAllTypes("connection", "info_hdd") +} + +func (a *App) GetAllHddSpeeds() ([]string, error) { + return a.getAllTypes("rpm", "info_hdd") +} + +func (a *App) GetAllGroups(vm *CreateDeviceVM) error { + var err error + vm.AssetBrands, err = a.GetAllBrands() + if err != nil { + return err + } + + vm.RamTypes, err = a.GetAllRamTypes() + if err != nil { + return err + } + + vm.HddTypes, err = a.GetAllHddTypes() + if err != nil { + return err + } + + vm.HddFormFactors, err = a.GetAllHddFormFactors() + if err != nil { + return err + } + + vm.HddFormFactors, err = a.GetAllHddConnections() + if err != nil { + return err + } + + vm.HddRpms, err = a.GetAllHddSpeeds() + if err != nil { + return err + } + return nil +} + func (a *App) GetAllTypes() ([]string, error) { var types []string var err error diff --git a/main.go b/main.go index 2567272..14526ea 100644 --- a/main.go +++ b/main.go @@ -43,12 +43,14 @@ func main() { templates, err := template.New("undefined.gohtml"). Funcs(template.FuncMap{ - "statusText": http.StatusText, - "createDeviceLink": createDeviceLink, - "formatMemorySize": formatMemorySize, - "formatMemoryPlainSize": formatMemoryPlainSize, - "formatType": formatType, - "isRamType": isRamType, + "statusText": http.StatusText, + "createDeviceLink": createDeviceLink, + "formatMemorySize": formatMemorySize, + "formatMemoryPlainSize": formatMemoryPlainSize, + "formatType": formatType, + "isRamType": isRamType, + "createSelectMenu": createSelectMenu, + "createSelectMenuDefault": createSelectMenuDefault, }). ParseFS(templateFS, "templates/*.gohtml") diff --git a/migrations/2_add_harddrives.sql b/migrations/2_add_harddrives.sql new file mode 100644 index 0000000..d2b0225 --- /dev/null +++ b/migrations/2_add_harddrives.sql @@ -0,0 +1,8 @@ +create table info_hdd ( + asset integer not null unique, + capacity integer, + type text, + form_factor text, + connection text, + rpm integer +); diff --git a/template_funcs.go b/template_funcs.go index c573463..c7d4481 100644 --- a/template_funcs.go +++ b/template_funcs.go @@ -91,7 +91,31 @@ func formatType(t string) string { switch t { case "ram": return "Random Access Memory" + case "hdd": + return "Hard Disk Drive" default: return t } } + +type SelectMenu struct { + Name string + Label string + Selected string + Options []string + DefaultValue string +} + +func createSelectMenu(name, label, selected string, options []string) SelectMenu { + return createSelectMenuDefault(name, label, selected, options, "Unknown") +} + +func createSelectMenuDefault(name, label, selected string, options []string, defaultValue string) SelectMenu { + return SelectMenu{ + Name: name, + Label: label, + Selected: selected, + Options: options, + DefaultValue: defaultValue, + } +} diff --git a/templates/create_device_step1.gohtml b/templates/create_device_step1.gohtml index c100beb..e4369e8 100644 --- a/templates/create_device_step1.gohtml +++ b/templates/create_device_step1.gohtml @@ -9,8 +9,10 @@ {{define "create_device_link"}} {{if .Qr}} -