package main import ( "errors" "fmt" "net/http" "strconv" "gitea.seeseepuff.be/seeseemelk/mysqlite" "github.com/gin-gonic/gin" ) type App struct { db *mysqlite.Db } type IndexVM struct { AssetCount int BrandCount int TotalRamCapacity int Brands []string Types []string } func (a *App) getIndex(c *gin.Context) { vm := &IndexVM{} var err error vm.AssetCount, err = a.GetAssetCount() if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } vm.BrandCount, err = a.GetBrandCount() if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } vm.TotalRamCapacity, err = a.GetTotalRamCapacity() if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } vm.Brands, err = a.GetAllBrands() if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } vm.Types, err = a.GetAllTypes() if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } c.HTML(http.StatusOK, "index", vm) } type DeviceVM struct { Qr int Name string Brand string Type string Description string RamType string RamCapacity int HddCapacity int HddType string HddFormFactor string HddConnection string HddRpm int } func (a *App) getDevice(c *gin.Context) { qr, err := strconv.Atoi(c.Query("id")) if err != nil { c.AbortWithError(http.StatusBadRequest, err) return } var count int err = a.db.Query("SELECT COUNT(*) FROM assets WHERE qr = ?"). Bind(qr). ScanSingle(&count) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } if count == 0 { c.Redirect(http.StatusTemporaryRedirect, "/create?id="+strconv.Itoa(qr)) return } vm := &DeviceVM{Qr: qr} err = a.db.Query("SELECT name, brand, type, description FROM assets WHERE qr = ?"). Bind(qr). ScanSingle(&vm.Name, &vm.Brand, &vm.Type, &vm.Description) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } if vm.Type == "ram" { err = a.db.Query("SELECT type, capacity FROM info_ram WHERE asset = ?"). Bind(qr). ScanSingle(&vm.RamType, &vm.RamCapacity) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } } else if vm.Type == "hdd" { err = a.db.Query("SELECT capacity, type, form_factor, connection, rpm FROM info_hdd WHERE asset = ?"). Bind(qr). ScanSingle(&vm.HddCapacity, &vm.HddType, &vm.HddFormFactor, &vm.HddConnection, &vm.HddRpm) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } } c.HTML(http.StatusOK, "device", vm) } type CreateDeviceVM struct { IsEdit bool // Assets Qr *int Type string AssetBrand string AssetBrands []string AssetName string AssetDescription string // RAM RamType string RamTypes []string RamCapacity int // HDD HddCapacity int HddType string HddTypes []string HddFormFactor string HddFormFactors []string HddConnection string HddConnections []string HddRpm string HddRpms []string } func (a *App) getCreateDevice(c *gin.Context) { var err error vm := &CreateDeviceVM{} vm.Type = c.Query("type") qr := c.Query("id") if qr != "" { qrInt, err := strconv.Atoi(qr) if err != nil { c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("invalid qr: %v", err)) return } vm.Qr = &qrInt } err = a.GetAllGroups(vm) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } if c.Query("edit") != "" && vm.Qr != nil { vm.IsEdit = true err = a.db.Query("SELECT name, type, brand, description FROM assets WHERE qr = ?"). Bind(*vm.Qr). ScanSingle(&vm.AssetName, &vm.Type, &vm.AssetBrand, &vm.AssetDescription) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } if vm.Type == "ram" { err = a.db.Query("SELECT type, capacity FROM info_ram WHERE asset = ?"). Bind(*vm.Qr). ScanSingle(&vm.RamType, &vm.RamCapacity) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } } else if vm.Type == "hdd" { err = a.db.Query("SELECT capacity, type, form_factor, connection, rpm FROM info_hdd WHERE asset = ?"). Bind(*vm.Qr). ScanSingle(&vm.HddCapacity, &vm.HddType, &vm.HddFormFactor, &vm.HddConnection, &vm.HddRpm) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } } } c.HTML(http.StatusOK, "create_device", vm) } func (a *App) postCreateDevice(c *gin.Context) { qr, err := strconv.Atoi(c.PostForm("qr")) if err != nil { c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("invalid qr: %v", err)) } assetType := c.PostForm("asset_type") tx, err := a.db.Begin() defer tx.MustRollback() if err != nil { c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("error beginning tx: %v", err)) return } err = a.DeleteAsset(tx, qr) if err != nil { c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("error removing assets: %v", err)) return } err = tx.Query("INSERT INTO assets (qr, type, brand, name, description) VALUES (?, ?, ?, ?, ?)"). Bind(qr, c.PostForm("asset_type"), c.PostForm("asset_brand"), c.PostForm("asset_name"), c.PostForm("asset_description")). Exec() if err != nil { c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("error inserting assets: %v", err)) return } if assetType == "ram" { err = a.postCreateDeviceRam(c, qr, tx) } else if assetType == "hdd" { err = a.postCreateDeviceHdd(c, qr, tx) } else { c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("invalid type: %s", assetType)) return } if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } err = tx.Commit() if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } c.Redirect(http.StatusSeeOther, "/") } func (a *App) postCreateDeviceRam(c *gin.Context, qr int, tx *mysqlite.Tx) error { var err error capacity := 0 capacityString := c.PostForm("ram_capacity") if capacityString != "" { capacity, err = strconv.Atoi(c.PostForm("ram_capacity")) if err != nil { return err } } switch c.PostForm("ram_capacity_unit") { case "B": case "KB": capacity *= 1024 case "MB": capacity *= 1024 * 1024 case "GB": capacity *= 1024 * 1024 * 1024 default: return errors.New("invalid ram_capacity_unit") } err = tx.Query("INSERT INTO info_ram (asset, type, capacity) VALUES (?, ?, ?)"). Bind(qr, c.PostForm("ram_type"), capacity).Exec() return err } func (a *App) postCreateDeviceHdd(c *gin.Context, qr int, tx *mysqlite.Tx) error { var err error capacity := 0 capacityString := c.PostForm("hdd_capacity") if capacityString != "" { capacity, err = strconv.Atoi(c.PostForm("hdd_capacity")) if err != nil { return err } } switch c.PostForm("hdd_capacity_unit") { case "B": case "KB": capacity *= 1024 case "MB": capacity *= 1024 * 1024 case "GB": capacity *= 1024 * 1024 * 1024 default: return errors.New("invalid hdd_capacity_unit") } err = tx.Query("INSERT INTO info_hdd (asset, capacity, type, form_factor, connection, rpm) VALUES (?, ?, ?, ?, ?, ?)"). Bind(qr, capacity, c.PostForm("hdd_type"), c.PostForm("hdd_form_factor"), c.PostForm("hdd_connection"), c.PostForm("hdd_rpm")). Exec() return err } type BrowseVM struct { Assets []Asset HasRam bool } type Asset struct { Qr int Name string Brand string Type string Description string RamType string RamCapacity int HddCapacity int HddType string HddFormFactor string HddConnection string HddRpm int } func (a *App) getBrowse(c *gin.Context) { brands := c.QueryArray("brand") types := c.QueryArray("type") query := `SELECT assets.qr, assets.name, assets.brand, assets.type, assets.description, info_ram.type, info_ram.capacity, info_hdd.capacity, info_hdd.type, info_hdd.form_factor, info_hdd.connection, info_hdd.rpm FROM assets LEFT JOIN info_ram ON info_ram.asset = assets.qr LEFT JOIN info_hdd ON info_hdd.asset = assets.qr WHERE 1=1` if len(brands) > 0 { query += " AND assets.brand IN (" + placeholders(len(brands)) + ")" } if len(types) > 0 { query += " AND assets.type IN (" + placeholders(len(types)) + ")" } query += "ORDER BY assets.type, assets.brand, assets.name, assets.qr" vm := &BrowseVM{} var err error q := a.db.Query(query).Bind(brands, types) for row := range q.Range(&err) { var asset Asset err := row.Scan(&asset.Qr, &asset.Name, &asset.Brand, &asset.Type, &asset.Description, &asset.RamType, &asset.RamCapacity, &asset.HddCapacity, &asset.HddType, &asset.HddFormFactor, &asset.HddConnection, &asset.HddRpm) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } vm.Assets = append(vm.Assets, asset) if asset.Type == "ram" { vm.HasRam = true } } if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } c.HTML(http.StatusOK, "browse", vm) } type DeleteVM struct { Qr int } func (a *App) getDelete(c *gin.Context) { qr, err := strconv.Atoi(c.Query("id")) if err != nil { c.AbortWithError(http.StatusBadRequest, fmt.Errorf("invalid qr: %v", err)) return } vm := &DeleteVM{Qr: qr} c.HTML(http.StatusOK, "delete", vm) } func (a *App) postDelete(c *gin.Context) { qr, err := strconv.Atoi(c.Query("id")) if err != nil { c.AbortWithError(http.StatusBadRequest, fmt.Errorf("invalid qr: %v", err)) return } tx, err := a.db.Begin() if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } err = a.DeleteAsset(tx, qr) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } err = tx.Commit() if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } c.Redirect(http.StatusSeeOther, "/") } func placeholders(count int) string { if count == 0 { return "" } placeholder := "?" for count > 1 { placeholder += ", ?" count-- } return placeholder }