Add ability to browse data

This commit is contained in:
2025-03-24 12:26:31 +01:00
parent c459148d33
commit dbfbd6e164
9 changed files with 272 additions and 5 deletions

121
views.go
View File

@@ -16,11 +16,33 @@ type App struct {
type IndexVM struct {
AssetCount int
BrandCount int
Brands []string
Types []string
}
func (a *App) getIndex(c *gin.Context) {
vm := &IndexVM{}
err := a.db.Query("SELECT COUNT(*) FROM assets").ScanSingle(&vm.AssetCount)
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.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
@@ -29,6 +51,15 @@ func (a *App) getIndex(c *gin.Context) {
c.HTML(http.StatusOK, "index", vm)
}
type DeviceVM struct {
Name string
Brand string
Type string
Description string
RamType string
RamCapacity int
}
func (a *App) getDevice(c *gin.Context) {
qr, err := strconv.Atoi(c.Query("id"))
if err != nil {
@@ -49,6 +80,27 @@ func (a *App) getDevice(c *gin.Context) {
c.Redirect(http.StatusTemporaryRedirect, "/create?id="+strconv.Itoa(qr))
return
}
vm := &DeviceVM{}
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
}
}
c.HTML(http.StatusOK, "device", vm)
}
type CreateDeviceVM struct {
@@ -153,3 +205,70 @@ func (a *App) postCreateDeviceRam(c *gin.Context, qr int) error {
}
return tx.Commit()
}
type BrowseVM struct {
Assets []Asset
HasRam bool
}
type Asset struct {
Qr int
Name string
Brand string
Type string
Description string
RamType string
RamCapacity 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
FROM assets
JOIN info_ram ON info_ram.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)) + ")"
}
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)
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)
}
func placeholders(count int) string {
if count == 0 {
return ""
}
placeholder := "?"
for count > 1 {
placeholder += ", ?"
count--
}
return placeholder
}